Introduction
This article shows how to add a CustomToast in Android.
Procedure
- Start Eclipse IDE.
- Create a new project.
- Create a MainActivity.java file.
- Create three XML files, for example, activity_main.xml, customtoastt.xml, and hhh.xml for layout design.
- Add Textview fields in activity_main.xml, textview, and imageview in the customtoastt.xml layout.
- In onTouch function set the code of the custom toast.
The following is the code.
MainActivity.java
- package com.example.customtoasttodayyyy;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Color;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnTouchListener {
- TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView) findViewById(R.id.textView2);
- tv.setOnTouchListener(this);
- }
- public boolean onTouch(View v, MotionEvent event) {
- Toast t1 = new Toast(this);
- LayoutInflater li = getLayoutInflater();
- View vv = li.inflate(R.layout.customtoastt, (ViewGroup) findViewById(R.id.aa));
- t1.setGravity(Gravity.TOP, 0, 0);
- t1.setDuration(Toast.LENGTH_LONG);
- t1.setView(vv);
- t1.show();
- return false;
- }
- }
activity_main.xml
- <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" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Good Morning Guyzz click on custom toast" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/textView1"
- android:layout_marginLeft="41dp"
- android:layout_marginTop="102dp"
- android:layout_toRightOf="@+id/textView1"
- android:text="CustomToast"
- android:textAppearance="?android:attr/textAppearanceLarge"/>
- </RelativeLayout>
customtoastt.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/aa"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="200px"
- android:background="@drawable/desert" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello Abhijeet "
- android:textColor="#ff00cc"
- android:textSize="40px"
- android:textStyle="bold" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- </LinearLayout>



Comments
Join the conversation! Your thoughts help the community grow.