Custom Toast inflater must be initialized

68 Views Asked by At

i have this fun

private fun testToast(){
        val inflater:LayoutInflater
        val view = inflater.inflate(R.layout.custom_toast_message)
        val toast = Toast(this.context)
        toast.setGravity(Gravity.TOP,0,70)
        toast.duration = Toast.LENGTH_LONG
        toast.view =view
        toast.show()
    }

the main idea its to change the background color of the toast without getting a square toast

in this code i get an error in inflater.inflate

(inflater must be initialized)

this is the test xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/background_blue"
    android:padding="16dp"
    android:weightSum="1"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_toasticon"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"/>

    <TextView
        android:id="@+id/TextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.8"
        android:textColor="@color/gauge_red"
        android:fontFamily="@font/roboto"
        android:text="Prueba prueba PRUEBA"
        />

</LinearLayout>

con someone help me or annother way to do a toast with custom color.

change the color of a toast with rounded corners

2

There are 2 best solutions below

0
Tenfour04 On BEST ANSWER

You declared the inflater variable but never assigned anything to it (using =). You can’t use a variable’s value if you haven’t assigned anything to it yet.

If this function is in an Activity, you can delete the line that declares val inflater and replace inflater. with layoutInflater. to use the property of the Activity. If it’s in a Fragment, use requireActivity().layoutInflator..

By the way, the latest version of Android has deprecated setting a custom view for your toast. On Android R and later if you set a view on your Toast, it won’t show. They want you to use SnackBars instead. There might be a third party library available that could help create something more similar to Toasts.

1
ObscureCookie On

you can make a drawable resource to achieve it

toast_background.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/teal_200" />
    <corners android:radius="48dp" />
</shape>

and use this drawable for background of toast view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/toast_background"
    android:orientation="horizontal"
    android:padding="16dp"
    android:weightSum="1">

    <ImageView
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:src="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.8"
        android:text="Prueba prueba PRUEBA"
        android:textColor="@color/purple_700"
        android:textSize="14sp" />

</LinearLayout>

the result