I have an android app which uses immersive mode for all activities - so its a full fullscreen app.
I have a BaseActivity class from which all other activities extend. In this activity I call the following to enable fullscreen/immersive
HelmiBlankActivity:
private boolean apiLowerImmersive = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
apiLowerImmersive = true;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus && !apiLowerImmersive ) {
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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
In activities it works great, the problem is: When opening a new activity (per intent) the actionbar/titlebar are displayed for a short time and then hidden again - which seems kind of laggy/buggy.
The Application also has a theme:
styles.xml:
<style name="FullscreenTheme" parent="android:Theme.Holo.Light">
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
I have tried applying the android:Theme.Holo.Light.NoActionBar as well - no success during transitions.
I could not find anything on stackoverflow (which btw is a great community and has helped me with many problems) or anywhere else on the internet and would appreciate your help.
If you put this snippet into onCreate() method your activities will open with the bars already hidden.
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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
You can also add conditions for older SDKs. The only problem I've got with this setup is the fact that once i swipe down to show bars, they won't hide again...
Related
When running on Android 10 emulator and Galaxy S8 physical device (running Android 9), the next code works perfectly fine. Nevertheless, on Motorola Moto G8 (running Android 9) entering full screen mode, all view's content goes down and in place of status bar blank black space appears.
Galaxy G8, normal and full screen views:
Motorola Moto G8, normal and full screen views:
As you can see, on motorola g8 screen goes down the size of status bar and on top of screen appears blank black screen.
Here is Android manifest:
android:theme="#style/AppTheme"
Here's styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<!--Required Api 21 above-->
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:onClick="onButtonClick"
android:src="#drawable/llama" />
</RelativeLayout>
Activity class:
public class MainActivity extends AppCompatActivity {
private boolean isFullScreen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
isFullScreen = false;
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
isFullScreen = true;
}
}
});
}
#Override
protected void onResume() {
super.onResume();
}
// onWindowFocusChanged()
public void onButtonClick(View view) {
if (isFullScreen) {
showSystemUI();
} else {
hideSystemUI();
}
}
private void hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
The whole project is available at Github: https://github.com/alecpetrosky/Android-Immerse-Demo
add below code after function setContentView
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
Some android phones have soft buttons (back, home, menu). I manage to hide them using setSystemUiVisibility as described here.
However, how to make the softbuttons visible pragmatically?
You can use setSystemUiVisibility()
Request that the visibility of the status bar or other screen/window decorations be changed.
SAMPLE CODE
public class MainActivity extends AppCompatActivity {
TextView tvTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
setContentView(R.layout.activity_main);
tvTextView=findViewById(R.id.tvTextView);
tvTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getWindow().getDecorView().setSystemUiVisibility(View.VISIBLE);
}
});
}
Setting View.SYSTEM_UI_FLAG_VISIBLE worked for me.
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
Please,somebody,tell me how to hide/disable status/notification bar using onluWindowManager, do not using WindowManager.LayoutParams.FLAG_FULLSCREEN, AndroidManifest or something that is not WindowManager.
this worked for me, but i had to call the method every time i wanted to make sure the flag was like this
anyViewInTheLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
Put this line in your activity onCreate() method above setContentView() :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Hope it help!
use this as a style:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
You can do it programmatically like this:
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
More info on official pages: http://developer.android.com/training/system-ui/status.html
You can put these code in your onCreate() method of your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
}
Hope it help!
I was wondering how I could hide the navigation bar in an Android application?
I know how to hide it initially, but as soon as I touch the screen it pops back up. I want to hide it the same way games like Clash of Clans hide it where the only way to make it pop up is by swiping down the notifications or by swiping where the navigation bar should be.
use immersive mode check this Immersive mode
// This snippet hides the system bars.
private void hideSystemUI() {
// Set the IMMERSIVE flag.
// Set the content to appear under the system bars so that the content
// doesn't resize when the system bars hide and show.
mDecorView.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 // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
Here it is in the context of the onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
The Android docs have a good explanation of what the different flags do:
Using Immersive Full-Screen Mode
Just put this method in your activity where you want to hide status bar and navigation bar in sticky mode.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//This is used to hide/show 'Status Bar' & 'System Bar'. Swip bar to get it as visible.
View decorView = getWindow().getDecorView();
if (hasFocus) {
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);
}
}
the easy way is adding this 2 lines after super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
You Can Create An Activity With The Snippet Above,And Then Add Another Windows Above The Layout:
LayoutParams layoutParameteres=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
v.setLayoutParams(layoutParameteres);
final WindowManager.LayoutParams parameters=new WindowManager.LayoutParams(sizeX, sizeY, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
parameters.gravity=Gravity.CENTER;
parameters.x=0;
parameters.y=0;
wm.addView(v, parameters);
Good Luck With It
In your activity's onCreate method:
this.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Modify this line in the AndroidManifest.xml file
android:theme="#style/Theme.AppCompat.DayNight.NoActionBar"
I want to make an activity to go into IMMERSIVE mode and hide top and buttom system bars as soon as it starts.
In developers site of android they say I should use setSystemUiVisibility() and provide SYSTEM_UI_FLAG_IMMERSIVE and SYSTEM_UI_FLAG_HIDE_NAVIGATION.
How can I do this in the OnCreate() method of the activity? I think the setSystemUiVisibility is not provided in the Activity class and it should happen in a view. Is there a workaround?
UPDATE
ok According to doorstuck I added the following lines but I dont see any changes, navigation bar and buttom buttons are still visible :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
}
//Rest of activity code
Get the decor view:
getWindow().getDecorView().setSystemUiVisibility(...)
Remember that the arguments are bit flags. Only call the method above once:
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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
Chris Banes gist shows a nice Helper Class we can use to set the immersive mode for all Versions from HoneyComb to Lollipop https://gist.github.com/chrisbanes/73de18faffca571f7292.
Update:
I tried get it from his github repo to include in my project, but i had to clone the gist files into my project and adjsut the packagename. If someone knows how to include it properly as a dependency, u r welcome to help me.
I added it in my FullScreenActivity i want to use the ImmersiveStickyMode like this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
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_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
SystemUiHelper uiHelper = new SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE ,flags);
uiHelper.hide();
}
You can create global function to go into immersive mode like:
public static void enableImmersiveMode(final View decorView) {
decorView.setSystemUiVisibility(setSystemUiVisibility());
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(setSystemUiVisibility());
}
}
});
}
public static int setSystemUiVisibility() {
return 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;
}
Above code will also control system UI visibility change. Hope this will help you.
Much nicer and credit to William J. Francis:
public class GameActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/* my code you dont need this
assets=getAssets();
sGame= new GameView(this, GAME_WIDTH, GAME_HEIGHT);
setContentView(sGame);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
*/
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_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
#Override
protected void onDoSomethingOtherImportantThing(){
...
}
}
Answer has already been given but here is How to utilize Immersive mode.
In your activity:
just before setContentview().......
call the method: toggleHideyBar();
Follow documentation documentation from Developer android and copy this method in your Activity.
android:immersive="true" will hide the Bottom system bars
<application>
<activity
android:name=".CarrierActivity"
android:label="#string/app_name"
android:excludeFromRecents="true"
android:immersive="true"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="com.example.SetupWiz.SUW_CARRIER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>