What is Toast in android?
Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime.
The android.widget.Toast class is the subclass of java.lang.Object class.
You can also create custom toast as well for example toast displaying image.
Syntax for displaying simple toast.
Toast.makeText(getApplicationContext,"Your message comes here",2000).show();
This is about displaying the simple toast. this message will appear till the 2 second on screen.
Custom Toast
We can create custom toast in android. So, you can display some images like congratulations or some network status etc. It means you are able to customize the toast now.
Steps to create the custom toast in android
1) Create the customtoast.xml file. save it on res/drawable folder.
2) In MainActivity.java.
i) Create the LayoutInflater instance.
ii) Get the view object as defined in the customtoast.xml.
iii) Create the Toast class object.
activitymain.xml
Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime.
The android.widget.Toast class is the subclass of java.lang.Object class.
You can also create custom toast as well for example toast displaying image.
Syntax for displaying simple toast.
Toast.makeText(getApplicationContext,"Your message comes here",2000).show();
This is about displaying the simple toast. this message will appear till the 2 second on screen.
Custom Toast
We can create custom toast in android. So, you can display some images like congratulations or some network status etc. It means you are able to customize the toast now.
Steps to create the custom toast in android
1) Create the customtoast.xml file. save it on res/drawable folder.
2) In MainActivity.java.
i) Create the LayoutInflater instance.
ii) Get the view object as defined in the customtoast.xml.
iii) Create the Toast class object.
activitymain.xml
- <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="@string/hello_world" />
- </RelativeLayout>
customtoast.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
- android:id="@+id/custom_toast_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#FF00FF"
- >
- <ImageView
- android:id="@+id/custom_toast_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/congratulation"/>
- <TextView
- android:id="@+id/custom_toast_message"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="custom toast" />
- </LinearLayout>
Activity class
- package com.baba.customtoastexample;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Creating the LayoutInflater instance
- LayoutInflater li = getLayoutInflater();
- //Getting the View object as defined in the customtoast.xml file
- View layout = li.inflate(R.layout.customtoast,
- (ViewGroup) findViewById(R.id.custom_toast_layout));
- //Creating the Toast object
- Toast toast = new Toast(getApplicationContext());
- toast.setDuration(Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
- toast.setView(layout);//setting the view of custom toast layout
- toast.show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }