Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set fullscreen in Android R?

I need to put a screen in fullscreen in my app. For this I am using this code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_camera_photo)

However, the WindowManager.LayoutParams.FLAG_FULLSCREEN flag is deprecated.

My app supports Android Lollipop (API 21) to Android R (API 30). What is the correct way to make a screen go fullscreen?

like image 417
Vitor Ferreira Avatar asked Jul 10 '20 13:07

Vitor Ferreira


People also ask

How do I put android in fullscreen mode?

Using Android Studio (current version is 2.2. 2 at moment) is very easy to add a fullscreen activity. See the steps: Right click on your java main package > Select “New” > Select “Activity” > Then, click on “Fullscreen Activity”.

How do I turn off fullscreen mode on android?

Exit full screen Tap the video. At the bottom of the player, tap full screen exit .


3 Answers

KOTLIN

override fun onCreate(savedInstanceState: Bundle?) {     super.onCreate(savedInstanceState)     setContentView(R.layout.layout_container)     @Suppress("DEPRECATION")     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {         window.insetsController?.hide(WindowInsets.Type.statusBars())     } else {         window.setFlags(             WindowManager.LayoutParams.FLAG_FULLSCREEN,             WindowManager.LayoutParams.FLAG_FULLSCREEN         )     } } 

if this doesn't help, try to remove android:fitsSystemWindows="true" in the layout file

JAVA

class Activity extends AppCompatActivity {  @Override @SuppressWarnings("DEPRECATION") protected void onCreate(@Nullable Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.layout_container);     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {         final WindowInsetsController insetsController = getWindow().getInsetsController();         if (insetsController != null) {             insetsController.hide(WindowInsets.Type.statusBars());         }     } else {         getWindow().setFlags(                 WindowManager.LayoutParams.FLAG_FULLSCREEN,                 WindowManager.LayoutParams.FLAG_FULLSCREEN         );     } } } 
like image 132
Andriy D. Avatar answered Oct 03 '22 22:10

Andriy D.


I had problem like user924

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference

I could fix this problem by adding full screen setting code after setContentView. Also, usually, full screen will be screen without not only status bar, but also navigation bar too. Furthermore, just hide() method isn't enough. If we put only this line, when we swipe down screen to see status bar, it comes down, but never goes up again. By setting systemBarBehavior, we can make status bar and navigation bar appear temporarily only when we swipe just like full screen what we know.

setContentView(R.layout.YOUR_LAYOUT)

//Set full screen after setting layout content
@Suppress("DEPRECATION")
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    val controller = window.insetsController

    if(controller != null) {
        controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
        controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
} else {
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
like image 44
Mandarin Smell Avatar answered Oct 04 '22 00:10

Mandarin Smell


For API >= 30, use WindowInsetsController.hide():

window.insetsController.hide(WindowInsets.Type.statusBars())
like image 21
Saurabh Thorat Avatar answered Oct 03 '22 22:10

Saurabh Thorat