How to Display Custom Toast Message in Your App Using Android Toast

0
Shares
How to Display Custom Toast Message in Your App Using Android Toast
3 (60%) 2 votes

What comes to your mind when you want to tell something to the user quickly?

Toast Message? Yes, they are indeed a quick way of telling the user something. A toast message is a short pop-up message that appears for one or two seconds and then fades away.

Basically, a toast is displayed in front of the activity, and Android toast is displayed at the bottom of the screen. Though, it’s also possible to change the display position with Android toast while developing your own Android app.

Today, in our Android application development tutorial, we’ll build an Android app to understand the custom Android toast implementation process through a simple Android toaster app.

Let’s Get Started

Open your Android Studio and create a new project “CustomAndroidToastDemo”.

s1

Select your target Android device and click on next.

s2

In the next tab, select the Base activity.

s3

Lastly, customize the activity.

s4

Now, create an XML layout file to display custom toast with message and icon.

Content_Custom_Toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llCustom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/colorAccent"
android:drawableLeft="@android:drawable/ic_dialog_alert"
android:gravity="center"
android:padding="10dp"
android:drawablePadding="5dp"
android:text="This is custom toast message"
android:textColor="@android:color/white"
android:textSize="16sp" />

</LinearLayout>

Modify the XML activity content layout file. Here, we’ll add two buttons. One for default layout and second for custom layout.

Content_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.customandroidtoastdemo.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/btnDefaultToast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="Show Default Toast"
android:textAllCaps="false" />

<Button
android:id="@+id/btnCustomToast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="Show Custom Toast"
android:textAllCaps="false" />
</LinearLayout>

Now, Initialize the button in activity and button click handling.

Declare button variables on top of Activity class

private Button btnDefaultToast, btnCustomToast;

Initialize both buttons.

btnDefaultToast = (Button) findViewById(R.id.btnDefaultToast);
btnCustomToast = (Button) findViewById(R.id.btnCustomToast);

Handle button click.

@Override
public void onClick(View view) {
if (view.equals(btnDefaultToast)) {
Toast.makeText(MainActivity.this, "This is default toast message", Toast.LENGTH_SHORT).show();
} else if (view.equals(btnCustomToast)) {
LayoutInflater inflater = getLayoutInflater();
View toastLayout = inflater.inflate(R.layout.content_custom_toast, (ViewGroup) findViewById(R.id.llCustom));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
}
}

And Done!

So, now that you know the complete implementation process for Android toast message, go ahead and implement in your Android app.

Android toast is the solution for mobile developers when they want to notify users quickly, but in case you’re building toaster app for your startup, then hiring Android app developer would be a wise choice as you’ll have the help from person who has the experience of developing Android apps of different types, and may give you feature-suggestions based on your idea.

This page was last edited on October 3rd, 2018, at 7:07.
 
0
Shares
 

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now