0

I want to change my navigation drawer list item's background colour to be changed when i click on it. Also it should change the text and icon colour of that item as well. Thanks in advance..

aditi
  • 21
  • 7
  • 2
    Possible duplicate of [Navigation Drawer item background colour for selected item](https://stackoverflow.com/questions/22374660/navigation-drawer-item-background-colour-for-selected-item) – RediOne1 Oct 08 '18 at 13:39

3 Answers3

0

This is quite simple and is similar to changing the background of any view that you use. You can simply create a method and write all the change code into it. Pass the view as a parameter in the method.

Whenever you click on any of the navigation drawer items, pass your view on the method.

For instance, check this method

private boolean viewSelected(View view) {
    ViewHolder currentViewHolder = (ViewHolder) view.getTag();
    KTextView yourtextView = currentViewHolder.yourtextView;
    view.setBackgroundColor(ResourceUtil.getInstance().getColor(R.color.colorSideMenuSelectedBackground));
    currentViewHolder.viewSelector.setVisibility(View.VISIBLE);
    yourtextView.setTypeface(yourtextView.getContext(), ResourceUtil.getInstance().getString(R.string.yourFontName));

    if (lastSelectedView == null) {
        lastSelectedView = view;
        return true;
    }
    if (lastSelectedView != view) {
        ViewHolder oldViewHolder = (ViewHolder) lastSelectedView.getTag();
        oldViewHolder.viewSelector.setVisibility(View.GONE);
        lastSelectedView.setBackgroundColor(ResourceUtil.getInstance().getColor(android.R.color.white));
        KTextView newTextView = oldViewHolder.yourtextView;
        newTextView.setTypeface(yourtextView.getContext(), ResourceUtil.getInstance().getString(R.string.yourfontname));
        lastSelectedView = view;
        return true;
    }
    return false;
}

This method will simply change the background, font and color of the views.

Hope this helps!

0

This can be done using https://developer.android.com/guide/topics/resources/color-list-resource.

Create two drawable files : 1.drawer_background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked_background_color" android:state_checked="true" />
<item android:color="default_background_color" />
</selector>

2.drawer_text_background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked_text_color" android:state_checked="true" />
<item android:color="default_text_color" />
</selector>

And add these two properties to your NavigationView ie

app:itemIconTint="@drawable/drawer_background"
app:itemTextColor="@color/drawer_text_background"

one more if some color is overlapping with some other color

  app:itemBackground="@android:color/transparent"

Now, All you have to do is to set an item as checked on Click listener of that item to change background and text color. You can do it programmatically.

Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37
0

I solved it setting this attribute to my NavigationView app:itemBackground="@drawable/drawer_selector"

and my drawer_selector is as below

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/white" />
<item android:state_focused="true" android:drawable="@color/white" />
<item android:state_activated="true" android:drawable="@color/white" />
<item android:drawable="@color/orange" />

aditi
  • 21
  • 7