CustomToast in Android

Introduction

 
This article explains CustomToasts in Android. Android Studio is used to create the sample. For this, you will use LayoutInflator to inflate the layout in another layout. For this, you will create the object of LayoutInflator by calling getLayoutInflator() provided by the LayoutInflator. Now get the Layout into the view by calling the inflate method of LayoutInflator.
 
You will use two XML files, one to hold the layout and another that will be held.
  • getLayoutInflator(): gets the layout object
  • inflate(): inflates another layout  
Step 1
 
Create a project like this:
 
ProjectCratetion
 
Step 2
 
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="Custom Toast"  
  15.         android:textStyle="bold"  
  16.         android:textSize="25dp"  
  17.         android:textColor="#f14e23"  
  18.         android:layout_centerHorizontal="true"  
  19.         />  
  20.    
  21. </RelativeLayout> 
Step 3
 
Create another XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:id="@+id/custom_Toast"  
  7.     android:background="#f14e23">  
  8.    
  9.     <ImageView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/imageView"  
  13.         android:layout_gravity="center"  
  14.         android:background="@drawable/ic_launcher"/>  
  15.    
  16.     <TextView  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="Custom Toast Example"  
  20.         android:id="@+id/textView"  
  21.         android:layout_gravity="center" />  
  22. </LinearLayout> 
Step 4
 
Create a Java class file with the following code.
 
For this, you will use LayoutInflator to inflate the layout in another layout. For this you will create an object of LayoutInflator by calling getLayoutInflator() provided by the LayoutInflator. Now get the Layout into the view by calling the inflate method of LayoutInflator.
  1. package com.customtoast;  
  2. import android.content.Context;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.Toast;  
  11.    
  12. public class MainActivity extends Activity {  
  13.    
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         LayoutInflater layoutInflater=getLayoutInflater();  
  19.         View layout=layoutInflater.inflate(R.layout.second,(ViewGroup)findViewById(R.id.custom_Toast));  
  20.         Toast t=new Toast(getApplicationContext());  
  21.         t.setDuration(Toast.LENGTH_LONG);  
  22.         t.setGravity(Gravity.CENTER_VERTICAL,0,0);  
  23.         t.setView(layout);  
  24.         t.show();  
  25.     }  
  26.    
  27.     @Override  
  28.     public boolean onCreateOptionsMenu(Menu menu) {  
  29.         // Inflate the menu; this adds items to the action bar if it is present.  
  30.         getMenuInflater().inflate(R.menu.main, menu);  
  31.         return true;  
  32.     }  
Step 5
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.customtoast"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.customtoast.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 6
 
The following is a Custom Toast containing another Layout:
 
customToast


Similar Articles