user3274770
user3274770

Reputation: 1

correct text input to next activity

I am a completely new programmer and I am looking to make a sort of quiz app as my first app. Just Questions with the right answer that will send the user to the next activity. I figured out how to edit buttons but I am not sure what to add to the JAVA file or XML file that will allow the next activity (screen) to be opened up.

This is my Layout so far. I have decided the password input but the answer to the question will be stored within the app. Not sure if this is the right approach?

`

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Question" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="78dp"
    android:ems="10"
    android:inputType="textPassword" />

 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/textView1"
     android:layout_centerHorizontal="true"
     android:text="Answer" />

`

Thanks in advance for any help rendered!

@kanwaljit Sngh

I am getting multiple errors like "button cannot be resolved to a type" and "R cannot be resolved to a variable" What does these mean?

`import android.os.Bundle; import android.app.Activity; import android.view.Menu;

public class Startscreen extends Activity {

  EditText editText1 = (EditText) findViewById(R.id.editText1);
    Button btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

                String answer = editText1.getText().toString().trim();
                if (answer.equals("desired answer")) {
                    Intent i = new Intent(getApplicationContext(),
                            CorrectAnswerActivity.class);
                    startActivity(i);
                } else {
                    Intent i = new Intent(getApplicationContext(),
                            WrongAnswerActivity.class);
                    startActivity(i);
                }
            }

    });

`

Upvotes: 0

Views: 586

Answers (2)

Sandip_Jahdav
Sandip_Jahdav

Reputation: 147

    EditText editText1 = (EditText) findViewById(R.id.editText1);
    Button btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

                String answer = editText1.getText().toString().trim();
                if (answer.equals("desired answer")) {
                    Intent i = new Intent(getApplicationContext(),
                            CorrectAnswerActivity.class);
                    startActivity(i);
                } else {
                    Intent i = new Intent(getApplicationContext(),
                            WrongAnswerActivity.class);
                    startActivity(i);
                }
            }

    });

and write new two activity CorrectAnswerActivty and WrongAnswerActivity and do actions what to display in these activties and add the following line to the xml file in button tag-

android:id="@+id/button"

Upvotes: 0

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

  1. Get the value of edittext

  2. check it with desired answer

  3. If true, redirect to next activity

Eg :

  if(editText1.getText().toString().equalsIgnoreCase("desiredanswer")
  {
               startActivity(new Intent(this,nextActivity.class));
  }
  else
  {
                Toast.makeText(this,"Wrong answer",2000).show();
  }

Upvotes: 1

Related Questions