In this article, we learn how to create a Custom Toast.

Toast

A Toast provides simple feedback in a small popup about an operation. It only fills the amount of space required for the message and the current activity remains visible and interactive.
Custom-Toast-in-Android.jpg
The need for Custom Toast
But what if you want to display your custom view in a Toast? Such as shown in the image above, only text is displayed, no other views are attached to it. It displayed at the bottom of the screen, but what if I want to display it in
the center or the top of the screen?
Each of the preceding questions has an answer. Let's have a look.
Tutorial: Custom Toast
In this tutorial, we will learn how to create and how to use a custom toast in our application. We will create an application that displays a toast message such as "Success" and "Failure" messages.
Step 1
Create a new Android project called "CustomToastDemo" with the following properties:
Project Build Target: Android 2.2 or higher
Package Name: com.test
Activity Name: CustomToastDemoActivity
Step 2
Now, we need to create a custom toast layout file. So create one layout file and name it "custom_toast.xml" and paste the following code into it:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:background="#DDAAAAAA"
  6. android:layout_height="match_parent"
  7. android:padding="10dp"
  8. android:gravity="center_vertical">
  9. <ImageView
  10. android:id="@+id/imgCustomToast" android:layout_width="wrap_content"
  11. android:layout_height="match_parent"
  12. android:src="@drawable/success"
  13. android:layout_marginRight="20dp" />
  14. <TextView
  15. android:id="@+id/txtCustomToast" android:layout_width="wrap_content"
  16. android:layout_height="match_parent"
  17. android:text="C# Corner"
  18. android:gravity="center_vertical" />
  19. </LinearLayout>
But this code will give you an error at "drawable/success". So what you need to do is to download 2 files shown here.
Failure Icon: Failure-Icon.jpg
Success Icon: Success-Icon.jpg
Download them and put then in the "drawable" directory as shown in the following figure:
drawable-directory.jpg
If you see the graphical output of the current layout then it will look as in the following:
graphical-output-of-current-layout.jpg
Step 3
Create a package called "com.custom" in your "src" directory.
Create a Java file called "CustomToast" in the "com.custom" package.
After completing the process above, paste the following code into the "CustomToast.java" file:
  1. package com.custom;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.test.R;
  11. /**
  12. * Custom Toast class extends Toast class to customize it
  13. *
  14. * @author R
  15. *
  16. */
  17. public class CustomToast extends Toast {
  18. public CustomToast(Context context) {
  19. super(context);
  20. }
  21. /**
  22. * Custom Toast Cuontructor
  23. *
  24. * @param context
  25. * : Required to pass to super constructor
  26. * @param activity
  27. * : Require to create custom layout inflater and to inflat
  28. * layout
  29. * @param message
  30. * : Message to be shown in Toast
  31. * @param flag
  32. * : <br>
  33. * True : Success Image<br>
  34. * False: Failure ImageF
  35. */
  36. public CustomToast(Context context, Activity activity, String message,
  37. boolean flag) {
  38. super(context);
  39. /**
  40. * Create a LayoutInflater to inflate layout, here activity is useful
  41. */
  42. LayoutInflater inflater = activity.getLayoutInflater();
  43. /**
  44. * Create a view of layout which is used to show as toast
  45. */
  46. View view = inflater.inflate(R.layout.custom_toast, null);
  47. /**
  48. * get id of image view which is defined in layout file
  49. */
  50. ImageView img = (ImageView) view.findViewById(R.id.imgCustomToast);
  51. /**
  52. * get id of text view which is defined in layout file
  53. */
  54. TextView txt = (TextView) view.findViewById(R.id.txtCustomToast);
  55. /**
  56. * Check whether flag is true or false and do operation
  57. */
  58. if (flag)
  59. img.setImageResource(R.drawable.success);
  60. else
  61. img.setImageResource(R.drawable.fail);
  62. /**
  63. * Set message to text view
  64. */
  65. txt.setText(message);
  66. /**
  67. * Set gravity of Toast, you can set as per your need. Currently set to
  68. * "CENTER" of the screen
  69. */
  70. setGravity(Gravity.CENTER, 0, 0);
  71. /**
  72. * Pass how long toast will shown to the screen
  73. */
  74. setDuration(Toast.LENGTH_LONG);
  75. /**
  76. * This is main code to set custom view to Toast
  77. */
  78. setView(view);
  79. }
  80. }
Step 4
You have finished all the steps necessary to create a custom view. Now, it's time to implement that custom toast view for the real world.
You have your "CustomToastDemoActivity" file. Open it and paste the following code:
  1. package com.test;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class CustomToastDemoActivity extends Activity {
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. new com.custom.CustomToast(this, this,
  11. "Congratulations!! You are C# Member", true).show();
  12. new com.custom.CustomToast(this, this,
  13. ":-( You are not C# Member...", false).show();
  14. }
  15. }
Step 5
Bingo.....!!! Run your application and you will see the following output:
Output-Custom-Toast-in-Android.jpg
1Output-Custom-Toast-in-Android.jpg

Summary

In this tutorial, we learn how to use Toast as per our needs and extend the use of Toast.