Adding CustomToast in Android

Introduction

 
This article shows how to add a CustomToast in Android.
 
Procedure
  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create three XML files, for example, activity_main.xml, customtoastt.xml, and hhh.xml for layout design.
  5. Add Textview fields in activity_main.xml, textview, and imageview in the customtoastt.xml layout.
  6. In onTouch function set the code of the custom toast.
The following is the code.
 
MainActivity.java
  1. package com.example.customtoasttodayyyy;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.Menu;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10. import android.view.View.OnTouchListener;  
  11. import android.view.ViewGroup;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15. public class MainActivity extends Activity implements OnTouchListener {  
  16.  TextView tv;  
  17.  @Override  
  18.  protected void onCreate(Bundle savedInstanceState) {  
  19.   super.onCreate(savedInstanceState);  
  20.   setContentView(R.layout.activity_main);  
  21.   tv = (TextView) findViewById(R.id.textView2);  
  22.   tv.setOnTouchListener(this);  
  23.  }  
  24.  public boolean onTouch(View v, MotionEvent event) {  
  25.   Toast t1 = new Toast(this);  
  26.   LayoutInflater li = getLayoutInflater();  
  27.   View vv = li.inflate(R.layout.customtoastt, (ViewGroup) findViewById(R.id.aa));  
  28.   t1.setGravity(Gravity.TOP, 00);  
  29.   t1.setDuration(Toast.LENGTH_LONG);  
  30.   t1.setView(vv);  
  31.   t1.show();  
  32.   return false;  
  33.  }  
activity_main.xml
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.       
  12.     <TextView  
  13.         android:id="@+id/textView1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Good Morning Guyzz click on custom toast" />  
  17.           
  18.     <TextView  
  19.         android:id="@+id/textView2"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_below="@+id/textView1"  
  23.         android:layout_marginLeft="41dp"  
  24.         android:layout_marginTop="102dp"  
  25.         android:layout_toRightOf="@+id/textView1"  
  26.         android:text="CustomToast"  
  27.         android:textAppearance="?android:attr/textAppearanceLarge"/>  
  28.           
  29. </RelativeLayout>  
customtoastt.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:id="@+id/aa"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:gravity="center"  
  9.     android:orientation="vertical" >  
  10.       
  11.     <ImageView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="200px"  
  14.         android:background="@drawable/desert" />  
  15.           
  16.     <TextView  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="Hello Abhijeet "  
  20.         android:textColor="#ff00cc"  
  21.         android:textSize="40px"  
  22.         android:textStyle="bold" />  
  23.           
  24. </LinearLayout> 
hhh.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8. </LinearLayout> 
Output
 
 


Similar Articles