Hide status bar until exit - android

I have managed to hide status bar on start up. But whenever I touch the controls status bar shows up. To solve this I tried this code to hide it on every touches but it shows up first then it hides. This loop continues forever.
private View mContentView;
private View mControlsView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testgame);
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
mContentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mContentView.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);
}
});
I have also tried to change theme android:theme="#android:style/Theme.NoTitleBar.Fullscreen" but still hides it until I touch controls. I want to hide it until I exit the application. It is possible to do it without root permissions because most of games that I played can hide it until I exit the game or swipe down on

I have just found solution on this page that hides status bar permanently.
Firstly I added this import android.view.WindowManager;.
Then here is my OnCreate function:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testgame);
//Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Hide action bar
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
And this code helps you to show status bar again
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);// Show status bar

Related

Show/Hide Back Button in Toolbar

I want to show back button in toolbar under some circumstances, and hide it under another. I wrote below code:
public void setupToolbar() {
if (mStacks.get(mCurrentTab).size() > 1) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbar.setNavigationOnClickListener(v -> onBackPressed());
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
}
But my problem is when I set home button as false, It won't be visible anymore even the first condition becomes true. Can anybody help me?
Show/Hide Back Button in Toolbar. please try it
show code
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
and hide code
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
if i have understood your point correctly you should use below codes:
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
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);
}
this function hides BackButton and HomeButton
for show you should use below codes:
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);
}

Auto hide status bar

Is there a way to auto hide the status bar?
I've already made the status bar hidden when activity starts and I thought it will automatically disappear after a few seconds or when the user interacts whith something else but it doesn't. And I'm still unable to make it disappear.
Here's my code :
if (Build.VERSION.SDK_INT < 16) {
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);
}
I finally found the solution after reading this page.
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
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // hide status bar and nav bar after a short delay, or if the user interacts with the middle of the screen
);
Try using these flags:
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN);
Google has a great write up on it here
Our base activity has a method to make it full screen.
Call this in onCreate after super.onCreate and before setContentView() like this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
removeTitleBar();
setContentView(your_layout);
}
this is the method:
protected void removeTitleBar() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
if (getSupportActionBar() != null)
getSupportActionBar().hide();
//Remove notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
This works for any api level as far as we support it. But we do not go as low as api 8 or so.
Sometimes 15, mostly 17+.
Hope this helps.

hide and show statusbar without any effect to layout in android

I want to write a book-reader application that normally runs in full-screen, but when user touched screen, status bar become visible OVER my main layout without re-creating the whole view.
I already read these but no help at all:
Hide notification bar without using fullscreen
Hide Notification bar
Android Show Activity Title/status bar at the top after it is hidden
any idea how can I do it?
thanks
finally, I did it!!
these 2 methods will show and hide status bar without destroying the layout.
you need to set
private View mMainView; // The main view of the activity
private int mSystemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
then
/** Hides StatusBar and ActionBar */
private void hideSystemUi() {
ActionBar ab = getActionBar();
ab.hide();
// Set flags for hiding status bar
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
// Hide the status bar.
mMainView.setSystemUiVisibility(uiOptions);}
and
/** Shows StatusBar and ActionBar */
private void showSystemUi() {
ActionBar ab = getActionBar();
ab.show();
// Reset flags
mMainView.setSystemUiVisibility(this.mSystemUiVisibility);
}
and put this in your onCreate() method:
mMainView = findViewById(R.id.mainLayout);
mMainView.setSystemUiVisibility(mSystemUiVisibility);

Android: show/hide status bar/power bar

I am trying to create a button where I can hide or show the status bar on my tablet.
I've put in the onCreate
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
and in the buttons
show:
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
hide:
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
Any hints/tipps?
//edit
I've looked at this hint here: http://android.serverbox.ch/?p=306
and changed my code like this:
private void hideStatusBar() throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"});
proc.waitFor();
}
private void showStatusBar() throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
proc.waitFor();
}
So if I click on my buttons an the methods are called I can see that something is happening because the app is waiting some seconds.
I also looked into LockCat and see that something is happening.
show: http://pastebin.com/CidTRSTi
hide: http://pastebin.com/iPS6Kgbp
Do you have the fullscreen theme set in the manifest?
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
I don't think you'll be able to go fullscreen without this.
I would use the following to add and remove the fullscreen flag:
// Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
For Some People, Showing status bar by clearing FLAG_FULLSCREEN may not work,
Here is the solution that worked for me, (Documentation) (Flag Reference)
Hide Status Bar
// Hide Status Bar
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
View decorView = getWindow().getDecorView();
// Hide Status Bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
Show Status Bar
if (Build.VERSION.SDK_INT < 16) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
View decorView = getWindow().getDecorView();
// Show Status Bar.
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
}
used for kolin in android
for hide status bar in kolin no need to used semicolon(;) at the end of the line
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
in android using java language for hid status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
I know its a very old question, but just in case anyone lands here looking for the newest supported way to hide status bar on the go programmatically, you can do it as follows:
window.insetsController?.hide(WindowInsets.Type.statusBars())
and to show it again:
window.insetsController?.show(WindowInsets.Type.statusBars())
I've tried so many things.
Finally, It is the most suitable code to hide and show full screen mode.
private fun hideSystemUi() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(true)
} else {
// hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
}
}
private fun showSystemUi() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
} else {
// Show status bar
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.decorView.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_STABLE
}
}
It Implemented it in this app : Android Breakdown.
Go to Videos(Bottom Bar) > Play Any Video > Toggle Fullscreen
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide status bar
requestWindowFeature( Window.FEATURE_NO_TITLE );
getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.activity_main);
}
For Kotlin users
TO SHOW
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
TO HIDE
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
with this method, using SYSTEM_UI_FLAG_IMMERSIVE_STICKY the full screen come back with one tap without any implementation. Just copy past this method below and call it where you want in your activity.
More details here
private void hideSystemUI() {
getWindow().getDecorView().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_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
Reference - https://developer.android.com/training/system-ui/immersive.html
// 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);
}
fun Activity.setStatusBarVisibility(isVisible: Boolean) {
//see details https://developer.android.com/training/system-ui/immersive
if (isVisible) {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
} else {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
}

Hide status bar while having Media controller on android

i am using this method to hide status bar :
public static void hideStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT < 16) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = activity.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
this works fine but when i start video i have notice the status bar is visible again , after some test on my code i have realized that the status bar i visible after i add the MediaController .
how can i keep my full screen and still using MediaController ?
this is how i add the MediaController
mMediaController = new MediaController(getContext());
mMediaController.setAnchorView(this);
setMediaController(mMediaController);
use:
<application
...
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
or
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
and see this link:
http://developer.android.com/training/system-ui/status.html
and this link:
How to hide status bar in Android
Yes, the videoView can bring back the status bar. Not sure what the behind principle is. While you can resolve it by setting a setOnSystemUiVisibilityChangeListener, Something like:
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
);
}
});

Categories

Resources