How to hide navigation bar - android

I've got a promblem with Android app.
I use this code to hide navigation bar.
public class Initer {
public static void fullScreen(Window window) {
if (Build.VERSION.SDK_INT >= 19) {
View decor = window.getDecorView();
fullScreen(decor);
} else {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
public static void fullScreen(View decor) {
if (Build.VERSION.SDK_INT >= 19) {
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
But when I show PopupWindow or Dialog up, navigation bar will come up again.
I tried do this
PopupWindow popupWindow =new PopupWindow(getWindow().getContext());
View popupWindowView = LayoutInflater.from(getWindow().getContext()).
inflate(R.layout.dialog_interval_insert, null);
Initer.fullScreen(popupWindowView);
...
popupWindow.showAsDropDown(button_start);
to hide navigation bar when PopupWindow showing up. But it doesn't work. When I call the PopupWindow, the navigation bar will show up then hide again. And when I dismiss() it, navigation bar will be there.
How can I hide navigation bar permanently in Whole application?

you can use this code
public void FullScreen_Activity(Activity activity) {
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
and change StatusBar Color with
public void setStatusBarColor(Activity activity, int RecColor) {
if (isApi21()) {
Window window = activity.getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(ContextCompat.getColor(activity, RecColor));
}
}
and how to use FullScreen_Activity method
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMethod().FullScreen_Activity(this);
setContentView(R.layout.activity_splash);
}
}

Related

Android - Toggle status bar transparency between Fragments in a Viewpager

I've exhausted essentially every post I've found regarding changing the status bar color programmatically, but none of them touch on this specific case.
I have an Activity that contains a ViewPager and a BottomNavigationView. The ViewPager holds three Fragments and the BottomNavigationView moves between them with smoothscroll turned off.
As the first of the three Fragments is a map, I'd like the status bar to be transparent when the first Fragment is shown, but revert back to its opaque color on the other two Fragments.
Here's some code that shows how I'm attempting to set and revert the status bar states.
private class TabSelectedObserver implements Observer<Integer> {
#Override
public void onChanged(#Nullable Integer selectedTab) {
if (selectedTab != null) {
activityMainBinding.mainPager.setCurrentItem(selectedTab, false);
switch (selectedTab) {
case 0:
applyTransparentStatusBarTheme();
break;
default:
applyOpaqueStatusBarTheme();
break;
}
}
}
}
private void applyTransparentStatusBarTheme() {
hideSystemUi();
getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}
private void applyOpaqueStatusBarTheme() {
showSystemUi();
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.rippallete_700));
}
private void hideSystemUi() {
// Set flags for hiding status bar and navigation bar
mSystemUiVisibility = mSystemUiVisibility
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(mSystemUiVisibility);
}
private void showSystemUi() {
// Reset flags for hiding status bar and navigation bar
mSystemUiVisibility = mSystemUiVisibility
& ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(mSystemUiVisibility);
}
Here's a gif showing the behavior I end up with
Every thing I tried with fitSystemWindows didn't produce the result I wanted.
If anyone has any suggestions for how you would give each Fragment an individual status bar color, with 1 being transparent, I'd really appreciate it.
So assuming you're setting the status bar colours based on the position of whatever tab you have selected you could do something similar to this:
private void updateStatusBarColour(int tabPosition) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
switch (tabPosition) {
case FIRST_TAB:
case SECOND_TAB:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mRootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryLight));
break;
case THIRD_TAB:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mRootView.setSystemUiVisibility(0);
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
break;
case FOURTH_TAB:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mRootView.setSystemUiVisibility(0);
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorDarkTan));
break;
}
}
}
Note that I call setSystemUiVisibility on mRootView, where mRootView is the root parent view containing the ViewPager (or whatever is the root layout of your containing Activity). The call specifically sets the theme of the status bar so that status bar items (like the battery symbol) can be visible on dark or light background depending on what colour you end up setting.

Hide status bar until exit

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

Lollipop make statusbar transparent on content frame of navigation drawer

i am trying to set status bar transparent on my content frame inside navigation drawer. I create the app using the template contain on android studio. I need make status bar transparent when navigation drawer is not open so my toolbar back ground is draw behind it.
This one:
This One What I mean
Not this one, because it already transparent:
Not this one
I actually had an issue related to this due to some custom rendering with parallax effects... Anyway, you can just set the status bar background color in onDrawerClosed(View drawerView) and then set it back to transparent when opening the drawer:
public class WindowHelper {
public static void setColoredStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(R.color.secondary_color));
}
}
public static void setTranslucentStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
So for you it'd something like:
#Override
public void onDrawerClosed(View drawerView) {
WindowHelper.setColoredStatusBar(activity);
}

onSystemUiVisibilityChange not called after screen rotation

I'm trying to have a fullscreen app, with the navigation bar disapearing after few seconds and reappearing on user interaction (like the Android 3d gallery video player behaviour) for Android 4.2.2 (API 17)
To achieve this, i'm using this code :
public class MainActivity extends Activity {
private View mRootView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// OnCreate code ...
mRootView = getWindow().getDecorView();
setOnSystemUiVisibilityChangeListener();
showSystemUi(false);
}
private void showSystemUi(boolean visible) {
int flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (!visible) {
// We used the deprecated "STATUS_BAR_HIDDEN" for unbundling
flag |= View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
mRootView.setSystemUiVisibility(flag);
}
private void setOnSystemUiVisibilityChangeListener() {
mRootView.setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
showSystemUi(false);
}
}, 2000);
}
});
}
As you can see, i've been inspired by the Android API and the android 3d gallery code : http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2.2_r1/com/android/gallery3d/app/MoviePlayer.java#MoviePlayer.setOnSystemUiVisibilityChangeListener%28%29
So, here is my problem :
This code works greatly if i don't move the device.
But if i made a screen rotation, the navigation bar will not be displayed, clicking on screen will display it, but the callback onSystemUiVisibilityChange seems to be never called (checked with debugger), so the navigation bar will never disappear. And if i rotate the screen again, it will works again. (In fact, it seems like it only works if i rotate the screen while the navigation bar is displayed)
Does anyone has an idea of where this problem comes from ?
Thx.

Toggle Fullscreen mode

I need some help toggling fullscreen mode. I have a setting in a preference screen to go fullscreen. In my main activity's onResume I have:
if(mFullscreen == true) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
But this doesn't seem to work because it needs to be called before setContentView right?
... But Also, I have requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView and it takes away the title AND status bar... Can anybody offer some help?
---Edit---
Ok, I had a bug that was causing this to not work. So it actually does. Now, I just need to know how to toggle the title bar.
private void setFullscreen(boolean fullscreen)
{
WindowManager.LayoutParams attrs = getWindow().getAttributes();
if (fullscreen)
{
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
}
else
{
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
}
getWindow().setAttributes(attrs);
}
/**
* toggles fullscreen mode
* <br/>
* REQUIRE: android:configChanges="orientation|screenSize"
* <pre>
* sample:
* private boolean fullscreen;
* ................
* Activity activity = (Activity)context;
* toggleFullscreen(activity, !fullscreen);
* fullscreen = !fullscreen;
* </pre>
*/
private void toggleFullscreen(Activity activity, boolean fullscreen) {
if (Build.VERSION.SDK_INT >= 11) {
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility();
int newUiOptions = uiOptions;
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.i(context.getPackageName(), "Turning immersive mode mode off. ");
} else {
Log.i(context.getPackageName(), "Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
activity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
} else {
// for android pre 11
WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
if (fullscreen) {
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
} else {
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
}
activity.getWindow().setAttributes(attrs);
}
try {
// hide actionbar
if (activity instanceof ActionBarActivity) {
if (fullscreen) ((ActionBarActivity) activity).getSupportActionBar().hide();
else ((ActionBarActivity) activity).getSupportActionBar().show();
} else if (Build.VERSION.SDK_INT >= 11) {
if (fullscreen) activity.getActionBar().hide();
else activity.getActionBar().show();
}
} catch (Exception e) {
e.printStackTrace();
}
// set landscape
// if(fullscreen) activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
// else activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
my code working fine with android 2.3 and 4.4.2
Since Jellybean (4.1) there is a new method that doesn't rely on the WindowManager. Instead use setSystemUiVisibility off of the Window, this gives you more granular control over the system bars than using WindowManager flags. This is how you enable fullscreen:
if (Build.VERSION.SDK_INT < 16) { //ye olde method
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else { // Jellybean and up, new hotness
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 this is how you revert the above code:
if (Build.VERSION.SDK_INT < 16) { //ye olde method
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else { // Jellybean and up, new hotness
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
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.show();
}
There's a shorter toggle full screen method implementation:
private void toggleFullscreen() {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}
It uses bitwise XOR logic to toggle FLAG_FULLSCREEN.
My solution combines answers from:
cprcrack's answer to this question
Holoeverywhere : how to programmatically remove at runtime the action bar from an activity
Android: Programmatically detect if device has hardware menu button
I added these methods to my Activity. To toggle full screen, use setFullScreen(!isFullScreen()).
public boolean isFullScreen() {
return (getWindow().getAttributes().flags &
WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
}
#SuppressLint("NewApi")
public void setFullScreen(boolean full) {
if (full == isFullScreen()) {
return;
}
Window window = getWindow();
if (full) {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
if (Build.VERSION.SDK_INT >= 11) {
if (full) {
getActionBar().hide();
} else {
getActionBar().show();
}
}
}
In my case, I wanted a menu button to do the toggling. The problem: on a device without a hardware menu button, hiding the action bar also hides the toggle to return from full screen. So, I added some extra logic so it only hides the action bar if the device has a hardware menu button. Note that devices running SDK 11-13 did not have one.
if (Build.VERSION.SDK_INT >= 14
&& ViewConfiguration.get(this).hasPermanentMenuKey()))) {
if (full) {
getActionBar().hide();
} else {
getActionBar().show();
}
}
Older devices (running Gingerbread or earlier) have a Title Bar instead of an Action Bar. The following code will hide it, but be aware that the Title Bar cannot be shown/hidden once the activity has started. I included a message to the user in my help menu stating that changes to full screen may not take full effect on older devices until they restart the app/activity (which of course assumes you persist their selection and execute this code only if they want it full screen).
// call before setContentView()
if (Build.VERSION.SDK_INT < 11) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
Kotlin solution for the fullscreen.
if (isFullscreen) {
WindowInsetsControllerCompat(activity?.window!!, root).apply {
hide(WindowInsetsCompat.Type.systemBars())
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else {
WindowInsetsControllerCompat(activity?.window!!, root)
.show(WindowInsetsCompat.Type.systemBars())
}

Categories

Resources