Toast in Android

In this lesson, 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 show message like "Signup Successfully" or "User Name Already Taken" after any user will press 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 out 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.

Toast in Android


Toast Example
<?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 press this Button in this Activity.


Toast Example

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 thats why I called that static method from "Toast" class. This makeText method get three parameters.
  • First Parameter : First parameter of makeText method is context. So to give context I added here MainActivity.this.
  • Second Parameter : Second parameter is your message. The message which you want to show in your Toast. i added here "Learn Android with Owlbuddy".
  • Thirst Parameter : 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.


Toast in Android


I hope so now you would have a clear idea 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 tutorial i would keep "main_activity.xml" file 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 Layout
<?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>




Main Java File

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();

            }
        });
    }
}



Custom Text view in Android


Share this tutorial with your friends on your social Media..

Toast in Android Toast in Android Toast in Android Toast in Android



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com