I'm creating a quiz. I have 3 buttons (options) in the activity and its corresponding questions. Now, my problem is I want to show the toast message, when the user chose the correct answer the toast message will appear in a few seconds, but when the user choose the wrong answer it will again display the toast message. I do not know how to do that.
I have done many research and read forums, but seems I don't understand and it don't meet my needs. Can someone please help? thanks in advance!
So far, here is the code. But it doesn't work.
Please correct me which code is wrong. Thanks deeply.
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button btn1 = (Button) findViewById(R.id.btnopt1_a);
btn1.isClickable();
switch(arg0.getId()){
case R.id.btnopt1_a:
if(btn1.isPressed()){
Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();
}
else btn1.setText("Your answer is wrong!, The correct answer is: Frog");
break;
}
}
});
To show toast message when you click button, use the following code. If you want to do some validations then use the code in your onclicklistener of button:
Button btn1 = (Button) findViewById(R.id.btnopt1_a);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With