Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast.makeText is not showing in Android 11 (with SDK 30 emulator)

According to https://developer.android.com/about/versions/11/behavior-changes-11#toasts, Toasts should still work as normal for Android11 (only setView() was deprecated).

Note that text toasts are still allowed; these are toasts created using Toast.makeText() that don't call setView().

However, I cannot get Toast to show for emulators running SDK30 with

    compileSdkVersion 30
    buildToolsVersion "30.0.3" 
    and with targetSdkVersion 30 
    

The code used was just a simple

    Toast.makeText(getApplicationContext(), "HELLO WORLD", Toast.LENGTH_SHORT).show();

The toast is displayed when I change the targetSdk to 29 (or with devices <SDK30).

Do I need to add any <queries> tags in manifest file for Toasts ? https://developer.android.com/about/versions/11/privacy/package-visibility

I've also tried the following without luck.

  1. rebooting the emulator Toast not showing in Android Q
  2. notification ON Toast message not shown
like image 382
Angel Koh Avatar asked Jan 13 '21 15:01

Angel Koh


People also ask

How do I set toast time on Android?

makeText(context, "Hello world, I am a toast.", Toast. LENGTH_SHORT). show(); The duration for which a toast is displayed on screen is unfortunately defined by a flag: you can either show it for a SHORT duration, which is 2 seconds or a LONG duration which is 3,5 seconds.

How do I customize a toast on Android?

If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView (View) method. Notice that the ID of the LinearLayout element is "toast_layout".

How do I change the toast gravity in Android?

Positioning your Toast A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.


1 Answers

First method

Updating of Android Emulator to 30.4.5 version fixed bug for me. P.S. I also updated Android SDK Platform-Tools to 31.0.0 version (may be this helped too).

Menu Tools -> SDK Manager -> SDK Tools, set checkbox of Android Emulator to V-mode, press button Apply, press button OK, confirm.

P.S. Also after updating of those the Android Studio and emulators were closed and project was cleaned before new compilation.

Second method

If you get this bug while using the last version of Android Emulator there is another solution. You can try the cold boot of emulator. Menu Tools -> AVD Manager. Find your emulator name, click button ▼ on the right side of it, select Cold boot now button.

like image 155
Zhebzhik Babich Avatar answered Sep 16 '22 11:09

Zhebzhik Babich