In this tut , I am going to show you how to create custom toast.There are 3 steps to create custom toast.
1.Create a layout file that defines what your toast should display.
2.Create a simple toast using the following constructor.
Toast toast = new Toast(Context c)
Now remember , Don't use Toast.maketext(); because we are creating a custom toast.
Set properties like duration and gravity.
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
Don't show() toast yet.
3.Construct the View from XML that will be used as the Toast's View.
LayoutInflater lin = getLayoutInflater();
View appear = lin.inflate(int(id of XML), ViewGroup);
toast.setView(appear);
toast.show();
Open “res/layout/activity_main.xml” file, add a button.
File : res/layout/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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showCustomtoast"
android:text="Show custom toast" />
</RelativeLayout>
Create a new XML file in res/layout and paste this code.
File : res/layout/custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/root"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/toast_custom" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginTop="34dp"
android:text="Hello from Jaydeep"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Open MainActivity.java file and paste following code.
package com.example.customtoast;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showCustomtoast(View v)
{
// TODO Auto-generated method stub
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_LONG);
t.setGravity(Gravity.BOTTOM, 0, 0);
LayoutInflater inflater = getLayoutInflater();
View appearance = inflater.inflate(R.layout.custom_toast, (ViewGroup)findViewById(R.id.root));
t.setView(appearance);
t.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Screen shot:
Don't forget to comment. Happy coding..:-)
1.Create a layout file that defines what your toast should display.
2.Create a simple toast using the following constructor.
Toast toast = new Toast(Context c)
Now remember , Don't use Toast.maketext(); because we are creating a custom toast.
Set properties like duration and gravity.
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
Don't show() toast yet.
3.Construct the View from XML that will be used as the Toast's View.
LayoutInflater lin = getLayoutInflater();
View appear = lin.inflate(int(id of XML), ViewGroup);
toast.setView(appear);
toast.show();
Open “res/layout/activity_main.xml” file, add a button.
File : res/layout/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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showCustomtoast"
android:text="Show custom toast" />
</RelativeLayout>
Create a new XML file in res/layout and paste this code.
File : res/layout/custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/root"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/toast_custom" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginTop="34dp"
android:text="Hello from Jaydeep"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Open MainActivity.java file and paste following code.
package com.example.customtoast;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showCustomtoast(View v)
{
// TODO Auto-generated method stub
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_LONG);
t.setGravity(Gravity.BOTTOM, 0, 0);
LayoutInflater inflater = getLayoutInflater();
View appearance = inflater.inflate(R.layout.custom_toast, (ViewGroup)findViewById(R.id.root));
t.setView(appearance);
t.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Screen shot:
Don't forget to comment. Happy coding..:-)


