Posts

Showing posts with the label Show Toast on button click Android Studio

Show Toast on button click Android Studio

Image
In this tutorial we will learn how to use Toast in android. We will create a button when this button is clicked the toast will be displayed. Follow the following steps to display the toast.. Step 1:   Create a new project  OR Open your project Step 2: Code to display toast: For Short Time(2 seconds): Toast . makeText ( MainActivity . this , "MESSAGE" , Toast . LENGTH_SHORT ). show (); For Long Time(3.5seconds): Toast . makeText ( MainActivity . this , "This is Toast..." , Toast . LENGTH_LONG ). show (); For Custom Time(e.g 6 seconds): for ( int i = 0 ; i < 3 ; i ++) { Toast . makeText ( MainActivity . this , "MESSAGE" , Toast . LENGTH_SHORT ). show (); } Step 3: To display Toast on Button click: Step 3.1: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:a...