How to hide status & navigation bars DURING popup menu? [Kotlin]

Viewed 76

This is my progress so far. The problem with this is that the status bar flashes briefly when the menu starts, and the navigation bar stays present during the menu and selection.

Is anyone aware of how to make a popup menu display in a fullscreen application without the system bars ever showing?

class MainActivity : AppCompatActivity() {
    lateinit var popup2:View
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        popup2 = findViewById<Button>(R.id.popup_press)
        supportActionBar?.hide()  //Doesn't work

        popup2.setOnClickListener {
            val popup = PopupMenu(applicationContext, popup2)
            popup.menuInflater.inflate(R.menu.popupmenu, popup.menu)
            popup.setOnMenuItemClickListener { i ->
                when (i.itemId) {
                    R.id.menuA -> {
                        Toast.makeText(applicationContext, "Menu A", Toast.LENGTH_LONG).show()
                    }
                    R.id.menuB -> {
                        Toast.makeText(applicationContext, "Menu B", Toast.LENGTH_LONG).show()
                    }
                    R.id.menuC -> {
                        Toast.makeText(applicationContext, "Menu C", Toast.LENGTH_LONG).show()
                    }
                    else -> false
                }
                true
            }
            popup.show()
        }
    }

    //Hides status bar AFTER menu has shown. Can't stop it showing!!!!!
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            hideSystemUIView(popup2) //runs before menu and after
        } else {
            hideSystemUIView(getWindow().getDecorView()) // Hides menu - blinks
        }
    }

    fun hideSystemUIView(view: View) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            window.attributes.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.setDecorFitsSystemWindows(false)
            window.insetsController?.apply {
                hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
                systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            @Suppress("DEPRECATION")
            window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
        }
    }
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/popup_press"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="POP UP MENU"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menuA"
        android:title="menu A">
    </item>
    <item
        android:id="@+id/menuB"
        android:title="menu B">
    </item>
    <item
        android:id="@+id/menuC"
        android:title="menu C">
    </item>
</menu>
1 Answers

How do you get down from an elephant? You don't; you get down from a duck.

Using this same philosophy I have considered that it isn't possible to correct/alter the behaviour of the in-built menu functions. My solution has been to scrap those and just build my own menu capability which really just needed a few standardised text fields in a Linear layout. Now I have full control over what happens.

Here is an example field:

        <TextView
            android:id="@+id/choice03"
            android:layout_width="@dimen/choicemenuletterwidth"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/choicemenuletterpadding"
            android:paddingStart="@dimen/choicemenuletterpadding"
            android:paddingBottom="@dimen/choicemenuletterpadding"
            android:layout_marginTop="@dimen/choicemenumargintop"
            android:layout_marginStart="@dimen/choicemenumarginstart"
            android:layout_marginEnd="@dimen/choicemenumarginstart"
            android:text="@string/menus_redo"
            android:textColor="@color/title_text"
            android:textSize="@dimen/choicemenuletterheight"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </TextView>
Related