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



Join the conversation! Your thoughts help the community grow.