Sunteți pe pagina 1din 5

How to automatically go to next level in a game after completion of first level?

Asked 4 years, 1 month ago Active 4 years, 1 month ago Viewed 1k times

I'm trying to develop a riddle game with levels. First level is in "OneActivity.java" and "activity_one.xml" 2nd level on "TwoActivity.java"
and "activity_two.xml" and so on. After the user types the answer to the question in level one, a toast display with "Correct" is displayed if
1 the answer is correct and "Wrong" if it's incorrect. Now, how do I automatically go to next level if the answer is correct but remain on
same level until the user inputs the correct answer. Here's my OneActivity.java and activity_one.xml

OneActivity.java:

package com.golo.user.gaunkhanekatha;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class OneActivity extends Activity {

public Button check;


public EditText typeh;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
check = (Button)findViewById(R.id.check); //R.id.button is the id on your xml
typeh = (EditText)findViewById(R.id.typeh); //this is the EditText id
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//Here you must get the text on your EditText
String Answer = (String) typeh.getText().toString(); //here you have the
text typed by the user
//You can make an if statement to check if it's correct or not
if(Answer.equals("4") || (Answer.equals("four")))
{
//Create a Toast because it's correct
Toast.makeText(OneActivity.this, "Correct!",
Toast.LENGTH_LONG).show();
}
else{
//It's not the correct answer
Toast.makeText(OneActivity.this, "Wrong! Try Again",
Toast.LENGTH_LONG).show();
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_aboutus, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

activity_one.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:weightSum="1"
android:textAlignment="center"
android:id="@+id/oneque">

<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:text="What is 2+2?"
android:id="@+id/que"
android:width="255dp"
android:textSize="30dp"
android:layout_margin="50dp"
android:textStyle="italic"
android:gravity="center" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/typeh"
android:layout_gravity="center_horizontal"
android:text="Type Here" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Answer"
android:id="@+id/check"
android:layout_gravity="center_horizontal" />
</LinearLayout>

activity_level.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToOneActivity"
android:background="@drawable/one" />

<Button
android:id="@+id/button2"

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToTwoActivity"
android:background="@drawable/two" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToThreeActivity"
android:background="@drawable/three" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToFourActivity"
android:background="@drawable/four" />

<Button
android:id="@+id/button5"

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToFiveActivity"
android:background="@drawable/five" />

<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToSixActivity"
android:background="@drawable/six" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToSevenActivity"
android:background="@drawable/seven" />

<Button
android:id="@+id/button8"

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToEightActivity"
android:background="@drawable/eight" />

<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToNineActivity"
android:background="@drawable/nine" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToTenActivity"
android:background="@drawable/ten" />

<Button
android:id="@+id/button11"

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToElevenActivity"
android:background="@drawable/eleven" />

<Button
android:id="@+id/button12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToTwelveActivity"
android:background="@drawable/twelve" />
</LinearLayout>
</LinearLayout>

java android

edited Aug 30 '15 at 10:53 asked Aug 30 '15 at 9:56


Bikal Nepal
147 1 13

Each level is on seperate activity? – Udi Idan Aug 30 '15 at 9:59

Yes, each level is on separate activity. – Bikal Nepal Aug 30 '15 at 10:11

Intent to another activity same answer is correct and if wrong do nothing on submit.. – Iamat8 Aug 30 '15 at 10:40

Have you checked my answer? You don't have to do anything else just 2 lines of code – Skizo-ozᴉʞS Aug 30 '15 at 10:53

@Bik You should probably check that getText() does not return null before proceeding to process it. – JoxTraex Aug 30 '15 at 11:15

3 Answers

To go to the second level you should add this Intent inside your correct Toast as follows :

1
Intent i = new Intent(OneActivity.this, TwoActivity.class);
startActivity(i);
finish();

This should be your code :

check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//Here you must get the text on your EditText
String Answer = (String) typeh.getText().toString(); //here you have the
text typed by the user
//You can make an if statement to check if it's correct or not
if(Answer.equals("4") || (Answer.equals("four")))
{
//Create a Toast because it's correct
Toast.makeText(OneActivity.this, "Correct! Going to Level 2...",
Toast.LENGTH_LONG).show();
Intent i = new Intent(OneActivity.this, TwoActivity.class);
startActivity(i);
finish();
}
else{
//It's not the correct answer
Toast.makeText(OneActivity.this, "Wrong! Try Again",
Toast.LENGTH_LONG).show();
}
}
});

EDIT

This edit will cancel the Toast when you are on your TwoActivity.class , you have to change some stuff, I'll exaplain to you.

1.- Create a global Toast variable.

private Toast toast;

2.- Initialize it on your onCreate() like this :

toast = Toast.makeText(OneActivity.this, "", Toast.LENGTH_SHORT);

3.- Then change your two Toast messages to this :

//Correct Toast
toast.setText("Correct! Going to Level 2...");
toast.show();

//Incorrect Toast
toast.setText("Wrong! Try Again");
toast.show();

4.- You want to make a finish() to avoid the back button to return to OneActivity() so you will call it, and it calls onDestroy() so you
have to add this method aswell to cancel the Toast

@Override
protected void onDestroy() {
super.onDestroy();
if(toast!= null) {
toast.cancel();
}
}

edited Aug 30 '15 at 11:32 answered Aug 30 '15 at 10:37


Skizo-ozᴉʞS
11.3k 13 49 111

Yes, Your way seems to be faster but I keep seeing the previous toast "Correct" even when I proceed to new level with another question. Is there a way
to decrease the time for which the toast is displayed on the scree? – Bikal Nepal Aug 30 '15 at 11:14

Just change the toast to this : Toast.makeText(OneActivity.this, "Correct! Going to Level 2...", Toast.LENGTH_SHORT).show(); – Skizo-ozᴉʞS
Aug 30 '15 at 11:15

It's not faster, it just not using the handler, so you navigate to the next activity immediately. The purpose of the handler was to avoid what you're
experiencing now, Anyway good luck. – Udi Idan Aug 30 '15 at 11:26

I've edited my answer. @Bik feel free to use the code what you want we just gave to you two differents ways to do it, all both WILL WORK :D So feel
free to mark any asnwer as a correct! :) – Skizo-ozᴉʞS Aug 30 '15 at 11:33

Thank you! Works like a charm...one small query again. When the user returns back to level one, s/he has to type the answer again even though s/he's
solved it before. Is there anyway in JAVA to keep the answer in the type panel (if the user's solved it that is) without having to create a database?? –
Bikal Nepal Aug 30 '15 at 17:29
if(Answer.equals("4") || (Answer.equals("four")))
{
2 //Create a Toast because it's correct
Toast.makeText(OneActivity.this, "Correct!",
Toast.LENGTH_LONG).show();

//Here create intent to the new activity - for example


//If you wish the user will have time to see the toast, you can use a Handler
with post delayed

new Handler().postDelayed(new Runnable() {


@Override
public void run() {
Intent newLevel= new Intent(OneActivity.this, TwoActivity.class);
startActivity(newLevel);
finish(); //finish the activity of the current level
}
}, 3000); //the code inside run() will be executed after 3 seconds so the user
can see the toast

}
else
{
//It's not the correct answer
Toast.makeText(OneActivity.this, "Wrong! Try Again",
Toast.LENGTH_LONG).show();
}

edited Aug 30 '15 at 10:53 answered Aug 30 '15 at 10:06


Udi Idan
5,434 7 40 74

thank you for your reply. It is showing 1 error in "new Handler()" saying "'Handler' is abstract; cannot be instantiated" – Bikal Nepal Aug 30 '15 at 10:20

1 You probably using the wrong import: import java.util.logging.Handler; Change it to import android.os.Handler; – Udi Idan Aug 30 '15 at 10:25

It worked but when I press the back button, it goes back to the question of level one. How do I make it to go to the level selection window. There's a
different activity_level.xml for level selection window. I've added the xml in the question, please take a look as I'm unable to post another question for 90
min and I need help real fast! – Bikal Nepal Aug 30 '15 at 10:51

Just call finish() on the activity, I have edited my answer – Udi Idan Aug 30 '15 at 10:52

1 Perfect! Thank you so much! :) – Bikal Nepal Aug 30 '15 at 10:59

You can use intents to switch to another activity.

1 Intent myIntent = new Intent(this, MyActivity.class);


startActivity(myIntent);

You could simply have an if statement. If the answer is correct, create an intent and start activity two, otherwise reloop in activity one or
do whatever other behaviour you want to do if the answer is wrong.

answered Aug 30 '15 at 10:07


Zarwan
3,864 2 21 46

S-ar putea să vă placă și