0Day Forums
android.view.View.systemUiVisibility deprecated. What is the replacement? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Kotlin (https://0day.red/Forum-Kotlin)
+--- Thread: android.view.View.systemUiVisibility deprecated. What is the replacement? (/Thread-android-view-View-systemUiVisibility-deprecated-What-is-the-replacement)

Pages: 1 2 3


android.view.View.systemUiVisibility deprecated. What is the replacement? - piersone - 07-20-2023

I have updated the project target API version to 30, and now I see that the systemUiVisibility property is deprecated.

The following kotlin code is the one I'm using which is actually equivalent to [setSystemUiVisibility](

[To see links please register here]

) method in Java.

playerView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)

Please let me know if you have got any stable replacement for this deprecated code. The google's recommendation is to use `WindowInsetsController`, but I don't how to do that.


RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - oleary614 - 07-20-2023

I hope It helps you.

Previously, when implementing edge-to-edge navigation or immersive mode, one of the first steps to take was to use the systemUiVisibility flags in order to request the app to be laid out fullscreen,
This new Android release deprecates this field and in order to layout the app fullscreen you have to use a new method on the `Window` class: `setDecorFitsSystemWindows` passing `false` as an argument like below.

window.setDecorFitsSystemWindows(false)


`WindowInsetsController` class which allows you to do things that previously were controlled via `systemUiVisibility` flags, like hiding or showing the status bar or navigation bar(hide and show methods, respectively)

For example, you can easily show and hide the keyboard as shown below:


// You have to wait for the view to be attached to the
// window (otherwise, windowInsetController will be null)
view.doOnLayout {
view.windowInsetsController?.show(WindowInsets.Type.ime())
// You can also access it from Window
window.insetsController?.show(WindowInsets.Type.ime())
}




RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - opalescently871077 - 07-20-2023

As of version `1.5.0-alpha02`, `androidx.core` has `WindowCompat.setDecorFitsSystemWindows()`

To enable edge-to-edge:
```
WindowCompat.setDecorFitsSystemWindows(window, false)
```


RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - crewless927277 - 07-20-2023

WindowCompat.setDecorFitsSystemWindows(window, false)
Watch this [tutorial][1] from official Android Developers channel.


[1]:


RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - ezequiel646 - 07-20-2023

TL;DR snippet

Wrapping in if-else structure needed to avoid `java.lang.NoSuchMethodError: No virtual method setDecorFitsSystemWindows` exception on older SDK versions.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
} else {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}


Links with full information about insets and fullscreen modes in Android 11

[To see links please register here]





RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - beefteqzg - 07-20-2023

Also you may want to have a translucent status bar and you can do it simply through setting a style to your app's theme as follows:

<style name="App.MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowLightStatusBar">true</item>
</style>

A lint message can be displayed (depending on you curren min api level): *android:windowLightStatusBar requires API level 23 (current min is 21)*, so you need to override this theme in v23 styles


RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - anopubic510737 - 07-20-2023

If somebody search for Java version.

For `Activity`:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().setDecorFitsSystemWindows(false);

if (getWindow().getInsetsController() != null) {
getWindow().getInsetsController().hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
} else {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}


for `FragmentDialog` and `AlertDialog`:

if (getDialog() != null && getDialog().getWindow() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getDialog().getWindow().setDecorFitsSystemWindows(false);
} else {
if (getActivity() != null) {
getDialog().getWindow().getDecorView().setSystemUiVisibility(getActivity().getWindow().getDecorView().getSystemUiVisibility());
}
}
}



RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - adnanigpxvbcx - 07-20-2023

According to [Chris Banes @
Android Developers official youtube channel
][1] use following code

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var view: View

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
view = binding.root
setContentView(view)
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) hideSystemUI()
}

private fun hideSystemUI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Tell the window that we want to handle/fit any system windows
WindowCompat.setDecorFitsSystemWindows(window, false)

val controller = view.windowInsetsController

// Hide the keyboard (IME)
controller?.hide(WindowInsets.Type.ime())

// Sticky Immersive is now ...
controller?.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

// When we want to hide the system bars
controller?.hide(WindowInsets.Type.systemBars())

/*val flag = WindowInsets.Type.statusBars()
WindowInsets.Type.navigationBars()
WindowInsets.Type.captionBar()
window?.insetsController?.hide(flag)*/
} else {
//noinspection
@Suppress("DEPRECATION")
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
}
}
Here is Sample Link [User interface sample][2]


[1]:
[2]:

[To see links please register here]




RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - lsiunixxjdyln - 07-20-2023

For compatibility, use `WindowCompat` and `WindowInsetsControllerCompat`. You'll need to upgrade your gradle dependency for `androidx.core` to at least `1.6.0-alpha03` so that there will be support for `setSystemBarsBehavior` on SDK < 30.


private fun hideSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, mainContainer).let { controller ->
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}

private fun showSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, mainContainer).show(WindowInsetsCompat.Type.systemBars())
}

You can find out more information about `WindowInsets` by watching this [YouTube video][2]

For devices with notches at the top of the display, you can add the following to your v27 `theme.xml` file make the UI appear either side of the notch:

<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>


You can read more at this link: [Display Cutout][1]


[1]:

[To see links please register here]

[2]:


RE: android.view.View.systemUiVisibility deprecated. What is the replacement? - nowt452 - 07-20-2023

For Java users (thanks to @James):

```java
//hide system UI
Window window = activity.getWindow();
View decorView = activity.getWindow().getDecorView();

WindowCompat.setDecorFitsSystemWindows(window, false);
WindowInsetsControllerCompat controllerCompat = new WindowInsetsControllerCompat(window, decorView);
controllerCompat.hide(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.navigationBars());
controllerCompat.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
```
(Edit) Use `BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE` to only show sytemBars temporarily when when swiping, `BEHAVIOR_SHOW_BARS_BY_SWIPE` will show them permanently after swiping.

Source :

[To see links please register here]