Wednesday, April 18, 2012

Custom Toast

custom_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/picture_frame"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="5dp"
        android:src="@drawable/inbox" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:text="Toast text here"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>


main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#454545"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click here to see custom Toast" />

</RelativeLayout>

In java File


Button mButton1 = (Button) findViewById(R.id.button1);
  mButton1.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast_layout,
      (ViewGroup) findViewById(R.id.root));

    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Message Sent Succesfully !!");

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.BOTTOM, 10, 80);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
   }
  });

Multiple Toast 



Toast m_currentToast;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button mButton = (Button) findViewById(R.id.button);
  mButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    showToast("5");
    showToast("4");
    showToast("3");
    showToast("2");
    showToast("1");
    showToast("Go go go...");

   }
  });

 }

 void showToast(String text) {
  if (m_currentToast != null) {
   m_currentToast.cancel();
  }
  LayoutInflater inflater = getLayoutInflater();
  View layout = inflater.inflate(R.layout.custom_toast_layout,
    (ViewGroup) findViewById(R.id.root));
  TextView text1 = (TextView) layout.findViewById(R.id.text);
  text1.setText(text);

  m_currentToast = new Toast(getApplicationContext());
  m_currentToast.setDuration(Toast.LENGTH_SHORT);
  m_currentToast.setView(layout);
  m_currentToast.setGravity(Gravity.TOP, 10, 180);
  m_currentToast.show();

 }