Android Toast (From the Activity as well as from outside the Activity)

  • If you want to add a Toast inside your Activity code then use this
       //Displays Toast for a short period of time
        Toast.makeText(getApplicationContext(),”Message you want to display”, Toast.LENGTH_SHORT).show();
        //Displays Toast for a longer period of time
        Toast.makeText(getApplicationContext(),”Message you want to display”, Toast.LENGTH_LONG).show();
  • There might be cases when you want to reuse your code for Toast notifications and want the code in a separate file that is called by different properties.
            In this case get this toast code in the separate file
            public static void toasty(Context con, String msg)

{

 

Toast.makeText(con, msg, Toast.LENGTH_LONG).show();

}

Call this method from your activities to reuse the code. Here AppUtilities is the class name where our                                                                                                                    method is written.

AppUtilies.toasty(getApplicationContext(), “Please enter an email and password”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s