Tutorial by Examples

A standard toast notification appears at the bottom of the screen aligned in horizontal centre. You can change this position with the setGravity(int, int, int). This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset. For example, if you decide that the toas...
In Android, a Toast is a simple UI element that can be used to give contextual feedback to a user. To display a simple Toast message, we can do the following. // Declare the parameters to use for the Toast Context context = getApplicationContext(); // in an Activity, you may also use "th...
If you don't want to use the default Toast view, you can provide your own using the setView(View) method on a Toast object. First, create the XML layout you would like to use in your Toast. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="...
public class MainApplication extends Application { private static Context context; //application context private Handler mainThreadHandler; private Toast toast; public Handler getMainThreadHandler() { if (mainThreadHandler == null) { mainThreadHand...
By default, Android will display Toast messages at the bottom of the screen even if the keyboard is showing. This will show a Toast message just above the keyboard. public void showMessage(final String message, final int length) { View root = findViewById(android.R.id.content); Toast toas...
If you don't want to extend Application and keep your toast messages thread safe, make sure you show them in the post execute section of your AsyncTasks. public class MyAsyncTask extends AsyncTask <Void, Void, Void> { @Override protected Void doInBackground(Void... params) { ...

Page 1 of 1