How to hide the titlebar in freeform multiwindow mode? - java

In an activity I can hide the titlebar using the following code, but when the Activity is started freeform multiwindow mode the title bar appears with x and multiwindow icon. How to hide the titlebar in freeform multiwindow mode ?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
final WindowInsetsController insetsController = getWindow().getInsetsController();
if (insetsController != null) {
insetsController.hide(WindowInsets.Type.statusBars());
}
} else {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
}

Related

How to hide status bar and make content appear behind on every device?

Goal
I'm trying to hide status bar and make content appear behind it.
What I have tried
On the phone I'm currently developing (Samsung Galaxy S9+), I managed to make it work with this function
public static void removeAndroidNavBar(View view){
int uiOptions = 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;
view.setSystemUiVisibility(uiOptions);
}
which is called on the OnCreate() method after SetContentView() like this
removeAndroidNavBar(getWindow().getDecorView());
Problem
HOWEVER, I run the code on a OnePlus Nord 2 which is on OxygenOs, and this doesn't work. The status bar is not displayed (there is a black space instead), but I can't figure out how to make it disappear completely, and make content appear behind it.
The doc
As the documentation says HERE,
On Android 4.1 and higher, you can set your application's content to appear behind the status bar, so that the content doesn't resize as the status bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN. You may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain a stable layout.
which is what I'm doing, and I'm encountering the problem only on the One Plus Nord 2 device
What am I doing wrong?
If you wants to achieve some thing like this
try like this:-
fun Activity.makeStatusBarTransparent() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.apply {
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
// addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(hasImmersive(context)){
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
}else {
// decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
// View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
// View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
// View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
if (Build.VERSION.SDK_INT >= 21) {
var window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.setStatusBarColor(context.getResources().getColor(color));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
var decor = getWindow().getDecorView();
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val controller = getWindow().insetsController
controller?.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller?.hide(WindowInsets.Type.statusBars())
}*/
}
} else {
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
statusBarColor = Color.TRANSPARENT
}
}
Log.d("Has onscreen Navigation"," "+hasImmersive(context))
} catch (e: Exception) {
}
}

How to permanently hide Navigation Bar in an Android

I am making an application,and have access to system permissions.
I want to make the navigation bar hide permanently,
It should not appear even on user interaction.
Now i'm using this piece of code,
it hides the bar but when user touches the screen it showing again. is there any way to hide it permanently until activity onStop();
protected void hideBottomUIMenu() {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
Window _window = getWindow();
WindowManager.LayoutParams params = _window.getAttributes();
params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
_window.setAttributes(params);
}
}
Any technical advice or comments/suggestions on the best implementation would be hugely appreciated.
I hope this might be helpful for you. Also look at this

In Android How do I change colour of status bar transparent as shown in bellow photograph [duplicate]

This question already has answers here:
Android Completely transparent Status Bar?
(38 answers)
Closed 4 years ago.
Can I change status bar colour like this in my app?
Yes, you can change it's color, but I am not sure if changing it to transparent will have the effect you want
public static void setStatusBarColor(Activity activity, int color){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(activity, color));
}
You can do it by this-:
private void changeStatusBarColor() {
Window window = this.getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
// finally change the color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
}
}

Hide and Show Actionbar elements disappeared

I need hide ActionBar at one precise moment hide the ActionBar and place the app in fullscreen, so hide the view of the elements like text view. The problem is that when I go back to normal mode the textview is behind the ActionBar.
Does anyone have any idea how can I fix this?
int uiOptions;
mView = this.getWindow().getDecorView();
TextView text = (TextView) findViewById(R.id.textName);
if (enabled) {
text.setVisibility(View.INVISIBLE);
uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
mView.setSystemUiVisibility(uiOptions);
getSupportActionBar().hide();
customHandler.postDelayed(updateTimerThread, 1000);
} else {
text.setVisibility(View.VISIBLE);
uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
mView.setSystemUiVisibility(uiOptions);
getSupportActionBar().show();
customHandler.removeCallbacks(updateTimerThread);
if (mMenu != null) {
mMenu.setTitle(R.string.menu_start);
}
mView.setBackgroundColor(Color.WHITE);
}
Just put the element to stay visible after the ActionBar is visible and it worked!

When using Immersive Mode with dialogs, nav. bar reappears and resizes my layout

I'm using Immersive Mode in my app when it's running on Android 4.4+. (http://developer.android.com/training/system-ui/immersive.html)
My activity indeed shows in full screen, and I work around the volume key pressing by using setOnSystemUiVisibilityChangeListener. I also have similar code for putting dialogs into immersive mode.
However, when a dialog is shown, the nav. bars jump on the screen and then retreat immediately. When the dialog is dismissed it's even worse - the nav. bars jump and resize the activity behind.
The following is my class for supporting immersive mode. It is simply called on each Activity's onResume and also a separate function is called when building each dialog.
Did I miss any flag or callback, or is it a known Android issue?
public class ImmersiveModeHelper {
public ImmersiveModeHelper(Activity activity)
{
mActivity = activity;
}
#SuppressLint("NewApi")
public void supportFullScreenImmersiveMode()
{
MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveMode: ");
// Support full-screen immersive mode on Android 4.4 and up
if (Build.VERSION.SDK_INT >= 19)
{
// Get the needed flags by reflection and use them
try
{
final int immersiveFlag = View.class.getField("SYSTEM_UI_FLAG_IMMERSIVE_STICKY")
.getInt(null);
final int hideNavigationFlag = View.class
.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION").getInt(null);
final int fullScreenFlag = View.class.getField("SYSTEM_UI_FLAG_FULLSCREEN").getInt(
null);
// Set the flags to the window decor view
mActivity.getWindow().getDecorView()
.setSystemUiVisibility(immersiveFlag | hideNavigationFlag | fullScreenFlag);
// Set a callback to be called when visibility changes
// (workaround
// for volume keys)
mActivity
.getWindow()
.getDecorView()
.setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener()
{
#Override
public void onSystemUiVisibilityChange(int visibility)
{
MyLog.d("ImmersiveModeHelper.supportFullScreenImmersiveMode().new OnSystemUiVisibilityChangeListener() {...}: onSystemUiVisibilityChange: " +
"");
if ((visibility & (immersiveFlag | hideNavigationFlag)) == 0)
{
Handler uiHandler = UiThreadUtils.getUiHandler();
uiHandler.removeCallbacks(mHideSystemUiCallback);
uiHandler.postDelayed(mHideSystemUiCallback,
HIDE_SYSTEM_UI_DELAY_MILLI);
}
}
});
} catch (Exception e)
{
e.printStackTrace();
MyLog.e("ImmersiveModeHelper: supportFullScreenImmersiveMode: couldn't support immersive mode by reflection");
}
} else
{
MyLog.i("ImmersiveModeHelper: supportFullScreenImmersiveMode: not supported on this platform version");
}
}
public static void supportFullScreenImmersiveModeForDialog(final Dialog dlg)
{
MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveModeForDialog: ");
// Support full-screen immersive mode on Android 4.4 and up
if (Build.VERSION.SDK_INT >= 19)
{
final Window dlgWindow = dlg.getWindow();
// Get the needed flags by reflection and use them
try
{
final int immersiveFlag = View.class.getField("SYSTEM_UI_FLAG_IMMERSIVE_STICKY")
.getInt(null);
final int hideNavigationFlag = View.class
.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION").getInt(null);
final int fullScreenFlag = View.class.getField("SYSTEM_UI_FLAG_FULLSCREEN").getInt(
null);
// Set the flags to the window decor view
int flags = dlgWindow.getDecorView().getSystemUiVisibility();
flags |= (immersiveFlag | hideNavigationFlag | fullScreenFlag);
dlgWindow.getDecorView().setSystemUiVisibility(flags);
// Set a callback to be called when visibility changes
// (workaround for volume keys)
dlgWindow.getDecorView().setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener()
{
#Override
public void onSystemUiVisibilityChange(int visibility)
{
MyLog.d("ImmersiveModeHelper.supportFullScreenImmersiveModeForDialog(...).new OnSystemUiVisibilityChangeListener() {...}: onSystemUiVisibilityChange: ");
if ((visibility & (immersiveFlag | hideNavigationFlag)) == 0)
{
Runnable hideSystemUiCallback = new Runnable()
{
#Override
public void run()
{
supportFullScreenImmersiveModeForDialog(dlg);
}
};
Handler uiHandler = UiThreadUtils.getUiHandler();
uiHandler.removeCallbacks(hideSystemUiCallback);
uiHandler.postDelayed(hideSystemUiCallback,
HIDE_SYSTEM_UI_DELAY_MILLI);
}
}
});
} catch (Exception e)
{
e.printStackTrace();
MyLog.e("ImmersiveModeHelper: supportFullScreenImmersiveMode: couldn't support immersive mode by reflection");
}
} else
{
MyLog.i("ImmersiveModeHelper: supportFullScreenImmersiveMode: not supported on this platform version");
}
}
private Activity mActivity;
private Runnable mHideSystemUiCallback = new Runnable()
{
#Override
public void run()
{
supportFullScreenImmersiveMode();
}
};
private static final int HIDE_SYSTEM_UI_DELAY_MILLI = 0;
}
From the Google API:
It's good practice to include other system UI flags (such as SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION and SYSTEM_UI_FLAG_LAYOUT_STABLE) to keep the content from resizing when the system bars hide and show.
You should also make sure that the action bar and other UI controls are hidden at the same time. This snippet demonstrates how to hide and show the status and navigation bars, without resizing the content:
// 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);
}
Hope this helps.
In Dialog or BottomSheetDialogFragment you have to implement this solution which is work for me.
Step 1:
In your dialog or BottomSheetDialog, write this code in onActivityCreated method,
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
var viewParent = view
while (viewParent is View) {
viewParent.fitsSystemWindows = false
viewParent.setOnApplyWindowInsetsListener { _, insets -> insets }
viewParent = viewParent.parent as View?
}
}
Step 2:
Also, override the below method :
override fun setupDialog(dialog: Dialog, style: Int) {
super.setupDialog(dialog, style)
dialog?.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
}
Now see the magic :)
What worked for me was adding the following to the parent layout xml:
android:fitsSystemWindows="true"
I fixed this by setting my flags on onWindowFocusChanged() rather than onResume() as per https://developer.android.com/training/system-ui/navigation.
The problem is the full screen popup resets the SYSTEM_UI flags. Tested and works.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Before setting full screen flags, we must wait a bit to let UI settle; otherwise, we may
// be trying to set app to immersive mode before it's ready and the flags do not stick
long IMMERSIVE_FLAG_TIMEOUT = 500L;
int FLAGS_FULLSCREEN = (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_HIDE_NAVIGATION);
mFullView.postDelayed(() -> mFrameLayout.setSystemUiVisibility(FLAGS_FULLSCREEN),
IMMERSIVE_FLAG_TIMEOUT);
}
I think you can also follow the new guidelines in https://developer.android.com/training/system-ui/immersive. I have not tested it though.
private fun hideSystemBars() {
val windowInsetsController =
ViewCompat.getWindowInsetsController(window.decorView) ?: return
// Configure the behavior of the hidden system bars
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}

Categories

Resources