RyanJohnstone RyanJohnstone - 1 month ago 24
Android Question

Navigation Drawer item background colour for selected item

I used Android Studio to implement the Navigation Drawer and I can't get the blue colour that is used to show which section we're currently in to change.

I've tried numerous things, I'm currently using a listSelector which looks like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_activated="true" android:drawable="@color/selected" />
<item android:state_pressed="true" android:drawable="@color/highlight" />


</selector>


I've also tried
state_checked
.
state_pressed
works in this situation but the currently selected item is still blue.

EDIT:
I've been examining this more and when the adapter is created the context that is passed is
getActionBar().getThemedContext()
so I'm thinking if I can find the right attribute to assign to my actionbar style I can change it from there. I've tried a few different attributes with no luck. Does anyone know the exact attribute?

I've also realised if I put

<item name="android:activatedBackgroundIndicator">@drawable/nav_listview_selector</item>


in the main part of my theme and change
getActionBar().getThemedContext()
for
getActivity.getBaseContext
then I can change the color but I don't think this is the correct way. I think the themed context should be used. So if anyone knows where the
activatedBackgroundIndicator
could be put so that it would be used in
getActionBar.getThemedContext()


EDIT2:

So the text view used for the listview is one within the SDK it looks like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>


So I tried modifying the
"?android:attr/activatedBackgroundIndicator"
at the theme level but it has no effect for checked/selected/activated but it does for pressed. Does anyone know why this is? And how I can change it?

Answer

Still not sure why it is that it doesn't work. But the way I found around it is to use my own simple_list_item_activated layout to be passed to the ArrayAdapter which was basically the same except for setting the text colour to white. I then replaced getActionBar().getThemedContext() with getActivity().getBaseContext() and it now has an effect.

This may not be the correct way and may have repercussions in the future, but for now I have it working the way I want it to.

Comments