Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android full screen on only one activity?

Tags:

java

android

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)

like image 496
Dimitris gs Avatar asked Jan 06 '18 04:01

Dimitris gs


2 Answers

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.

like image 126
Lalit Fauzdar Avatar answered Sep 28 '22 07:09

Lalit Fauzdar


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

like image 21
Aditya Avatar answered Sep 28 '22 07:09

Aditya