Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethodError: No virtual method getInsetsController()

I upgrade my app to SDK 30. I saw that FLAG_FULLSCREEN deprecated. Then, i changed the code with this;

final WindowInsetsController insetsController = getWindow().getInsetsController();
if (insetsController != null) {
    insetsController.hide(WindowInsets.Type.statusBars());
}

but I am getting this error when i try to run my app on API 26.

java.lang.NoSuchMethodError: No virtual method getInsetsController()Landroid/view/WindowInsetsController; in class Landroid/view/Window; or its super classes (declaration of 'android.view.Window' appears in /system/framework/framework.jar:classes2.dex)
like image 749
Ercan Acar Avatar asked Dec 22 '22 16:12

Ercan Acar


1 Answers

Google is currently working on a Compat version of WindowInsetsContrloller. This way, you won't need to branch if/else by android API level (as presented in @PerracoLabs's answer). On Compat version of WindowInsetsContrloller we don't need to use getWindow().setFlags() because window.insetsController?.hide() will work on all android versions.

Update:

They already made it - class androidx.core.view.WindowInsetsControllerCompat. You'll need to use androidx.core 1.5.0-alpha02 or greater.

My code example:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

    WindowCompat.setDecorFitsSystemWindows(window, false)

    hideSystemBars()
}

private fun hideSystemBars() {
    val insetsControllerCompat = WindowInsetsControllerCompat(window, window.decorView)
    insetsControllerCompat.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    insetsControllerCompat.hide(systemBars())
}

private fun showSystemBars() {
    val insetsControllerCompat = WindowInsetsControllerCompat(window, window.decorView)
    insetsControllerCompat.show(systemBars())
}
like image 120
Evgeniy Kosikov Avatar answered Jan 13 '23 23:01

Evgeniy Kosikov