Lesson for Android Programming Beginners :
Step 1 : Create Button
In your layout.xml file create a button view and set the attribute : android:onClick = "displayToastMsg"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.toastexample.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="displayToastMsg"</b>
android:text="Display Toast Message!" />
</RelativeLayout>
Step 2 : Create Toast Method
Now let's create a method that displays a Toast message when the button is being clicked. Let the method takes in one parameter i.e. the Message to be displayed as String.
public void toastMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
}
Step 3 : Create Button Click Listener Method
Now let's implement Button click Listener that will be called when the button is clicked. We call the toastMsg() function to display the Toast Message.
public void displayToastMsg(View v) {
toastMsg("Hello how are you today!!");
}
Step 4 : Run the App :

✋️ Now every time the button will be clicked this toast message will be displayed.
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- How to come out of dquote prompt in Terminal - macOS/Linux - MacOS
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode - Java
- Android : How to make TextView Scrollable - Android
- zsh: command not found [fix] macOS - zsh
- 403 forbidden error for Image - PHP
- Python f-strings Formatted String Literals Syntax and Examples - Python
- Encode or Decode Base64 String using Mac Terminal Command - MacOS
- How to disable Siri on Mac Monterey - MacOS