2

I want to change the default color (blue) of a selected item in a Navigation Drawer in Android. I have gotten it to work in past projects, but I cannot in the project I am currently working on.

This is the app theme.

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    <item name="android:buttonStyle">@style/AppTheme.Button</item>
</style>

This is the activated_background drawable.

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@drawable/list_selected"/>
    <item android:state_selected="true" android:drawable="@drawable/list_selected"/>
    <item android:state_pressed="true" android:drawable="@drawable/list_selected"/>
</selector>

Finally, this is the navigation drawer fragment.

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
tools:context=".NavigationDrawerFragment" />

Like I said, this has worked for me before. The only difference between projects that I can think of is that before, my app theme was inherited from "Theme.AppCompat" and this project's theme is inherited from "android:Theme.Holo.Light.DarkActionBar". Also, I am using v4 support fragments in my new project, while as in the other project I was just using the standard fragment.

Any advice would be greatly appreciated. Thanks.

gdvander
  • 305
  • 6
  • 15

2 Answers2

1

Edit:

Seems like the question is how to keep an item in a ListView selected, not actually how to set the resulting background of a selected item.

See this great answer for how change the background of a ListView item once it is selected:

Android - Keep ListView's item highlighted once one has been clicked

The basic idea is that when the item is tapped, you change it's background (or whatever else). The tricky part is you must have your List adapter remember the row that is selected. Otherwise when/if you scroll the list/navigation drawer, you'll lose the selection.

Original Answer:

You need to set a custom ListView style in AppTheme in which you set the android:listSelector item to @drawable/activated_background.

If you don't want that style for your entire app, just specify that style for the ListView in your application drawer layout, or even more simply just set the android:listSelector property of that ListView and don't even bother defining a style.

Community
  • 1
  • 1
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
1

I figured it out. Instead of using my theme to try and apply the style, I just created my own xml layout for the list items instead of using the default and set the selector from there.

Here is what the list item xml looks like.

<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:textColor="@drawable/list_txtcolor"
  android:gravity="center_vertical"
  android:paddingStart="?android:listPreferredItemPaddingStart"
  android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
  android:background="@drawable/activated_background"
  android:minHeight="?android:attr/listPreferredItemHeightSmall">
</TextView>

As you can see, I just set the item background to my selector.

android:background="@drawable/activated_background"

And again, here is my selector.

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_activated="true" android:drawable="@drawable/list_selected"/>
  <item android:state_selected="true" android:drawable="@drawable/list_selected"/>
  <item android:state_pressed="true" android:drawable="@drawable/list_selected"/>
</selector>
gdvander
  • 305
  • 6
  • 15