ActionBar text's background color not changing - java

I put an ActionBar into my app and changed the background color, as I'll show below. Oddly enough, the color behind the text on the ActionBar won't change. (picture)
I looked around and found this:
<item name="android:actionBarItemBackground">#null</item>
But I would prefer not to use it because then my minSdk has to be set to 14, whereas I have it at 11 now. I did try boosting it up to 14 to see if this tag would work, but I again couldn't get the color to change.
Does anyone have any experience with this? Many thanks in advance for your help.
Styles.xml:
<style name="AppBaseTheme" parent="Theme.AppCompat">
</style>
<!-- Application theme. -->
<style name="MyAppTheme" parent="AppBaseTheme">
<item name="android:colorBackground">#color/background</item>
<item name="android:background">#color/background</item>
<item name="android:textColor">#color/myWhite</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="actionMenuTextColor">#color/myWhite</item>
<!--Support Library compatibility-->
<item name="background">#color/myBlack</item>
</style>
drawable/actionbar_background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/myBlack" />
</shape>
menu/login.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/settings_label"
android:showAsAction="never" />
</menu>
And finally, inside the LoginActivity class, the onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks again!

I figured it out. Turns out I was using the wrong attribute -- instead of using android:background in the styles.xml file, you need to use android:windowBackground. android:background changes all sorts of other things, too.

Related

Cant style Android ActionMode

I am making a notes app.In that app i want an actionmode to trigger when long pressed on any item.It works but i cant change any style attributes of the actionmode.I have included my custom style in the styles.xml of my app, still doesn't work
styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/white</item>
<item name="android:textColor">#color/black</item>
<item name="colorPrimaryVariant">#color/blue_main</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/white</item>
<item name="colorSecondaryVariant">#color/white</item>
<item name="colorOnSecondary">#778899</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="m">#D3D3D3</item>
<item name="android:actionBarStyle" >#style/Theme.AppCompat.Light</item>
<item name="actionModeStyle">#style/Widget.ActionMode</item>
<item name="actionBarTheme">#style/Theme.AppCompat.Light</item>
</style>
<style name="Widget.ActionMode" parent="#style/Widget.AppCompat.Light.ActionMode.Inverse">
<item name="background">?attr/actionModeBackground</item>
<item name="android:actionBarDivider">#null</item>
<item name="actionModeCloseDrawable">#drawable/cancel</item>
<item name="android:actionModeBackground">#android:color/white</item>
</style>
<style name="CustomMaterialDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Background Color-->
<item name="android:background">#FFFFFF</item>
<!-- Text Color for title and message -->
<item name="colorOnSurface">#BF40BF</item>
<!-- Text Color for buttons -->
<item name="colorPrimary">#BF40BF</item>
<item name="shapeAppearanceOverlay">#style/ShapeAppearance.MaterialComponents.LargeComponent
</item>
</style>
</resources>
I specifically want to remove a green divider line that shows up at the bottom of the contextual menu i have attached the picture please see green divider line below the action mode menu
solved it. The problem was with the theme applied to the custom toolbar that i implemented. applied the lighttheme and everything works fine

action bar menu icon is not showing properly after gradle update

Before update gradle it was awesome but after update its changed to
Actually I was using appcompat v7:26.0.0 and it was working fine but after downgrade to 25.0.0( due to a customization) , I found that problem and I tried to fix it but I was disappointed and I also configured back to 26.0.0 but I am getting this problem till now. How to fix it ?
There is no icon size issue because till yesterday it was ok.
my code which i am using is
Manifeast.xml
<activity
android:name=".ui_activities.MyInvoicesList"
android:label="#string/label"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustResize" />
styles.xml
<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>
</style>
menu_download.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:icon="#drawable/ic_cloud_download_black_24dp"
android:title="Download Invoices"
android:id="#+id/menu_downloadInvoices"
app:showAsAction="always">
</item>
</menu>
My Java Code is
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_download, menu);
return super.onCreateOptionsMenu(menu);
}
Problem solved by using correct size of icons 24*24
Actually I was using icon with 96*96 thats why I was getting that bug I have updated my icon size and fixed it

Android Action Bar Text Custom Color

I am using a theme in styles.xml as below:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/md_blue_700</item>
<item name="android:colorAccent">#color/md_blue_900</item>
<item name="android:colorPrimaryDark">#color/md_blue_900</item>
<item name="android:windowBackground">#color/md_white_1000</item>
<item name="android:colorControlNormal">#color/md_blue_700</item>
<item name="android:colorControlActivated">#color/md_blue_700</item>
<item name="android:colorControlHighlight">#color/md_blue_700</item>
<item name="android:textColorPrimary">#color/md_white_1000</item>
</style>
which I am declaring as android:theme in AndroidManifest.xml:
<activity
android:name=".LoginActivity"
android:theme="#style/AppTheme"
android:label="#string/activity_login_actionbar" />
The problem is highlighted in red in the image below.
Since I am setting
<item name="android:textColorPrimary">#color/md_white_1000</item>
to white the text in the progress bar also appears white and is invisible. If I change it to gray, the text in the action bar also changes to gray which I do not want.
I have tried things such as creating a seperate theme for the progress dialog but when I do that I lose the rounded corners of the dialog box and such.
This is my java code for creating the spinner:
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setTitle("Attempting Sign In");
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
How should I go about solving this problem?
All answers appreciated!
You can use Theme.AppCompat.Light.DarkActionBar and remove <item name="android:textColorPrimary">#color/md_white_1000</item> in your theme style, then the title will become white. You can check the comparison between Light and Dark themes here
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/md_blue_700</item>
<item name="android:colorAccent">#color/md_blue_900</item>
<item name="android:colorPrimaryDark">#color/md_blue_900</item>
<item name="android:windowBackground">#color/md_white_1000</item>
<item name="android:colorControlNormal">#color/md_blue_700</item>
<item name="android:colorControlActivated">#color/md_blue_700</item>
<item name="android:colorControlHighlight">#color/md_blue_700</item>
</style>

Android: Menu item always show up in overflow

I have looked around many times already at different posts and none of them have solved this problem for me. My XML defined menu item always show up in the the overflow, and it show up twice. Here is the XML file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/add_Playlist"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="ifRoom|withText"
android:title="#string/addPlaylist" />
</menu>
I'm inflating the menu on top of my toolbar in the activity file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBar((android.widget.Toolbar) findViewById(R.id.toolbar));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
And yet the menu items show up in the overflow. In Android Studio's render view the menu looks correct.
If you want me to attach a snapshot I will. I'm running the app on an emulator with API 23 (For now). I don't know if I should include any other files but tell me if you want them. Thanks in advance!
[EDIT] Here is a screenshot of the toolbar:
https://gyazo.com/2743b74a8aee07035b8398f1b41002c4
Here is my styles.xml:
<resources
xmlns="http://schemas.android.com/apk/res-auto">
<color name="card_title_text">#111111</color>
<color name="card_desc_text">#aaaaaa</color>
<color name="action_bar_text">#ffffff</color>
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.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="actionMenuTextColor">#color/action_bar_text</item>
<item name="android:navigationBarColor">#color/black</item>
<item name="android:colorForeground">#color/foreground_material_light</item>
<item name="android:colorBackground">#color/background_material_light</item>
<item name="android:statusBarColor">#color/statusBarColor</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="actionMenuTextColor">#color/action_bar_text</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
I also tried setting app:showAsAction="always" but it didn't work. No visible change.
I'm not sure I understand fully - do you mean you want to see the icons in the toolbar instead of the words in your #string/addPlaylist
If so, you need to change the following line in your menu item:
app:showAsAction="always"

How to change color of the back arrow in the new material theme?

I've updated my SDK to API 21 and now the back/up icon is a black arrow pointing to the left.
I would like it to be grey. How can I do that?
In the Play Store, for example, the arrow is white.
I've done this to set some styles. I have used #drawable/abc_ic_ab_back_mtrl_am_alpha for homeAsUpIndicator. That drawable is transparent (only alpha) but the arrow is displayed in black. I wonder if I can set the color like I do in the DrawerArrowStyle. Or if the only solution is to create my #drawable/grey_arrow and use it for homeAsUpIndicator.
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="homeAsUpIndicator">#drawable/abc_ic_ab_back_mtrl_am_alpha</item>
<item name="android:homeAsUpIndicator" tools:ignore="NewApi">#drawable/abc_ic_ab_back_mtrl_am_alpha</item>
</style>
<!-- ActionBar style -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:background">#color/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#color/actionbar_background</item>
</style>
<!-- Style for the navigation drawer icon -->
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#color/actionbar_text</item>
</style>
My solution so far has been to take the #drawable/abc_ic_ab_back_mtrl_am_alpha, which seems to be white, and paint it in the color I desire using a photo editor. It works, although I would prefer to use #color/actionbar_text like in DrawerArrowStyle.
You can achieve it through code. Obtain the back arrow drawable, modify its color with a filter, and set it as back button.
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
Revision 1:
Starting from API 23 (Marshmallow) the drawable resource abc_ic_ab_back_mtrl_am_alpha is changed to abc_ic_ab_back_material.
EDIT:
You can use this code to achieve the results you want:
toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.blue_gray_15), PorterDuff.Mode.SRC_ATOP);
Looking at the Toolbar and TintManager source, drawable/abc_ic_ab_back_mtrl_am_alpha is tinted with the value of the style attribute colorControlNormal.
I did try setting this in my project (with <item name="colorControlNormal">#color/my_awesome_color</item> in my theme), but it's still black for me.
Update:
Found it. You need to set the actionBarTheme attribute (not actionBarStyle) with colorControlNormal.
Eg:
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="actionBarTheme">#style/MyApp.ActionBarTheme</item>
<item name="actionBarStyle">#style/MyApp.ActionBar</item>
<!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
<item name="colorControlNormal">#color/my_awesome_color</item>
<!-- The animated arrow style -->
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
<style name="MyApp.ActionBarTheme" parent="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- THIS is where you can color the arrow! -->
<item name="colorControlNormal">#color/my_awesome_color</item>
</style>
<style name="MyApp.ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="elevation">0dp</item>
<!-- style for actionBar title -->
<item name="titleTextStyle">#style/ActionBarTitleText</item>
<!-- style for actionBar subtitle -->
<item name="subtitleTextStyle">#style/ActionBarSubtitleText</item>
<!--
the actionBarTheme doesn't use the colorControlNormal attribute
<item name="colorControlNormal">#color/my_awesome_color</item>
-->
</style>
Tried all suggestions above. The only way that I managed to change the color of navigation icon default Back button arrow in my toolbar is to set colorControlNormal in base theme like this. Probably due to the fact that the parent is using Theme.AppCompat.Light.NoActionBar
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">#color/white</item>
//the rest of your codes...
</style>
Need to add a single attribute to your toolbar theme -
<style name="toolbar_theme" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlNormal">#color/arrow_color</item>
</style>
Apply this toolbar_theme to your toolbar.
OR
you can directly apply to your theme -
<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">#color/arrow_color</item>
//your code ....
</style>
Just add
<item name="colorControlNormal">#color/white</item>
to your current app theme.
if use Toolbar,you can try this
Drawable drawable = toolbar.getNavigationIcon();
drawable.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);
As said on most of the previous comments, the solution is to add
<item name="colorControlNormal">#color/white</item>
to your app theme.
But confirm that you donĀ“t have another theme defined in your Toolbar element on your layout.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
The answer by Carles is the correct answer, but few of the methods like getDrawable(), getColor() got deprecated at the time I am writing this answer. So the updated answer would be
Drawable upArrow = ContextCompat.getDrawable(context, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
Following some other stackoverflow queries I found that calling ContextCompat.getDrawable() is similar to
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
And ContextCompat.getColor() is similar to
public static final int getColor(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompatApi23.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}
Link 1: ContextCompat.getDrawable()
Link 2: ContextCompat.getColor()
I found a solution that works pre Lollipop. Set the "colorControlNormal" within the "actionBarWidgetTheme" to change the homeAsUpIndicator color. Modifying rockgecko's answer from above to look like this:
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="actionBarTheme">#style/MyApp.ActionBarTheme</item>
<item name="actionBarStyle">#style/MyApp.ActionBar</item>
<!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
<item name="colorControlNormal">#color/my_awesome_color</item>
<!-- The animated arrow style -->
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<!-- The style for the widgets on the ActionBar. -->
<item name="actionBarWidgetTheme">#style/WidgetStyle</item>
</style>
<style name="WidgetStyle" parent="style/ThemeOverlay.AppCompat.Light">
<item name="colorControlNormal">#color/my_awesome_color</item>
</style>
On compileSdkVersoin 25, you could do this:
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:textColorSecondary">#color/your_color</item>
</style>
Change Menu Nav Icon Color
In style.xml :
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
<style name="DrawerArrowStyle" parent="#style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#color/colorPrimaryDark</item>
</style>
In Mainfests.xml :
<activity android:name=".MainActivity"
android:theme="#style/MyMaterialTheme.Base"></activity>
Use below method:
private Drawable getColoredArrow() {
Drawable arrowDrawable = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
Drawable wrapped = DrawableCompat.wrap(arrowDrawable);
if (arrowDrawable != null && wrapped != null) {
// This should avoid tinting all the arrows
arrowDrawable.mutate();
DrawableCompat.setTint(wrapped, Color.GRAY);
}
return wrapped;
}
Now you can set the drawable with:
getSupportActionBar().setHomeAsUpIndicator(getColoredArrow());
Solution is very simple
Just put following line into your style named as AppTheme
<item name="colorControlNormal">#color/white</item>
Now your whole xml code will look like shown below (default style).
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#0F0F0F</item>
<item name="android:statusBarColor">#EEEEF0</item>
<item name="android:windowLightStatusBar">true</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActivityTransitions">true</item>
<item name="colorControlNormal">#color/white</item>
</style>
Another solution that might work for you is to not declare your toolbar as the app's action bar ( by setActionBar or setSupportActionBar ) and set the back icon in your onActivityCreated using the code mentioned in another answer on this page
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
toolbar.setNavigationIcon(upArrow);
Now, you will not get the onOptionItemSelected callback when you press the back button. However, you can register for that using setNavigationOnClickListener. This is what i do:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().onBackPressed(); //or whatever you used to do on your onOptionItemSelected's android.R.id.home callback
}
});
I'm not sure if it will work if you work with menu items.
This is what worked for me:
Add the below attributes to your theme.
To change toolbar/actionbar back arrow above api 21
<item name="android:homeAsUpIndicator">#drawable/your_drawable_icon</item>
To change toolbar/actionbar back arrow below api 21
<item name="homeAsUpIndicator">#drawable/your_drawable_icon</item>
To change action mode back arrow
<item name="actionModeCloseDrawable">#drawable/your_drawable_icon</item>
To remove toolbar/actionbar shadow
<item name="android:elevation" tools:targetApi="lollipop">0dp</item>
<item name="elevation">0dp</item>
<!--backward compatibility-->
<item name="android:windowContentOverlay">#null</item>
To change actionbar overflow drawable
<!--under theme-->
<item name="actionOverflowButtonStyle">#style/MenuOverflowStyle</item>
<style name="MenuOverflowStyle" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="srcCompat">#drawable/ic_menu_overflow</item>
Note : 'srcCompat' is used instead of 'android:src' because of vector drawable support for api below 21
Here's how I did it in Material Components:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="drawerArrowStyle">#style/AppTheme.DrawerArrowToggle</item>
</style>
<style name="AppTheme.DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">#android:color/white</item>
</style>
We were running into the same problem and all we wanted was to set the
app:collapseIcon
attribute in the toolbar in the end, which we did not find since it is not very well documented :)
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbarHeight"
app:collapseIcon="#drawable/collapseBackIcon" />
try this
public void enableActionBarHomeButton(AppCompatActivity appCompatActivity, int colorId){
final Drawable upArrow = ContextCompat.getDrawable(appCompatActivity, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);
android.support.v7.app.ActionBar mActionBar = appCompatActivity.getSupportActionBar();
mActionBar.setHomeAsUpIndicator(upArrow);
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(true);
}
function call:
enableActionBarHomeButton(this, R.color.white);
I just changed the toolbar theme to be #style/ThemeOverlay.AppCompat.Light
and the arrow became dark gray
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
app:layout_collapseMode="pin"
app:theme="#style/ThemeOverlay.AppCompat.Light">
The simplest (and best) way to change the color of the back/up-arrow. Best part is
that there are no side-effects (unlike the other answers)!
Create a style with parent Widget.AppCompat.DrawerArrowToggle, define the color and any other attributes you'd like.
Set the style on the drawerArrowStyle attribute in the apps Theme.
Create style:
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<!-- Set that the nav buttons will animate-->
<item name="spinBars">true</item>
<!-- Set the color of the up arrow / hamburger button-->
<item name="color">#color/white</item>
</style>
Set the style in App Theme:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Change global up-arrow color with no side-effects -->
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
To change the ToolBar text color (and other attributes), create this style:
<style name="ToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:textColor">#color/white</item>
<item name="titleTextColor">#color/white</item>
<item name="colorControlNormal">#color/white</item> <!-- colorControlNormal is Probably not necessary -->
</style>
Then set that style on the AppTheme:
<!-- Base application theme. -->
<style name="AppTheme.MyProduceApp" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Style the toolbar (mostly the color of the text) -->
<item name="toolbarStyle">#style/ToolbarStyle</item>
<!-- Change color of up-arrow -->
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
You can change the color of the navigation icon programmatically like this:
mToolbar.setNavigationIcon(getColoredArrow());
private Drawable getColoredArrow() {
Drawable arrowDrawable = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
Drawable wrapped = DrawableCompat.wrap(arrowDrawable);
if (arrowDrawable != null && wrapped != null) {
// This should avoid tinting all the arrows
arrowDrawable.mutate();
DrawableCompat.setTintList(wrapped, ColorStateList.valueOf(this.getResources().getColor(R.color.your_color)));
}
}
return wrapped;
}
private fun setToolbarNavigationIconColor(context: Context, toolbar: Toolbar?, color: Int) {
val drawableBack = toolbar?.navigationIcon
drawableBack?.let {
val drawableWrap = DrawableCompat.wrap(drawableBack).mutate()
DrawableCompat.setTint(
drawableWrap, ContextCompat.getColor(context, white)
)
toolbar.navigationIcon = drawableWrap
}
}
I saw all soluation but didn't fix the problem , If you need fix the problem just add in XMl file this code if you but dark make it light
app:theme="#style/ThemeOverlay.AppCompat.Light"
Just remove android:homeAsUpIndicator and homeAsUpIndicator from your theme and it will be fine. The color attribute in your DrawerArrowStyle style must be enough.
Unless there's a better solution...
What I did is to take the #drawable/abc_ic_ab_back_mtrl_am_alpha images, which seem to be white, and paint them in the color I desire using a photo editor.

Categories

Resources