1

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:

Before

After adding this piece of code:

After

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.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • Hi, do you want to change the color of the selected item of the BottomNavView when it's selected to have a certain color, and to have another color when it's non-selected? – Zain Nov 17 '20 at 20:43
  • @Zain hi! I try to have a specific color for the selected background item – ΩlostA Nov 17 '20 at 20:45
  • I think using `app:itemBackground="@drawable/nav_item_drawable"` is sufficient, doesn't it change the background color of the checked item? – Zain Nov 17 '20 at 21:19
  • @Zain yep it works but my problem is that I dynamically change it by downloading the properties from the web. So the color will change dynamically. – ΩlostA Nov 17 '20 at 22:04

1 Answers1

1

You can change the background color programmatically as:

Drawable drawable = AppCompatResources.getDrawable(context,
        R.drawable.nav_item_drawable);
drawable.setColorFilter(getResources().getColor(android.R.color.holo_red_dark), PorterDuff.Mode.MULTIPLY);
bottomNavigationView.setItemBackground(drawable);

And keep the app:itemBackground="@drawable/nav_item_drawable" of your BottomNavigationView

Hope it targets it!

Zain
  • 37,492
  • 7
  • 60
  • 84
  • it changes the color, but the problem is that the display is not displaying for each items. Very strange. I edit my ticket. – ΩlostA Nov 18 '20 at 14:47
  • 1
    Just saw ur updates in question.. Thanks for sharing them – Zain Nov 18 '20 at 17:39
  • 1
    I would like to validate your response, but the first one were done first, I'm sorry (he's a newb, I would like to give him some points ;) ). Thanks for your attention and answer, it helped me to insist that way. The result is perfect. – ΩlostA Nov 18 '20 at 17:40