How to display Toast in Android?
In order to display Toast in your application, try this:
Toast.makeText(getActivity(), (String)data.result, Toast.LENGTH_LONG).show();
Another example:
Toast.makeText(getActivity(), "This is my Toast message!", Toast.LENGTH_LONG).show();
We can define two constants for duration:
int LENGTH_LONG Show the view or text notification for a long period of time.
int LENGTH_SHORT Show the view or text notification for a short period of time.
Customizing your toast
LayoutInflater myInflater = LayoutInflater.from(this);View view = myInflater.inflate(R.layout.your_custom_layout, null);Toast mytoast = new Toast(this);mytoast.setView(view);mytoast.setDuration(Toast.LENGTH_LONG);mytoast.show();
Extending activity using baseadapter
used this
Toast.makeText(getActivity(), "Your Message", Toast.LENGTH_LONG).show();
or if you are using activity or mainactivity
Toast.makeText(MainActivity.this, "Your Message", Toast.LENGTH_LONG).show();
Syntax
Toast.makeText(context, text, duration);
Parameter Value
context
getApplicationContext()
- Returns the context for all activities running in application.
getBaseContext()
- If you want to access Context from another context within application you can access.
getContext()
- Returns the context view only current running activity.
text
text
- Return "STRING" , If not string you can use type cast.
(string)num // type caste
duration
Toast.LENGTH_SHORT
- Toast delay 2000 ms predefined
Toast.LENGTH_LONG
- Toast delay 3500 ms predefined
milisecond
- Toast delay user defined miliseconds (eg. 4000)
Example.1
Toast.makeText(getApplicationContext(), "STRING MESSAGE", Toast.LENGTH_LONG).show();
Example.2
Toast.makeText(getApplicationContext(), "STRING MESSAGE", 5000).show();