Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Custom Toast


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.     <TextView
  7.         android:layout_width=“wrap_content”
  8.         android:layout_height=“wrap_content”
  9.         android:layout_centerHorizontal=“true”
  10.         android:layout_centerVertical=“true”
  11.         android:text=“@string/hello_world” />
  12. </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.     <ImageView
  10.         android:id=“@+id/custom_toast_image”
  11.         android:layout_width=“wrap_content”
  12.         android:layout_height=“wrap_content”
  13.         android:contentDescription=“@string/hello_world”
  14.         android:src=“@drawable/ic_launcher”/>
  15. <TextView
  16.         android:id=“@+id/custom_toast_message”
  17.         android:layout_width=“wrap_content”
  18.         android:layout_height=“wrap_content”
  19.         android:contentDescription=“@string/Toast”
  20.         android:text=“@string/Toast” />
  21. </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. public class MainActivity extends Activity {
  11.      @Override
  12.         public void onCreate(Bundle savedInstanceState) {
  13.             super.onCreate(savedInstanceState);
  14.             setContentView(R.layout.activity_main);
  15.         //Creating the LayoutInflater instance
  16.             LayoutInflater li = getLayoutInflater();
  17.         //Getting the View object as defined in the customtoast.xml file
  18.             View layout = li.inflate(R.layout.customtoast,
  19.               (ViewGroup) findViewById(R.id.custom_toast_layout));
  20.         //Creating the Toast object 
  21.             Toast toast = new Toast(getApplicationContext());
  22.             toast.setDuration(Toast.LENGTH_SHORT);
  23.             toast.setGravity(Gravity.CENTER_VERTICAL, 00);
  24.             toast.setView(layout);//setting the view of custom toast layout
  25.             toast.show();
  26.         }
  27.         @Override
  28.         public boolean onCreateOptionsMenu(Menu menu) {
  29.             getMenuInflater().inflate(R.menu.activity_main, menu);
  30.             return true;
  31.         }
  32. }

Output:

customtoastoutput1

Comments

comments