Android: custom notification of Toast Inherit Default Toast

I have a custom toast notification that has an image and text. A custom toast works great, but I'm wondering how to make my own toast inherit toast by default? I want it to look like the default with beautiful 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>
+5
source share
4 answers

I am using this in one of my applications. Change a few things around and it will 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();
+6
source

To get the default background with nice rounded corners (for candy), use:

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

or

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

Toast Android, , toast_frame.9.png ..sdk\platform\android - [ ]\data\res\drawable - [ ]

:

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

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

+1

try it

<style name="Themename" parent="android:Theme.dialog">

from developer.android.com/guide/topics/ui/themes.html

0
source

Try entering a code.

Tilt the view from the XML layout and name it as inflated_xml_view.

    Toast toastView = new Toast(this);
    toastView.setView(inflated_xml_view);
    toastView.setDuration(Toast.LENGTH_LONG);
    toastView.setGravity(Gravity.CENTER, 0,0);
    toastView.show();
0
source

All Articles