How to make toast message when button is clicked

41,424

Solution 1

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();   
    }
});

Solution 2

Try this, First implements Your Activity OnClickListener . And then each end every click check condition you will get solution.

public class Deals extends Activity implements OnClickListener {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.yourxml);
        //declare ur buttons here
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu: {
                // Do your stuff here
                // call method
                if (isright) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "Your answer wrong!", Toast.LENGTH_SHORT ).show();
                }
            }
        }
    }
}

Solution 3

If you have many conditions in which you have to show toast. Then it would be better to make a method which shows it.

private void showToast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

And call the above method in the onClick() or wherever you want to show.

 btn1.setOnClickListener(new OnClickListener() {
   public void onClick(View v)  {
       ..................
       ..........................

       case R.id.btnopt1_a:  
           if (btn1.isPressed()) {
               showToast("Your answer is correct!");
           } else {
               showToast("Your answer is wrong!, The correct answer is: Frog");
           }

           break;
       });
Share:
41,424
Mary Grace
Author by

Mary Grace

Updated on July 13, 2022

Comments

  • Mary Grace
    Mary Grace almost 2 years

    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;  
               }
          }
        });