I am getting error while creating a Toast
Toast toast = Toast.makeText(this, text, duration);
I am getting cannot resolve ma
Just like Blackbelt said the syntax of Toast is as follows:
Toast.makeText(Activity.this, "Message",Toast.<specify Lenght>).show();
where Activity.this
is the current activity, Message
is the string you want to show and Toast.length
is the length you want it to show it for.
In case of a Toast in a fragment inside a Tabbed Activity, use getContext() e.g.
Toast.makeText(getContext(), "Your Text Here", Toast.LENGTH_SHORT).show();
First add
import android.widget.Toast;
statement if you did not already and then
Toast.makeText(YourActvityName.this, "Your Text", Toast.LENGTH_SHORT).show();
The makeText's signature is the following
public static Toast makeText (Context context, CharSequence text, int duration)
the first paramter has to be a context object. You put this
, but this
refers to this object and it can be something different from an Activity
(a Fragment
for instance).
Make sure that you type:
Toast toast = Toast.makeText(this, text, duration);
Not:
Toast toast = new Toast.makeText(this, text, duration);
Toast.makeText(YourActivity.this, text, duration).show();