What is Toast?
A toast is a view containing a quick little message for the user. When the view is shown to the user, appears as a floating view over the application. It will never receive focus. 
Normal syntax of Toast is
Toast.makeText(context, text, duration).show();
context the context to use. Usually your android.app.Application or android.app.Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGHT_LONG.
Here is the example: Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
Now let's start with custom toast.
Create an Android Project:
CustomToastActivity.java
public class CustomToastActivity extends ActionBarActivity implements OnClickListener {
 Button customToast;
 Button showToast;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_custom_toast);
  showToast=(Button)findViewById(R.id.button1);
  showToast.setOnClickListener(this);
  customToast=(Button)findViewById(R.id.button2);
  customToast.setOnClickListener(this);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.custom_toast, menu);
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  return super.onOptionsItemSelected(item);
 }
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.button1:
   Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
   break;
  case R.id.button2:
   LayoutInflater inflater=getLayoutInflater();
   View layout=inflater.inflate(R.layout.custom_toast, (ViewGroup)findViewById(R.id.RelativeLayout1));
   Toast toast=new Toast(getApplicationContext());
   toast.setGravity(Gravity.BOTTOM, 0, 0);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
   break;
  }
 }
}
activity_custom_toast.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sachinsandbhor.customtoastdemo.CustomToastActivity"
    tools:ignore="MergeRootFrame" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="Toast" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Custom Toast" />
</RelativeLayout>
custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/abc_ic_voice_search_api_holo_light" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/imageView1"
        android:gravity="center"
        android:text="That's great you did it..." />
</RelativeLayout>
ScreenShots:
Here is the sample Code


Comments
Post a Comment