-4

i want custom designed toast message in my project, i want custom toast with background color and image in toast message. i have seen this type of toast message in many android application. i want to implement this type of toast in my application.

i follow this link : customize toast in android

e.g.

enter image description here

Community
  • 1
  • 1
bbr patel
  • 32
  • 2
  • 6

2 Answers2

2

Yes, you can show custom toast.

All you need to do is: create custom xml for toast.

Something like:

my_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mylayout"
    android:orientation="horizontal"
    android:background="@android:color/cyan">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/my_img"
        android:id="@+id/imageview">
    </ImageView>

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1" android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="My message"
        android:textColor="@android:color/black">
    </TextView>

</LinearLayout>

Now, in your activity, instead of default Toast, inflate this custom xml.

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.my_toast,
                               (ViewGroup) findViewById(R.id.mylayout));

Toast custToast = new Toast(this);
custToast.setView(view);
custToast.show();

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

you can do it simply by creating a layout for toast in xml

layout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="#DAAA">

    <ImageView
        android:id="@+id/toast_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="10dp" />

    <TextView android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#FFF" />

</LinearLayout

to inflate and setup the custome toast:

LayoutInflater inflater = getLayoutInflater();
                        View layout = inflater.inflate(R.layout.toast_custom,
                                                       (ViewGroup) findViewById(R.id.toast_layout_root));
                        ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
//put your image in the drawable folder
                        image.setImageResource(R.drawable.username_incorrect);
                        TextView text = (TextView) layout.findViewById(R.id.toast_text);
                        text.setText("The user name is incorect!");

                        Toast toast = new Toast(getApplicationContext());
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(layout);
                        toast.show();

>

Blue_Alien
  • 2,148
  • 2
  • 25
  • 29