1

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 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>
Bikal Nepal
  • 477
  • 1
  • 10
  • 26

3 Answers3

2
   if(Answer.equals("4") || (Answer.equals("four")))
   {
         //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();
   }
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • 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; – SuperFrog 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 – SuperFrog Aug 30 '15 at 10:52
  • Why you want to use hander if with an `Intent` would work perfect? – Skizo-ozᴉʞS ツ Aug 30 '15 at 10:54
  • Because if you just start the new activity the user won't understand what happend. This way the user will see the toast and only then will be transferred to the new activity. – SuperFrog Aug 30 '15 at 10:55
  • The toast is shown anyways, you could change your toast and put "Correct, going to next level..." and user would know what's going on, but I don't know if use a `Handler` on every Activity would be a good idea (it works, I know, I'm talking about efficiencie) :P – Skizo-ozᴉʞS ツ Aug 30 '15 at 10:58
  • 1
    Perfect! Thank you so much! :) – Bikal Nepal Aug 30 '15 at 10:59
  • How would the user see the toast if on the next line you're starting a new activity? try your code and tell me if you can read the toast ;) – SuperFrog Aug 30 '15 at 10:59
  • Umm..I'm not understanding the technical talk, guys. But it worked. Thanks to both of you! :) – Bikal Nepal Aug 30 '15 at 11:00
  • @UdiI if I posted an answer is because I tested it. Just test yourself, tell me if you make a Toast before an Intent you can't see the Toast... I just go to the new Activity and I was able to see the toast cause it's Toast.LENGHT_LONG() – Skizo-ozᴉʞS ツ Aug 30 '15 at 11:01
  • @Bik we are talking about efficience code, the two answers (his answer and mine) would work perfectly, so use the answer that you prefer :) it's your code. – Skizo-ozᴉʞS ツ Aug 30 '15 at 11:03
1

You can use intents to switch to another activity.

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.

Zarwan
  • 5,537
  • 4
  • 30
  • 48
1

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

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();
    }
}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • 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. – SuperFrog 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
  • Yes but I'll need some time to do that... if you want accept this question and ask another question since i've solved your main problem... now I've got no time but tomorrow if you send me the link I'll post an answer :) – Skizo-ozᴉʞS ツ Aug 30 '15 at 20:38
  • @Bik Have you asked the new question? My answer didn't help to you? You didn't mark it as a correct is there any problem? – Skizo-ozᴉʞS ツ Aug 31 '15 at 21:22
  • Yes, your answer helped, thank you so much! I've got two questions now... http://stackoverflow.com/questions/32324876/how-to-save-an-answer-in-a-riddle-game-without-creating-a-database and http://stackoverflow.com/questions/32299276/how-to-automatically-display-a-foreign-keyboard-instead-of-default-english-keybo/32299740#32299740 – Bikal Nepal Sep 01 '15 at 06:26
  • I just answered this [question](http://stackoverflow.com/questions/32324876/how-to-save-an-answer-in-a-riddle-game-without-creating-a-database) hope it helps if it does not just let me know and I'll try to update it. – Skizo-ozᴉʞS ツ Sep 01 '15 at 09:08
  • No, I'm having trouble putting the code in the right place! :( Is it possible for you to put them in the proper place and post the code? – Bikal Nepal Sep 01 '15 at 14:17