7

My app has 3 activities(activity_main,activity_one,activity_two). I want to full screen only the first activity so I searched and I found out I got to use this:

 View decorView = getWindow().getDecorView();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}

When I go to the other activities that are not full screen and press the back button and go back to main activity the home button back button is there hiding the bottom of my main activity.How do I solve this? Also if possible I would like to know how can I make my activity full screen but keep the action bar. edit: Im asking for a full screen on one activity and not the other two(except if the action bar stays).If i go on activity_two (which is not full screen) i have the home button back button etc .If i press the back button and go back to the main activity the buttons stay there which i don't want to (not only because i want my main full screen but because the buttons from activity two stay and hide the bottom of my main activity)

Dimitris gs
  • 91
  • 1
  • 13
  • 1
    Possible duplicate of [Fullscreen Activity in Android?](https://stackoverflow.com/questions/2868047/fullscreen-activity-in-android) – AskNilesh Jan 06 '18 at 04:26
  • not really there is a problem over here while that guy asks just for full screen i on the other hand im asking for a full screen on one activity and not the other two(except if the action bar stays).If i go on activity_two (which is not full screen) i have the home button back button etc .If i press the back button and go back to the main activity the buttons stay there which i don't want to (not only because i want my main full screen but because the buttons from activity two stay and hide the bottom of my main activity) – Dimitris gs Jan 06 '18 at 04:31

3 Answers3

5

As onWindowsFocusChanged() is what you need so try this:

1. First, Declare this variable as global in your MainActivity

private int currentApiVersion;

2. then paste this code in onCreate() of that activity.

currentApiVersion = Build.VERSION.SDK_INT;
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
    getWindow().getDecorView().setSystemUiVisibility(flags);
    final View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
          @Override
          public void onSystemUiVisibilityChange(int visibility) {
              if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                   decorView.setSystemUiVisibility(flags);
              }
          }
    });
}

3. Now paste this method in your MainActivity class.

@SuppressLint("NewApi")
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
               View.SYSTEM_UI_FLAG_LAYOUT_STABLE
             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
             | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
             | View.SYSTEM_UI_FLAG_FULLSCREEN
             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
             | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
 }

Now this should work, give it a try.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
5

You can do this in two ways,

Programmatically -- Set FullScreen flag to window of activity inside onCreate() method (just after or before setContentView()). Whom you want to be fullscreen. For example,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                      WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Or you can set theme in your AndroidManifest.xml as,

<activity 
         android:name=".YourActivityName"
         android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

Or If you are using AppCompatActivity then you can set as,

<activity 
        android:name=".YourActivityName"
        android:theme="@android:style/Theme.AppCompat.Light.NoActionBar"/> 

For more info about the window flags check this link https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

Aditya
  • 3,525
  • 1
  • 31
  • 38
1

use this method:

private fun changeScreenSystemUiController(isFullScreen: Boolean) {
    window?.also {
        WindowCompat.setDecorFitsSystemWindows(it, !isFullScreen)
        WindowCompat.getInsetsController(it, it.decorView).apply {
            systemBarsBehavior =
                if (isFullScreen)
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                else
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
            if (isFullScreen)
                hide(WindowInsetsCompat.Type.systemBars())
            else
                show(WindowInsetsCompat.Type.systemBars())
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            it.attributes.layoutInDisplayCutoutMode =
                if (isFullScreen)
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                else
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
        }
    }
}
Mahdi Zareei
  • 1,299
  • 11
  • 18