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 :
![Display Toast Message on Button Click.png Display Toast Message on Button Click.png](https://code2care.org/pages/how-to-display-toast-on-button-click-android/images/Display Toast Message on Button Click.png)
Display Toast Message on Button Click.png
✋️ Now every time the button will be clicked this toast message will be displayed.
More Posts related to Android,
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
More Posts:
- How to know current Ubuntu Linux version via terminal command - Ubuntu
- How to Add Tab in Python - Python
- How to Sort CSV File Data in Notepad++ based on a Column - NotepadPlusPlus
- Read a file and Split using StringTokenizer in Java - Java
- [Python] Fix: TypeError: NoneType object is not subscriptable - Python
- Define an Infinite Number in Python - Python
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken - Git
- Notepad++ display files on tab bar as horizontal instead of vertical - NotepadPlusPlus