I'm new to Android development and am trying to create a simple notification that tells a user when some user action has been completed.
I want the notification to appear at the top of the screen and disappear on its own shortly after. Basically I want a toast.
However in Android 30+, the Toast.makeText() method doesn't allow the option to set the position of the toast and when I create a custom toast I get a warning that setting the view is deprecated.
The answer to this post recommends using a snackbar, but I can't find an elegant way to set the snackbar to appear at the top of the screen either.
What is the best way to achieve what I'm looking for?
Here's the deprecated code attempt at getting the toast to the top of the screen
val inflater = layoutInflater
val layout: View = inflater.inflate(
R.layout.custom_toast, null
)
val text: TextView = layout.findViewById(R.id.text)
text.text = "Hello! This is a custom toast!"
val toast = Toast(applicationContext)
toast.setGravity(Gravity.TOP, 0, 0)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()