Toast & Custom Toast With Example in Android Studio: EX - NO: 3
Toast & Custom Toast With Example in Android Studio: EX - NO: 3
NO: 3
Special Note: In Android, Toast is used when we required to notify user about an operation
without expecting any user input. It displays a small popup for message and automatically fades
out after timeout.
Let’s we discuss some important methods of Toast that may be called in order to manage the
Toast.
Constants of Toast: Below is the constants of Toast that are used for setting the duration for the
Toast.
1. LENGTH_LONG: It is used to display the Toast for a long period of time. When we set this
duration the Toast will be displayed for a long duration.
2. LENGTH_SHORT: It is used to display the Toast for short period of time. When we set this
duration the Toast will be displayed for short duration.
Below we show the use of makeText() method of Toast in which we set application context, a
text message and duration for the Toast.
2. show(): This method is used to display the Toast on the screen. This method is display the text
which we create using makeText() method of Toast.
Below we Firstly initiate the Toast and then display it using show() method.
3. setGravity(int,int,int): This method is used to set the gravity for the Toast. This method
accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
Below we Firstly initiate the Toast, set top and left gravity and then display it using show()
method.
4. setText(CharSequence s): This method is used to set the text for the Toast. If we use
makeText() method and then we want to change the text value for the Toast then we use this
method.
Below we firstly create a new Toast using makeText() method and then set the text for the Toast.
Below we firstly create a new Toast using makeText() method and then set the duration for the
Toast.
6. inflate(int, ViewGroup): This method is used to inflate the layout from the xml. In this
method first parameter is the layout resource ID and the second is the root View.
Below we retrieve the Layout Inflater and then inflate the layout from the xml file.
// Retrieve the Layout Inflater and inflate the layout from xml
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
7. setView(View): This method is used to set the view for the Toast. In this method we pass the
inflated layout which we inflate using inflate() method.
Below we firstly retrieve the layout inflater and then inflate the layout and finally create a new
Toast and pass the inflated layout in the setView() method.
// Retrieve the Layout Inflater and inflate the layout from xml
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
In Android, Sometimes simple Toast may not be satisfactory, and then we can go for
customizing a Toast. For creating a custom layout, define a View layout, in XML and pass the
root View object to the setView(View) method.
Step 2: Create a new Toast with Toast(Context) and set some properties of the Toast, such as the
duration and gravity.
Step 3: Call setView(View) and pass the inflated layout in this method.
Step 4: Display the Toast on the screen using show() method of Toast.
In the below example we have shown the functioning of Toast and custom Toast both.
Below is the example of Toast and Custom Toast in Android. In this example we display two
Button’s one for Simple Toast and other for Custom Toast and perform click event on them.
Whenever a user click on simple Toast Button a Toast with message “Simple Toast In Android”
displayed on the screen and when a user clicks on custom toast Button a message “Custom Toast
In Android” with a image displayed on the screen. For Creating a custom toast we firstly retrieve
the layout inflater and then inflate the custom toast layout from the xml file. After that we get the
reference of TextView and ImageView from the inflated layout and set the text and image in the
TextView and ImageView. Finally we create a new Toast and pass the inflated layout in the
setView() method and then display the Toast by using show() method of Toast.
Below is the final output, download Android Studio code and step by step explanation of the
example:
Step 2: Open res -> layout ->activity_main.xml (or) main.xml and add following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- Button's for simple and custom Toast -->
<Button
android:id="@+id/simpleToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#f00"
android:text="Simple Toast"
android:textColor="#fff"
android:textSize="20sp" />
<Button
android:id="@+id/customToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleToast"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:background="#0f0"
android:text="Custom Toast"
android:textColor="#fff"
android:textSize="20sp" />
</RelativeLayout>
Step 3: Now create a xml layouts by right clicking on res/layout -> New -> Layout
Resource File and name it custom_toast_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DAAA"
android:orientation="horizontal"
android:padding="8dp">
<!-- ImageVView and TextView for custom Toast -->
<ImageView
android:id="@+id/toastImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp" />
<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
In this step we open MainActivity and add the code for initiate the Button’s and perform click
event on Button’s. Whenever a user click on simple Toast Button a Toast with message “Simple
Toast In Android” displayed on the screen and when a user clicks on custom toast Button a
message “Custom Toast In Android” with a image displayed on the screen. For Creating a
custom toast we firstly retrieve the layout inflater and then inflate the custom toast layout from
the xml file. After that we get the reference of TextView and ImageView from the inflated layout
and set the text and image in the TextView and ImageView. Finally we create a new Toast and
pass the inflated layout in the setView() method and then display the Toast by using show()
method of Toast.
package com.abhiandroid.toastexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.view.ViewGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of Button's
simpleToast = (Button) findViewById(R.id.simpleToast);
customToast = (Button) findViewById(R.id.customToast);
// perform setOnClickListener event on simple Toast Button
simpleToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initiate a Toast with message and duration
Toast toast = Toast.makeText(getApplicationContext(), "Simple
Toast In Android", Toast.LENGTH_LONG); // initiate the Toast with context,
message and duration for the Toast
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
0, 0); // set gravity for the Toast.
toast.show(); // display the Toast
}
});
// perform setOnClickListener event on custom Toast Button
customToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Retrieve the Layout Inflater and inflate the layout from
xml
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
// get the reference of TextView and ImageVIew from inflated
layout
TextView toastTextView = (TextView)
layout.findViewById(R.id.toastTextView);
ImageView toastImageView = (ImageView)
layout.findViewById(R.id.toastImageView);
// set the text in the TextView
toastTextView.setText("Custom Toast In Android");
// set the Image in the ImageView
toastImageView.setImageResource(R.drawable.ic_launcher);
// create a new Toast using context
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG); // set the duration for
the Toast
toast.setView(layout); // set the inflated layout
toast.show(); // display the custom Toast
}
});
}
}