How can I hide the border from the title bar in Xamarin Android? I am using Xamarin Forms & Following code is used in MainActivity.cs to hide the rest of the title bar.
[Activity(Label = "", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
protected override void OnCreate(Bundle bundle)
{
this.RequestWindowFeature(WindowFeatures.ActionBarOverlay);
base.OnCreate(bundle);
//Transperent Action Bar
ActionBar.SetIcon(Android.Resource.Color.Transparent);
ActionBar.SetBackgroundDrawable(new ColorDrawable(Color.ParseColor("#000000ff")));
ActionBar.SetStackedBackgroundDrawable(new ColorDrawable(Color.ParseColor("#000000ff")));
Xamarin.Forms.Forms.Init(this, bundle);
SetPage(App.GetMainPage());
}
Seems like I don't have to do anything specific in Android Project, using following code in xaml.cs OnAppearing method did the trick
//Hide Nav Bar
NavigationPage.SetHasNavigationBar(this, false);
Use this code
protected override void OnCreate(Bundle bundle)
{
RequestWindowFeature(WindowFeatures.NoTitle);
base.OnCreate(bundle);
and i hope if this works for you
I think you should use Theme for that
[Activity(Label = "", MainLauncher = true, Theme="#android:style/Theme.Holo.Light.NoActionBar", ScreenOrientation = ScreenOrientation.Portrait)]
Hope this will help you
You can use this code in your styles.xml file in Droid Project. Setting the elevation to 0 dp is the main thing here.
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="elevation">0dp</item>
</style>
Just use your NavigationPage
public HelloWorlPage()
{
InitializeComponent();
NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetHasNavigationBar(this, false);
}
Try this theme in your MainActivity.cs ,
[Activity (Label = "XXX",
Theme = "#android:style/Theme.Holo.Light",
WindowSoftInputMode = SoftInput.AdjustPan,
ConfigurationChanges=global::Android.Content.PM.ConfigChanges.Orientation|global::Android.Content.PM.ConfigChanges.ScreenSize |
global::Android.Content.PM.ConfigChanges.KeyboardHidden)]
Related
I'm using Xamarin and I want to have transparent status bar in my application. I've read a lot of other threads here and on Xamarin forums but nothings helped me. The problems is instead of transparent status bar I got gray status bar.
Here is my source code: In MainActivity.cs
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
var uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions |= (int)SystemUiFlags.LayoutStable;
uiOptions |= (int)SystemUiFlags.LayoutFullscreen;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
Window.SetStatusBarColor(Color.Transparent)
}
If tried setting different properties to the theme in styles.xml but nothing helped. Currently styles.xml is:
<style name="MainTheme" parent="MainTheme.Base">
</style>
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#2196F3</item>
<item name="colorAccent">#FF4081</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">#style/AppCompatDialogStyle</item>
</style>
I'm testing on Android 5.1 (lollipop) so the transparent status bar should be supported. I even wrote simple native Android application to test if transparent is supported by my phone. I started with an application with blank activity, set a background image of the activity (to check if the status bar is transparent) and to MainActivity.java I wrote:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}
And I got transparent status bar. However, when I'm doing the same from Xamarin it doesn't work.
You can try to add the following to your styles.xml inside values-v21 folder.
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
Ah, didn't saw that you tried it by code already.
Use below mentioned code. That has worked for me perfectly for Xamarin forms.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.DecorView.SystemUiVisibility = 0;
var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
statusBarHeightInfo.SetValue(this, 0);
Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
}
LoadApplication(new App());
}
}
I want to make my activity full screen with status bar on top of it like this picture:
I have used this code in manifest inside activity tag:
'android:theme="#style/Theme.AppCompat.Light.NoActionBar"'
But my view doesn't start from the status bar & it looks like this:
How can I make my activity look like the first one?
I know that the guy asking the question may have found his own solution but for the people who are still looking for a solution this is a very simple solution but one thing it has a limitation till Kitkat so a condition is added
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
Add these to your Base Application Theme in styles.xml
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
And add following attribute to your parent layout:
android:fitsSystemWindows="false"
Hope this helps.
If you dont want your "Translucent navigation" transparent, here is the code to just make transparent the status bar
In your theme:
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
In your activity onCreate:
Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
winParams.flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.setAttributes(winParams);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1. Transparent Statusbar
window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.statusBarColor = Color.TRANSPARENT
2. Transparent Statusbar & Bottomnav bar
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
);
3. Hide Statusbar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
}
else {
#Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
4. Hide Statubar & Bottomnav bar
val actionBar: ActionBar? = supportActionBar
if (actionBar != null) actionBar.hide()
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
where to put this code ?
override fun onCreate(savedInstanceState: Bundle?) {
/* Put above code here ..... */
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_slider)
}
Note
I checked this code in Pixel 3A emulator
Maybe customise android OS not support
set styel <style name="Theme.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
To achieve something similar to this, use this:
1. Your Activity Theme
<style name="FullScreenTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowActivityTransitions">true</item>
</style>
2. There is NO NEED to add android:fitsSystemWindows property into your root layout XML file.
3. In your Activity
Add padding top to your toolbar. I am using custom toolbar, so I am adding padding to my toolbar of the height of statusbar.
toolbar.setPadding(0, getStatusBarHeight(), 0, 0)
and the function getStatusBarHeight() is added in my Utils class
fun Activity.getStatusBarHeight(): Int {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return if (resourceId > 0) resources.getDimensionPixelSize(resourceId)
else Rect().apply { window.decorView.getWindowVisibleDisplayFrame(this) }.top
}
put below of code inside onCreate()
private void setStatusBarTransparent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
View decorView = window.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
window.setStatusBarColor(Color.TRANSPARENT);
}
}
Try Like This
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Hope this one will help
In Kotlin just use following code in your activity, also add android:fitsSystemWindows="true" in your parent view of activity.xml layout file.
This will also show content over navigation bar too, provided you declared full screen style in your style.xml
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
}
you can use this code after onCreate
setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
your activity will be fullscrean with status bar :)
#tahsinRupam answer is correct and tested on post Kitkat version.
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
If you want to use it only for a certain activity, create a style, and attach it on your manifest style like this.
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/Activity.Fullscreen.Theme" />
<style name="Activity.Fullscreen.Theme" parent="MyMaterialTheme.Base">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
Thumbs up on origal #tahsinRupam
Happy codings Cheers !
Put this code in your Activity onCreate this will hide the status bar
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();
I want to hide status bar / notification bar in splash screen I am using
style:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
this dose not hide the status bar, how should i do this?
If you want to remove status bar then use this before setContentView(layout) in onCreateView method
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
credits
You can do something like below for appropriately setting theme on your particular activity -
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/launch_theme</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
Try this:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
Use Below Code to solve your problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Add Below line to hide
getSupportActionBar().hide();
}
Try this complete solution for backward below android version 16 and forward compatible solution
if (Build.VERSION.SDK_INT < 16)
{
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
For Kotlin users.
In styles.xml add new style:
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Now in AndroidManifest.xml add newly created style:
<activity
android:name=".MainActivity"
android:theme="#style/NoActionBarTheme"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"
>...</activity>
Finally in MainActivity.kt make following changes:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Add below line to hide status bar
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}
Now the status bar should be gone:
To make your activity fullscreen, add this code in onCreate before setContentView:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
This is what I used and it worked perfectly.
Add this code to your activity onCreate before setContentView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
if you are using this in theme app
Theme.MaterialComponents.NoActionBar
change to
Theme.AppCompat.NoActionBar
and add code in activity:
fun fullScreen(activity: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (activity.window.insetsController != null) {
val insetsController = activity.window.insetsController
if (insetsController != null) {
insetsController.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
insetsController.systemBarsBehavior =
WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
} else {
activity.window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
}
}
If you want to hide status bar line and action bar, you have to change the line in themes.xml
<style name="Theme.MyApp"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
to
<style name="Theme.MyApp"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
and this to activity class in which you don't want status bar line..
requestWindowFeature(Window.FEATURE_NO_TITLE)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Plz make sure that you have to add these lines above this in onCreate() method.
setContentView(R.layout.activity_main)
Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
I did some magic code. Buttons in the status bar can be clicked, but it's difficult. Part for 4.4 does not work well.
/** Скрывает статус-бар */
private void hideStatusBar(View decorView)
{
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
android.app.ActionBar actionBar = getActionBar();
if (actionBar != null)
actionBar.hide();
}
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
// for < 4.0
if (Build.VERSION.SDK_INT < 16)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// for > 4.0
else
{
final View decorView = getWindow().getDecorView();
hideStatusBar(decorView);
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
{
#Override public void onSystemUiVisibilityChange(int visibility)
{
if (visibility != View.SYSTEM_UI_FLAG_FULLSCREEN)
hideStatusBar(decorView);
}
});
}
}
catch (Throwable t)
{
Util.log("Ошибка при попытке свернуть статус бар");
t.printStackTrace();
}
setContentView(R.layout.activity_main);
}
If you want to remove the status bar then
For Java
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
For Kotlin
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
For flutter
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
I am trying to theme my app to follow the material Android look. My project is a Xamarin.Android project, which targets API 23, but the minimum API target is 16. So I have to use the AppCompat library to have the same design on all APIs.
I have followed these guides: Material Theme,Beautiful Material Design with the Android Support Design Library,Android Tips: Hello AppCompatActivity, Goodbye ActionBarActivity and created these files:
Resources/values/styles.xml:
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#F44336</item>
<item name="colorPrimaryDark">#D32F2F</item>
<item name="colorAccent">#FF4081</item>
</style>
</resources>
Resources/values-v21/styles.xml
<resources>
<!--
Base application theme for API 21+. This theme replaces
MyTheme from resources/values/styles.xml on API 21+ devices.
-->
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
namespace TestThemeApp
{
[Activity(Label = "TestThemeApp", MainLauncher = true, Icon = "#drawable/icon", Theme = "#style/MyTheme")]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
}
}
}
I'm just trying to make the the toolbar red, and the status bar a darker red, but the status bar color won't change. This is what it looks like:
What did I do wrong?
Try this in your style (Resources/values-v21/styles.xml)
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
>
<item name="android:statusBarColor">#color/cobalt_light</item>
I personaly prefer to handle it in the onCreate method of the activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.gray_very_dark));
window.setNavigationBarColor(getResources().getColor(R.color.gray_darker));
}
For a simple workaround you can change your primary color to the desired color of your status bar and it will change the color of your status bar automatically.
NOTE: It is a work around till you find the correct answer.
You'll have to add a flag at the top of the MainActivity and set the bar color at the end.
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
// First check if the current SDK is >= 21. Else it will cause trouble
if ((int)Build.VERSION.SdkInt >= 21)
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
SetBarColor();
}
public void SetBarColor ()
{
if((int)Build.VERSION.SdkInt >= 21)
Window.SetStatusBarColor(Color.ParseColor("#FF0000"));
}
}
Use below code snippet to change statusbar color:
public void setStatusBarColor(Activity context, int color) {
if (Build.VERSION.SDK_INT >= 21) {
Window window = context.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(context,color));
}
}
I think that i have found the most elegant solution based on the other answers:
The only thing i changed was added the flag in the OnCreate(Bundle bundle) function:
protected override void OnCreate(Bundle bundle)
{
if ((int)Build.VERSION.SdkInt >= (int)BuildVersionCodes.Lollipop)
Window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
}
And that's it! The style files do the rest, and on older version of android, the status bar just won't change.
I am trying to use the android.view.ActionMode with the new android.support.v7.widget.Toolbar, in addition to the traditional android.app.ActionBar. I am able to display it with:
toolbar.startActionMode(callback);
The problem is that the ActionMode is displayed over the ActionBar, and not over the Toolbar. Is there a way to change that?
I tryied to set the following in my theme, but it does not seem to change anything:
<item name="windowActionModeOverlay">true</item>
Since you are using the Toolbar, I also assume you are using the
AppCompatActivity and have replaced the built in ActionBar with
your custom Toolbar using setSupportActionBar(toolbar);
First of all ensure you are importing the correct namespace:
import androidx.appcompat.view.ActionMode;
// Or
import android.support.v7.view.ActionMode;
and NOT
import android.view.ActionMode;
then use
_actionMode = startSupportActionMode(this);
and NOT
_actionMode = startActionMode(this);
Do not start it on your activity, but on your toolbar. In you activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.startActionMode(mActionModeCallback)
and you have to use
<item name="windowActionModeOverlay">true</item>
in your theme as stated by Andre.
Try this in your theme:
<item name="windowActionModeOverlay">true</item>
I think the one thing people are not making clear is that the line
<item name="windowActionModeOverlay">true</item>
is placed in the Base theme i.e AppTheme and not the AppTheme.NoActionBar
<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="windowActionModeOverlay">true</item>
</style>
<style name="transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
find your AndroidManifest.xml ,next add below code in your application or Activity theme
<item name="windowActionModeOverlay">true</item>
so like:
<style name="WorkTimeListTheme" parent="AppTheme.NoActionBar">
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">#color/theme_primary</item>
</style>
So, after days going thru this thread, I finally got it working.
I'd like to summarize what I did.
Note: This is the solution using a Toolbar to replace the default ActionBar in AppCompatActivity.
I added this line to my AppTheme: It tells android that you want your action mode to overlay the toolbar
<item name="windowActionModeOverlay">true</item>
Use the right imports:
You have to use these imports in order to work with AppCompatActivity:
import androidx.appcompat.view.ActionMode;
// or
import android.support.v7.view.ActionMode;
Start the ActionMode on your Activity like so:
actionMode = startSupportActionMode(callback);
And not like so:
actionMode = startActionMode(callback);
You Activity creates the ActionMode on the toolbar automatically, because it's set as the supportActionToolbar.
The style handles the dsiplaying as overlay.
Thanks to #Kuffs and #Lefty.
This is the solution I made.
In my onCreateActionMode method of ActionMode.Callback, I add this:
StandaloneActionMode standaloneActionMode = (StandaloneActionMode) actionMode;
Field mContextView;
try {
mContextView = StandaloneActionMode.class.getDeclaredField("mContextView");
mContextView.setAccessible(true);
View contextView = (View) mContextView.get(standaloneActionMode);
MarginLayoutParams params = (MarginLayoutParams) contextView.getLayoutParams();
params.topMargin = mToolbar.getTop();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
It works for me.
I have tried all the methods above, but it still doesn`t work. And then, I tried the below method:
private class ActionModeCallback implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.note_find_action, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
((AppCompatActivity) getActivity()).getSupportActionBar().show();
}
}
Here, I used action mode and startSupportActionMode method of support library. At the same time I have also tried to modify the theme of given activity. Surely, it doesn`t work. So, if you really have no better choice you may try this one.
Just recently, I have found that I used the Colorful frame to enable multiple theme of my app, this will change the theme in code. When I tried to modify the style in this framework, it works.
Hope it works.
You can leave all the rubbish that only works in certain scenarios/phones/blue moons and simply hack it (in a very clean way):
Create 2 menu groups:
<menu>
<group android:id="#+id/group_normal">
<item id="#+id/action_edit"/>
</group>
<group android:id="#+id/group_action_mode"
android:visible="false">
<item id="#+id/action_mode_1"/>
<item id="#+id/action_mode_2"/>
..
</group>
</menu>
In your Fragment (or Activity):
class MyFragment: Fragment() {
private var actionMode = false
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
toolbar.apply {
setNavigationOnClickListener {
if(actionMode) endActionMode(this)
else findNavController().navigateUp()
}
setOnMenuItemClickListener {
when(it.itemId) {
R.id.action_edit -> {
// start action mode
startActionMode(this)
}
R.id.action_mode_1 -> {
// todo
}
R.id.action_mode_2 -> {
// todo
endActionMode(this)
}
...
else -> return#setOnMenuItemClickListener false
}
true
}
}
}
private fun startActionMode(toolbar: Toolbar) {
with(toolbar.menu) {
setGroupVisible(R.id.group_normal, false)
setGroupVisible(R.id.group_action_mode, true)
}
toolbar.title = 0 // todo format number of selected items
actionMode = true
}
private fun endActionMode(toolbar: Toolbar) {
with(toolbar.menu) {
setGroupVisible(R.id.group_normal, true)
setGroupVisible(R.id.group_action_mode, false)
}
toolbar.setTitle(R.string.original_title)
actionMode = false
}
}
Works every time as intended. Add extra functionality as needed.
If you see the view tree,You will can write below code:
ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
traverseView(decorView);
/**
* traverse find actionmodebar
* #param root view
*/
public void traverseView(View root) {
if (root==null){
return;
}
if (root instanceof ActionBarContextView){
root.setVisibility(View.GONE);
return;
}
if ((root instanceof ViewGroup)) { // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
traverseView(viewGroup.getChildAt(i));
}
}
}
Make sure to put this line:
<item name="windowActionModeOverlay">true</item>
in the style of the theme that you're using in your app (it's written in your manifest file).
After hours of debugging this problem, I realized that I was making changes to a style that I DIDN'T EVEN USE!