Android Toast

Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime. The android.widget.Toast class is the subclass of java.lang.Object class. You can also create custom toast as well for example toast displaying image.

Create a New Android Project

Open Android Studio and in Right Panel select \"Start a new Android Studio Project\"



Toast class is used to show notification for a particular interval of time. After sometime it disappears. It doesn't block the user interaction.

Your Activity Class (Java File) stored in Src->Your package Name->mainActivity.java
Open activity_main.xml from android resources and remove everything.Drag an Relative Layout and then drag a TextView on Layout in Graphical mode of IDE.
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="dzone.jaipur.myapp1.MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="63dp"
        android:layout_marginTop="52dp"
        android:text="Hello Dzone" />
    </RelativeLayout>

Now Write the code to show a Toast Message in java File (Android Activity) .


public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);


		//Displaying Toast with Hello Dzone message  
            Toast.makeText(getApplicationContext(),"Hello Dzone",Toast.LENGTH_LONG).show();  
	}

}


Now Run the Application by upper toolbar green triangle then select your device .As App is started you will get an "Hello Dzone" flash message on the screen.