Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Custom Toast Notification Inherit Default Toast

I have a custom toast notification which has an image and text. The custom toast works fine however I am wondering how do I make my custom toast inherit the default toasts look and feel? I want it to look like the default one with the nice rounded corners and borders.

This is what my custom toast looks like.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/toast_layout_root"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp"
  android:background="#DAAA">
    <ImageView android:id="@+id/chatIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_chat"/>
    <TextView android:id="@+id/text"
        android:text="@string/unread_message_toast"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>
like image 990
EpicOfChaos Avatar asked Dec 31 '11 05:12

EpicOfChaos


2 Answers

I use this in one of my apps. Change a few things around and it should work for you too.

Toast ImageToast = new Toast(getBaseContext());
                LinearLayout toastLayout = new LinearLayout(
                        getBaseContext());
                toastLayout.setOrientation(LinearLayout.HORIZONTAL);
                ImageView image = new ImageView(getBaseContext());
                image.setImageResource(R.drawable.easter_egg);
                toastLayout.addView(image);
                ImageToast.setView(toastLayout);
                ImageToast.setDuration(Toast.LENGTH_SHORT);
                ImageToast.show();
like image 142
adneal Avatar answered Sep 27 '22 16:09

adneal


To get the default background with the nice rounded corners (on lollipop) use:

android:background="@android:drawable/toast_frame"

or

android:background="?android:attr/toastFrameBackground"

It will give the Toast background depending on the version of Android, if you want the latest I suggest looking for the toast_frame.9.png file under ..sdk\platforms\android-[latest version]\data\res\drawable-[density]

Text Style:

    android:textAppearance="@android:style/TextAppearance.Toast"
    android:textColor="@android:color/bright_foreground_dark"
    android:shadowColor="#BB000000"
    android:shadowRadius="2.75"

Source: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/transient_notification.xml

like image 20
TouchBoarder Avatar answered Sep 27 '22 18:09

TouchBoarder