android-studio
  1. android-studio-custom-toast

Android Studio Custom Toast

Toast is a small pop-up message that appears on the screen for a short amount of time. We can easily create Toast messages in Android by using predefined methods. However, we can also create custom Toast messages by designing our own layout.

Syntax

LayoutInflater inflater = getLayoutInflater();  
View layout = inflater.inflate(R.layout.custom_toast_layout,  
    (ViewGroup) findViewById(R.id.toast_layout_root));  

TextView text = (TextView) layout.findViewById(R.id.text);  
text.setText("Custom Toast Message");  

Toast toast = new Toast(getApplicationContext());  
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  
toast.setDuration(Toast.LENGTH_LONG);  
toast.setView(layout);
toast.show();  

Example

In this example, we will create a custom Toast message with a background color, image, and text.

First, we need to create a custom layout for our Toast message. We can do this by creating an XML file named custom_toast_layout.xml in the res/layout folder. Here is the code for the layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/toast_layout_root"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:background="#80000000"  
    android:orientation="horizontal"  
    android:padding="8dp">  

    <ImageView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:src="@drawable/ic_launcher" />  

    <TextView  
        android:id="@+id/text"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textColor="#fff"  
        android:textSize="16sp"  
        android:text="Custom Toast Message" />  

</LinearLayout>

Next, we need to use the code above to call the custom layout and create the Toast message in our MainActivity.java file.

public void showToast(View view) {  
    LayoutInflater inflater = getLayoutInflater();  
    View layout = inflater.inflate(R.layout.custom_toast_layout,  
        (ViewGroup) findViewById(R.id.toast_layout_root));  

    TextView text = (TextView) layout.findViewById(R.id.text);  
    text.setText("Custom Toast Message");  

    Toast toast = new Toast(getApplicationContext());  
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  
    toast.setDuration(Toast.LENGTH_LONG);  
    toast.setView(layout);
    toast.show();  
}

Output

Custom Toast Message Screenshot

As we can see, the custom Toast message appears on the screen with an image, background color, and text.

Explanation

In the example above, we created a LinearLayout with an ImageView and TextView inside it. The ImageView displays an app icon and the TextView displays the message that we want to show in the Toast.

We use the getLayoutInflater() method to create an instance of LayoutInflater and inflate our custom layout file. We then get the TextView by finding it in our custom layout using findViewById(). We set the text of our TextView using setText().

Next, we create an instance of Toast and set the gravity of the Toast using the setGravity() method. We then set the duration of the Toast using the setDuration() method. After that, we set the layout of our Toast using the setView() method and pass it the layout we just inflated in the first step. Finally, we show the custom Toast message using the show() method.

Use

Custom Toast messages can be used in any Android application where a small pop-up message is needed. We can use them to provide feedback to the user, show error messages, or display other important information.

Important Points

  • Custom Toast messages allow us to create our own layouts and display them as Toast messages.
  • The layout file for a custom Toast message should be created in the res/layout folder.
  • We need to use the getLayoutInflater() method to inflate the custom layout file.
  • The code for creating and showing custom Toast messages should be placed in the Activity where the message will be displayed.
  • We can set the duration and gravity of a Toast message using the setDuration() and setGravity() methods.

Summary

In this tutorial, we learned how to create and display custom Toast messages in Android. We created a custom layout file and used it to create a Toast message with an image, background color, and text. We learned the syntax for creating custom Toast messages and discussed the important points to keep in mind when using them in an Android application. Custom Toast messages are a useful feature of Android that allow us to provide feedback to users in a quick and easy way.

Published on: