Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


        }
    });
like image 610
user820913 Avatar asked Nov 05 '16 12:11

user820913


3 Answers

do it without assignment statement

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
like image 87
Maksim Ostrovidov Avatar answered Oct 12 '22 04:10

Maksim Ostrovidov


apply it as.

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
like image 42
SRB Bans Avatar answered Oct 12 '22 04:10

SRB Bans


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

Toast toast = Toast.makeText(context, text, duration);
toast.show();
like image 3
Jitesh Mohite Avatar answered Oct 12 '22 05:10

Jitesh Mohite