sirhclau
sirhclau

Reputation: 33

How can I change the default blue color of a selected item in Navigation drawer

I tried to use android:listSelector, but it works only when I click on the listview cell, how can I change the highlight color of a selected cell, so that the cell will be stay highlighted with that color? Many thanks.

highlight in blue

highlight in orange

Upvotes: 2

Views: 5832

Answers (2)

SpyZip
SpyZip

Reputation: 5540

add this to your layout you use to show the elements of the listview

android:background="?android:attr/activatedBackgroundIndicator" 

Create a file res/drawable/my_background.xml with the following content :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/blue" />
    <item android:drawable="@android:color/transparent" />
</selector>

Replace the @color/blue with the color of your choice. Then, in your theme, add the following line :

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

Upvotes: 16

kiwi
kiwi

Reputation: 487

you may need to implement your own adapter in this case, and return your custom layout which background color is set to "highlight in orange". Something like this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View your_custom_view = null;

        if (convertView == null) {
            your_custom_view = LayoutInflater.from(context).inflate(R.layout.your_custom_view_layout, null);
        } else {
            your_custom_view = convertView;
        }

        return your_custom_view;
    }

Upvotes: 0

Related Questions