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!

<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();
}
}
- 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 list all tables using Java JDBC - Java
- Installing and using unzip Command to unzip a zip file using Terminal - Linux
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- How to check your installed version of Git - Git
- How to open terminal on Mac to run commands - MacOS
- Fix Microsoft Teams Admin Center error - The Security zone setting isn't configured correctly - Teams
- How to change Chrome Spell Check from UK English to US English - Chrome
- Fix: TypeError: can only concatenate str (not int) to str in Python - Python