How to display Toast in Android?

1,151,570

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

To toast in Android

Toast.makeText(MainActivity.this, "YOUR MESSAGE", LENGTH_SHORT).show();

or

Toast.makeText(MainActivity.this, "YOUR MESSAGE", LENGTH_LONG).show();

( LENGTH_SHORT and LENGTH_LONG are acting as boolean flags - which means you cant sent toast timer to miliseconds, but you need to use either of those 2 options )

Solution 5

You can customize your tost:

LayoutInflater mInflater=LayoutInflater.from(this);

View view=mInflater.inflate(R.layout.your_layout_file,null);
Toast toast=new Toast(this);
toast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();

Or General way:

Toast.makeText(context,"Your message.", Toast.LENGTH_LONG).show();
Share:
1,151,570
DarkLeafyGreen
Author by

DarkLeafyGreen

Tackling Complexity in the Heart of Software

Updated on July 08, 2022

Comments

  • DarkLeafyGreen
    DarkLeafyGreen almost 2 years

    I have a slider that can be pulled up and then it shows a map. I can move the slider up and down to hide or show the map. When the map is on front, I can handle touch events on that map. Everytime I touch, a AsyncTask is fired up, it downloads some data and makes a Toast that displays the data. Although I start the task on touch event no toast is displayed, not till I close the slider. When the slider is closed and the map is not displayed anymore the Toast appears.

    Any ideas?

    Well start the task

    EDIT:

    public boolean onTouchEvent(MotionEvent event, MapView mapView){ 
        if (event.getAction() == 1) {
            new TestTask(this).execute();
            return true;            
        }else{
            return false;
        }
     }
    

    and in onPostExecute make a toast

    Toast.makeText(app.getBaseContext(),(String)data.result, 
                    Toast.LENGTH_SHORT).show();
    

    In new TestTask(this), this is a reference to MapOverlay and not to MapActivity, so this was the problem.

  • Jorgesys
    Jorgesys over 10 years
    Read the documentation, you will only choose either Toast.LENGTH_SHORT or Toast.LENGTH_LONG for the message duration.
  • Jaykumar Patel
    Jaykumar Patel about 9 years
    User defined 5000 millisecond toast delay.
  • Yousha Aleayoub
    Yousha Aleayoub over 8 years
    @Elen, you need Activity context for making TOASTS, not Application context.
  • Yousha Aleayoub
    Yousha Aleayoub over 8 years
    Good explain, but you need Activity context for making TOASTS, not Application context.
  • Yousha Aleayoub
    Yousha Aleayoub over 8 years
    Good explain, but you need Activity context for making TOASTS, not Application context.
  • Admin
    Admin over 8 years
    Well, well, a cast such as (string)num has not yet been introduced to Java. Try Integer.toString() or similar.
  • ChrisCM
    ChrisCM over 8 years
    Actually, requiring an Activity context is not correct. HOWEVER, getting toasts to work outside of activities (for example in a system Service) requires access to the main loop. Posting my solution.
  • drorw
    drorw about 8 years
    Right - here are some examples from real projects on GitHub - codota.com/android/methods/android.widget.Toast/show
  • Edward
    Edward about 8 years
    If you're putting it inside an onClick method, replacing getActivity() with getBaseContext() worked for me.
  • Mahesh
    Mahesh over 7 years
    there is no such thing like base adapter or main activity , toast only display by passing parameter app context, message, duration-long/short
  • Jorgesys
    Jorgesys over 7 years
    setting duration is not possible for a toast, only the predefined times, LENGTH_SHORT & LENGTH_LONG.
  • lgunsch
    lgunsch over 7 years
    The android docs specifically mention application context: "This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object."
  • Ellen Spertus
    Ellen Spertus over 7 years
    @PhilipBelgrave-Herbert Unfortunately your link is no longer good.
  • Grand Skunk
    Grand Skunk over 5 years
    @IWannaKnow this is probably late haha but change getActivity() to your activity name
  • Shahab Einabadi
    Shahab Einabadi over 5 years
    Unfortunately this answer does not work with the new version of android studio
  • Shahab Einabadi
    Shahab Einabadi over 5 years
    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();