I have a color by default in my XML, and I would like to change it programmatically. I don't know how to access to the highlight_color item color in the highlight_color.xml programmatically...
main java code:
//BEGIN TEST COLOR
ColorStateList iconsColorStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.parseColor("#000000"),
Color.parseColor("#ffffff")
});
ColorStateList textColorStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.parseColor("#000000"),
Color.parseColor("#ffffff")
});
//color ec5d60
bottomNavigation.setItemIconTintList(iconsColorStates);
bottomNavigation.setItemTextColor(textColorStates);
main XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LocalNav">
<WebView
android:id="@+id/vieweb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:layout_marginBottom="-2dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.NewTelApps.ToEvent.newBottomNav
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
app:itemIconSize="45dp"
android:visible="false"
app:itemBackground="@drawable/nav_item_drawable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
nav_item_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/highlight_color" android:state_checked="true"/>
</selector>
highlight_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ec5d60"/>
</shape>
EDIT:
This two piece of codes (took from the different answers) change well the color, but the display is not good:
1/
Drawable drawable = AppCompatResources.getDrawable(context,
R.drawable.nav_item_drawable);
drawable.setColorFilter(getResources().getColor(android.R.color.holo_green_light), PorterDuff.Mode.MULTIPLY);
bottomNavigation.setItemBackground(drawable);
2/
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context,
R.drawable.nav_item_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.GREEN);
Before adding this piece of code:
After adding this piece of code:
EDIT2:
I finally find the way to correct the problem. I simply had to put the piece of code in a loop just before the color item tint and text. Here is my full code:
Main Java class
int itemSelected = this.getSelectedItem(this);
Log.d("Item Selected :", String.valueOf(itemSelected));
Log.d("Number of items :", String.valueOf(this.getMenu().size()));
if (initialization == 0)
{
this.getMenu().setGroupCheckable(0, true, false);
for (int j = 0; j < this.getMenu().size(); j++) {
this.getMenu().getItem(j).setChecked(false);
}
this.getMenu().setGroupCheckable(0, true, true);
}
final ViewGroup bottomMenu = (ViewGroup)getChildAt(0);
final int bottomMenuChildCount = bottomMenu.getChildCount();
BottomNavigationItemView item = null;
View itemTitle;
for(int i=0; i<bottomMenuChildCount; i++){
item = (BottomNavigationItemView)bottomMenu.getChildAt(i);
//this shows all titles of items
item.setChecked(true);
//every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle
itemTitle = item.getChildAt(1);
//every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView
((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTextSize(10);
((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setGravity(Gravity.CENTER_HORIZONTAL);
((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTextSize(10);
((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setGravity(Gravity.CENTER_HORIZONTAL);
Drawable drawable = AppCompatResources.getDrawable(context,
R.drawable.nav_item_drawable);
drawable.setColorFilter(getResources().getColor(android.R.color.holo_green_light), PorterDuff.Mode.SRC_ATOP);
item.setItemBackground(drawable);
}
//BEGIN TEST COLOR
ColorStateList iconsColorStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.parseColor("#000000"),
Color.parseColor("#ffffff")
});
ColorStateList textColorStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.parseColor("#000000"),
Color.parseColor("#ffffff")
});
//color ec5d60
bottomNavigation.setItemIconTintList(iconsColorStates);
bottomNavigation.setItemTextColor(textColorStates);
Thanks in advance.