AdSense

AdSense3

Wednesday, 22 July 2015

Android Custom Toast Example

You are able to create custom toast in android. So, you can display some images like congratulations or loss on the toast. It means you are able to customize the toast now.


activity_main.xml

Drag the component that you want to display on the main activity.
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_centerVertical="true"  
  12.         android:text="@string/hello_world" />  
  13.   
  14. </RelativeLayout>  

customtoast.xml

Create another xml file inside the layout directory. Here we are having ImageView and TextView in this xml file.
File: customtoast.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.       android:id="@+id/custom_toast_layout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     android:background="#F14E23"  
  8.      >  
  9.        
  10.     <ImageView  
  11.         android:id="@+id/custom_toast_image"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:contentDescription="@string/hello_world"  
  15.         android:src="@drawable/ic_launcher"/>  
  16.       
  17. <TextView  
  18.         android:id="@+id/custom_toast_message"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:contentDescription="@string/Toast"  
  22.         android:text="@string/Toast" />  
  23. </LinearLayout>  

Activity class

Now write the code to display the custom toast.
File: MainActivity.java
  1. package com.example.customtoast2;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Gravity;  
  5. import android.view.LayoutInflater;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.      @Override  
  13.         public void onCreate(Bundle savedInstanceState) {  
  14.             super.onCreate(savedInstanceState);  
  15.             setContentView(R.layout.activity_main);  
  16.               
  17.         //Creating the LayoutInflater instance  
  18.             LayoutInflater li = getLayoutInflater();  
  19.         //Getting the View object as defined in the customtoast.xml file  
  20.             View layout = li.inflate(R.layout.customtoast,  
  21.               (ViewGroup) findViewById(R.id.custom_toast_layout));  
  22.            
  23.         //Creating the Toast object   
  24.             Toast toast = new Toast(getApplicationContext());  
  25.             toast.setDuration(Toast.LENGTH_SHORT);  
  26.             toast.setGravity(Gravity.CENTER_VERTICAL, 00);  
  27.             toast.setView(layout);//setting the view of custom toast layout  
  28.             toast.show();  
  29.         }  
  30.         @Override  
  31.         public boolean onCreateOptionsMenu(Menu menu) {  
  32.             getMenuInflater().inflate(R.menu.activity_main, menu);  
  33.             return true;  
  34.         }  
  35.   
  36. }   

No comments:

Post a Comment