Let's see a simple example to display a Toast Message in Android Programming. We have an Activity class called MainActivity.java and the corresponding layout XML file called activity_main.xml with a Button, on click of the button we display the Toast Message!

Display Toast on Button Click
activity_main.xml
<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.code2care.toast.MainActivity" >
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="displayToast"
android:text="Display Toast" />
</RelativeLayout>
MainActivity.java
package com.code2care.toast;
/**
*
* This is a simple example to display a
* toast message in Android when a button
* is being pressed.
*
*/
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//Display toast on button click
public void displayToast(View view) {
Toast.makeText(MainActivity.this, "Hello Toast Message!", Toast.LENGTH_LONG).show();
}
}
More Posts related to Android,
- 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
More Posts:
- iOS Fix: This app is no longer shared with you. To use it, you must buy it from the App Store. - Apple
- JavaScript date in yyyy-MM-dd format - JavaScript
- JavaScript : redirect page to other url - Android
- Maven Eclipse (M2e) No archetypes currently available - Java
- Error : Invalid key hash.The key hash does not match any stored key hashes - Android
- How to turn off Facebook autoplay videos on timeline - Facebook
- Center align text in TextView Android Programming - Android
- How to perform Find and Replace using vim Editor - vi