Pages

Friday 14 March 2014

Android: Custom Toast Messages.

In last tutorial we saw how to use Toast Messages, and now we will see how to customize Toast messages.
Output will be as:
Android Custom Toast

Xml file:
res/layout
1. activity_main.xml
 <LinearLayout 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:orientation="vertical"  
    >  
    <Button   
      android:id="@+id/button1"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="Android Toast"  
      />  
   </LinearLayout>  
2. toast.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/toast_layout"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="horizontal" >  
    <ImageView android:src="@drawable/rating_good"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         />  
   <TextView android:id="@+id/tv1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        />  
 </LinearLayout>  
Here we have defined a layout for our custom Toast. Also don't forget to add an image in your drawable folder.(res/drawable).

Java file:
src/
 package com.example.toastexample;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.Button;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           b1=(Button) findViewById(R.id.button1);  
           b1.setOnClickListener(this);  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){  
           case R.id.button1:  
                LayoutInflater inflater = getLayoutInflater();  
                View layout = inflater.inflate(R.layout.toast,  
                                (ViewGroup) findViewById(R.id.toast_layout));//referencing toast layout  
                TextView text1 = (TextView) layout.findViewById(R.id.tv1);  
                text1.setText("Well Done");  
                Toast toast = new Toast(getApplicationContext());  
                toast.setDuration(Toast.LENGTH_LONG);  
                toast.setView(layout);  
                toast.show();  
                break;  
           }  
      }  
 }  
Here we have referenced our custom toast layout(toast.xml) and added that layout on our toast message.

No comments:

Post a Comment