1

I am trying to display a custom toast view in my app when the user receives a notification. The custom toast appears fine, but I can't inflate its content and then update it as I wish. Here is my code:

Watcher class receiving the notification

class Watcher : ParsePushBroadcastReceiver() {
    override fun onPushReceive(context: Context, intent: Intent) {
        NotificationToast.show(context, "title text")
    }
}

NotificationToast.kt

class NotificationToast {
    companion object {
        fun show(context: Context, title: String) {
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val layout = inflater.inflate(R.layout.notification_toast, null)
            // This line below always returns null
            layout.findViewById<AppCompatTextView>(R.id.notification_toast_title_text_view).text = title
            val toast = Toast(context)
            toast.duration = Toast.LENGTH_SHORT
            toast.setGravity(Gravity.TOP, 0, 0)
            toast.view = layout
            toast.show()
        }
    }
}

notification_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_toast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginHorizontal="24dp"
    android:layout_marginTop="24dp" >

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/notification_toast_title_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

For some reason, in NotificationToast, the line with the findViewById() trying to access the notification_toast_title_text_view textview is always null. What is the reason?

Thank you for your help

John DoeDoe
  • 133
  • 1
  • 7
  • 1
    Not sure if this is it but FYI starting with API 30 custom toast views are deprecated: https://developer.android.com/reference/android/widget/Toast#setView(android.view.View) – Ken Wolf Mar 18 '23 at 14:49
  • Yes, Android now ignores your request to set a custom layout. But that wouldn’t be the cause of the issue they described since they’re calling it directly on the layout, not the Toast after trying to set it on the Toast. OP, when you say it gives you null, do you mean the above code throws NullPointerException? – Tenfour04 Mar 18 '23 at 15:16
  • @KenWolf thank you for the information. I don't really know what to use then. I've read some things about SnackBars but I've read [here](https://stackoverflow.com/questions/32453946/how-to-customize-snackbars-layout) that they are not very customizable... – John DoeDoe Mar 18 '23 at 15:29
  • @Tenfour04 No it doesn't throw any exception, it's just that the underlying textview is not found with the `findViewById()` line, hence setting the text doesn't work – John DoeDoe Mar 18 '23 at 15:30
  • If it wasn't found, it would throw a NullPointerException when you did `.text =`. How are you determining there is a null? – Tenfour04 Mar 18 '23 at 19:04

0 Answers0