1

I am using Android Studio. I want show "correct" or "incorrect" using Toast in my app for the answers. How to show Toast messages? Below is my code:

public class LayarKuisUmumNo1 extends Activity {

Button bt;
TextView tv;
RadioGroup rg;
RadioButton rbu1, rbu2, rbu3, rbu4;

public static String question[] = { "Negara terluas keempat di dunia"};
String answer[] = { "Amerika"};
String opts[] = {   "Rusia", "Australia", "Amerika", "Indonesia" };
int position = 0;
public static int correct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layar_kuis_umum_no1);

    bt = (Button) findViewById(R.id.btnextu1);
    tv = (TextView) findViewById(R.id.pertanyaanu1);
    rg = (RadioGroup) findViewById(R.id.radioGroup2);
    rbu1 = (RadioButton) findViewById(R.id.rbu1a);
    rbu2 = (RadioButton) findViewById(R.id.rbu1b);
    rbu3 = (RadioButton) findViewById(R.id.rbu1c);
    rbu4 = (RadioButton) findViewById(R.id.rbu1d);

    tv.setText(question[position]);
    rbu1.setText(opts[position]);
    rbu2.setText(opts[position + 1]);
    rbu3.setText(opts[position + 2]);
    rbu4.setText(opts[position + 3]);

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton selectedans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
            String selectedanstext = selectedans.getText().toString();

            if (selectedanstext == answer[position]) {
                correct++;
            }
            position++;
            if (position < question.length) {
                tv.setText(question[position]);
                rbu1.setText(opts[position * 4]);
                rbu2.setText(opts[position * 4 + 1]);
                rbu3.setText(opts[position * 4 + 2]);
                rbu4.setText(opts[position * 4 + 3]);
            } else {
                Intent in = new Intent(getApplicationContext(), LayarNilaiUmum1.class);
                startActivity(in);
            }
        }
    });
}
}
Mangesh
  • 5,491
  • 5
  • 48
  • 71
rionymous_
  • 11
  • 7
  • Your english is ok, no problem :). Just add Toast.makeText(context,"HELLO",Toast.LENGTH_LONG).show(); – Opiatefuchs Mar 18 '16 at 08:40
  • 1
    please read documentation or search in internet, because many answer for your problem is exist. for checking string don't use `==` in java, use `.equals()` – Shayan Pourvatan Mar 18 '16 at 08:40
  • 1
    Possible duplicate of [How to display Toast in Android?](http://stackoverflow.com/questions/3500197/how-to-display-toast-in-android) – cafebabe1991 Mar 18 '16 at 08:48
  • i want to say thanks for everyone. now,my app is working. thanks for Opiatefuchs for shayan pourvatan for cafebabe1991 – rionymous_ Mar 18 '16 at 11:34

2 Answers2

0

The code to add Toast message is this,

    Toast.makeText(this, "ZOOM Or use Landscape mode",
            Toast.LENGTH_SHORT).show();

Basically, the first parameter is the context, second one is the string that contains the message you want to display and the last parameter is the duration you want the message to be displayed for.

Natarajan Raman
  • 606
  • 1
  • 4
  • 14
0

Here is my code:

public class MainActivity extends Activity {

    RadioGroup rg;
    RadioButton rbut1;
    Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg = (RadioGroup) findViewById(R.id.radioGroup1);

        bt = (Button) findViewById(R.id.button1);

        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int selectedId = rg.getCheckedRadioButtonId();
                rbut1 = (RadioButton) findViewById(selectedId);
                Toast.makeText(MainActivity.this, rbut1.getText().toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });

    }
}
Prakash Gajera
  • 563
  • 1
  • 6
  • 18