Toast in Android

In this tutorial, we would learn about Toast in Android. We use Toast in Android to show small piece of information for small time. For example, we want to show a message like “Signup Successfully” or “User Name Already Taken” after a user will press the signup button. In that kind of situations toast is a best option to give a small message to user. In this tutorial, we will learn how can we show a Toast in our Android App. Toast is class already available in Android. we can use that class to show Toast in our App and We can also create a custom Toast in Android. In this tutorial we will also learn how we can create a custom Toast in Android.

XML Code:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textColor="@color/colorWhite"
        android:background="@color/colorPrimary"
        android:id="@+id/button"
        android:text="Show Toast"/>

</LinearLayout>

In this layout file, I added a button and I will show a Toast whenever someone presses this Button in this Activity.

Java Code:

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast Example
                Toast.makeText(MainActivity.this,"Learn Android with Owlbuddy",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

To create Toast in Android we have a static method “makeText” in “Toast” class. so that’s why I called that static method from “Toast” class. This makeText method gets three parameters.

  • First Parameter: The first parameter of makeText method is context. So to give context I added here MainActivity.this.
  • Second Parameter: The second parameter is your message. The message which you want to show in your Toast. I added here “Learn Android with Owlbuddy”.
  • Thirst Parameter: The third Parameter in makeText is duration mean for how much time you want to show this message. To set duration we have two static variables in Toast class Toast.LENGTH_SHORT and Toast.LENGTH_LONG.

I hope so now you would have a clear idea of how to add a simple Toast in our Android App. Now we will learn how to create a “Custom Toast” in Android. For this rest of the tutorial, I would keep “main_activity.xml” file the same. I will just add a new layout file to give layout to my custom toast and I will also change some code in MainActivity java file to add custom toast.

Custom Toast XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="15dp"
    android:id="@+id/toast_layout"
    android:background="@color/colorPrimary"
    android:layout_gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Toast"
        android:textColor="@color/colorWhite"/>

</LinearLayout>

MainActivity Java Code:

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);

        LayoutInflater layoutInflater=getLayoutInflater();
        final View view=layoutInflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast Example
              Toast toast=new Toast(MainActivity.this);
              toast.setDuration(Toast.LENGTH_SHORT);
              toast.setView(view);
              toast.show();

            }
        });
    }
}

 

Spread the love
Scroll to Top
×