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,
- Share or Send SMS via Android Intent
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Fixed Length Android Edittext
- Error:The SDK Build Tools revision (XX.X.X) is too low for project. Minimum required is XX.X.X
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Pass data between two Android Activities and access it using Intent
- AlertDialog with no buttons (just text message)
- Android Eclipse This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in
- Can't Run SDK Manager find_java.bat issue
- Android Studio Native typeface cannot be made error
More Posts:
- SharePoint Server 2016 IT Preview Deprecated Removed features - SharePoint
- How to make jsfiddle bootstrap ready - CSS
- How to enable line numbers in Eclipse - Eclipse
- Parsing Data for android-21 failed unsupported major.minor version 51.0 - Android
- SDK Manager: failed to install : Android Studio on Windows - Android-Studio
- Remove ActionBar from Activity that extends appcompat-v7 - Android
- Loading previous page using html button using JavaScript - JavaScript
- Tutorial : Simple Lightweight Pure CSS based Vertical Navigation Menu - CSS
- Calculate Area and Circumference of Circle - C-Program
- Gmail Error Bad Request Your client has issued a malformed or illegal request - Google
- How to Apply Themes to Notepad++ - NotepadPlusPlus
- [Solved] SharePoint Access Denied error editing Document Name - SharePoint
- Calculate Volume of Cube - C-Program
- The service instance "Microsoft SharePoint Insights" could not be provisioned because of the following error: Authentication information for SPInsights service has not been set yet - SharePoint
- Use Netbeans keyboard shortcuts in Android Studio - Android-Studio