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”.
Select your target Android device and click on next.
In the next tab, select the Base activity.
Lastly, customize the activity.
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.
Want To Create An Android Application?
Validate your app idea and get a free quote.
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.
In case, if you still have any query or confusion regarding double toasted SoundCloud, how long does it take to make an app or how to make money with an app idea, then you can get in touch with us through our contact us form. One of our sales representatives will revert to you as soon as possible. The consultation is absolutely free of cost.
You may also like,
This page was last edited on December 28th, 2020, at 8:24.