Toast setGravity() method does not work in Level 30 API

Asked

Viewed 79 times

1

I’m trying to convey a message from Toast on top of a Activity common, when clicking on a Button, using the method setGravity(Gravity.TOP, 0, 0) in API Level 30 (release R), however not working. The original section, in Kotlin, would be the following:

val toast = Toast.makeText(
                this,
                R.string.correct_toast,
                Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.TOP, 0, 0)
            toast.show()

In the API Level 29 (release Q) works normally. Looking at the documentation, (https://developer.android.com/reference/kotlin/android/widget/Toast#setgravity), I came across a Warning saying that the Toast.setGravity is as no-op in API Build.VERSION_CODES#R. I don’t quite understand what that is Build.VERSION_CODES#R I mean, but apparently it doesn’t seem to work in Build new (specifically in the Level 30). How could I get around this by using the API Level 30? Or really in the Build newer can no longer use the setGravity?

The Warning mentioned above:

Warning: Starting from Android Build.VERSION_CODES#R, for apps Targeting API level Build.VERSION_CODES#R or Higher, this method is a no-op when called on text toasts.

1 answer

0

Often this change is indicating that it’s nice to exchange Toast for snackbar, like:

val snackbar = Snackbar.make(layout, message, duration)
    val snackbarLayout: View = snackbar.view
    val lp = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    )

    lp.setMargins(50, 0, 0, 0)
    
    snackbarLayout.setLayoutParams(lp)
    snackbar.show()

snackbarLayout.setLayoutParams(lp); snackbar.show();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.