user820913
user820913

Reputation: 619

Android, using Toast inside onClickListener

I am trying to make a toast display some text given certain conditions inside an onClickListener. The app won´t run in the simulator, and I get the following error: "void cannot be converted to Toast"

I have searched around, and find several similar problems and solutions on this forum, but none of them applies completely to my problem. The others haven´t used the correct context in the statement, but I really mean that I do. (The name of the javafile (context) is: "Case1Activity") Can anyone help me with this? I have simplified the code a bit:

public void onClick(View view) {
            if (button1Pushed == false){
                count++;
                Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
            }


        }
    });

Upvotes: 5

Views: 12392

Answers (4)

Androider
Androider

Reputation: 3873

Dear Friend check below before eating Toast,

Your Toast (Incompatible types error) :

 Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show(); 

Normal Case (Standard Use):

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();`

Special Case (if you need reference of Toast):

 View toast = Toast.makeText(MainActivity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();

because here ".show()" method is -

public void show () which shows the view for the specified duration.

Thanks

Upvotes: 2

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

if you want to use assignment operator then you can use below code

Toast toast = Toast.makeText(context, text, duration);
toast.show();

Upvotes: 3

Sourabh Bans
Sourabh Bans

Reputation: 3134

apply it as.

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();

Upvotes: 4

Maksim Ostrovidov
Maksim Ostrovidov

Reputation: 11058

do it without assignment statement

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();

Upvotes: 9

Related Questions