[Android] Custom Toast style with image

Public on October 24, 2015

The Android SDK has some default or standard toasts. In this Android tutorial I show you how you can make your own personal customized pop-up. Part of this example is creating a customized shape of the toast including an image in the toast (ImageView).
- We first create the shape of the toast through the XML.
- Then we create the layout XML of the toast.
- Finally we make the references and initiation via the Java.
At the result we got something like this...

So now open your project file in Eclipse and create a new XML file in your resources folder and name it: shape.xml. In this XML file we define a stroke (a border around the toast) we set a solid background color of the toast, and finally set some small curved corners in the toast.
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:shape="rectangle">
  5. <stroke
  6. android:width="2dp"
  7. android:color="#FF6700" />
  8. <solid
  9. android:color="#FF9000" />
  10. <corners
  11. android:bottomRightRadius="5dp"
  12. android:bottomLeftRadius="5dp"
  13. android:topLeftRadius="5dp"
  14. android:topRightRadius="5dp"/>
  15. </shape>
Now that we have set the shape of the custom toast we need to make the actual layout. Create in your layout folder a XML file called toastcustom.xml. In this layout we will have multiple LinearLayouts. One defining the overall layout, one for creating the layout which retrieves the ImageView, and lastly a LinearLayout referring to the TextView. Note that I have added some additional parameters to make things a bit nicer.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/toastcustom"
  4. android:background="@layout/shape">
  5. <LinearLayout
  6. android:layout_margin="5dip"
  7. android:orientation="horizontal"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. <ImageView
  11. android:layout_width="40dp"
  12. android:layout_height="40dp"
  13. android:src="@drawable/icon"/>
  14. <LinearLayout
  15. android:orientation="vertical"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. android:gravity="center">
  19. <TextView
  20. android:id="@+id/texttoast"
  21. android:paddingLeft="10dip"
  22. android:paddingRight="10dip"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"
  25. android:textColor="#ffffff"
  26. android:textSize="12sp"
  27. android:gravity="center"/>
  28. </LinearLayout>
  29. </LinearLayout>
  30. </LinearLayout>
So now we have created the shape and the layout of the toast. We need something in our tutorial which initiate the toast message. For this I just made a simple button in our Activity_Main.xml.
  1. <RelativeLayout xmlns:android="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" >
  6. <Button
  7. android:id="@+id/btn_showToast"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentTop="true"
  11. android:layout_centerHorizontal="true"
  12. android:layout_marginTop="50dp"
  13. android:text="Show Custom Toast" />
  15. </RelativeLayout>
As far as this Tutorial can be excited :) The fun part starts now (hehe). Open your MainActivity.java file.
First create reference to your Button (btnCustomToast) and set the onClickListener.
In the onClickListener we inflate the custom toast. Create a new initiation of LayoutInflater(Android SDK) and make the reference to your Layout (toastcustom) and to your TextView and set the text which you want in the toast to appear.
Now that we have inflated the Layout and TextView we need to create the actual toast. Create an new instance of Toast (Android SDK) mytoast and set it to the layout (layouttoast).
  1. package christianpeeters.customtoast;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  12. public class MainActivity extends Activity {
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. Button btnCustomToast = (Button)findViewById(R.id.btn_showToast);
  19. btnCustomToast.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. LayoutInflater inflater = getLayoutInflater();
  23. View layouttoast = inflater.inflate(R.layout.toastcustom, (ViewGroup)findViewById(R.id.toastcustom));
  24. ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Yippie, a custom toast");
  25. Toast mytoast = new Toast(getBaseContext());
  26. mytoast.setView(layouttoast);
  27. mytoast.setDuration(Toast.LENGTH_LONG);
  28. mytoast.show();
  29. }
  30. });
  31. }
  32. }
Now you have made a custom toast :)

[Advertising]Faster Thinking - Speed up your brain

Faster Thinking Game

sentiment_satisfied Emoticon