Shoyun
Shoyun

Reputation: 98

How to Change the list selected item color in the navidgation drawer?

I am starting from the Android Studio example of navigation browser. I'm customizing everything i want, all is ok but i can't find how to change the selected item color in the navigation drawer... For now it's green and i don't manage to change that.

I tried with a selector drawable but no way, i managed to change the whole background but not juste the selected one.

Sorry if it's a noob question but i spent hours yesterday on google and here without finding :/

Green color i want to change


Finally, i managed to make it work. The problem was the "android.R.layout.simple_list_item_activated_1".

It's an android built in xml so you can't deal with colors you want.

So i created a file named nav_drawer_layout.xml containing the same as the "android.R.layout.simple_list_item_activated_1" file.

Then i tweaked it changing the android:background attribute like this:

<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/nav_drawer_colors"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

It refers to the the nav_drawer_colors.xml selector that you have to create:

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

selected_drawer, pressed_drawer and backgroung_drawer are defined in the values\colors.xml as always.

Now all is ok, except the shadow effect that works everywhere but not in the listview ^^ EDIT: just specify a background color in the fragment navigation drawer layout (fragment_navigation_drawer.xml) and remove the <item android:drawable="@color/backgroung_drawer" /> in the nav_drawer_colors.xml file :)

Hope that it will help others who have the same problem :)

Upvotes: 3

Views: 10149

Answers (3)

RockStar
RockStar

Reputation: 409

Through coding you can achieve by this on item click of ListView:

int save = -1;
listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    parent.getChildAt(position).setBackgroundColor(
                            Color.parseColor("#A9BCF5"));

                    if (save != -1 && save != position) {
                        parent.getChildAt(save).setBackgroundColor(
                                Color.parseColor("#d6e6ff"));
                    }

                    save = position;
          });

Using Selector:

Make one file named select.xml in drawable folder.

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

Now set this file to as a Listselector for ListView in xml file. Like,

android:listselector="@drawable/select"

or use as a background too

android:background="@drawable/select"

Upvotes: 6

kalidoss rajendran
kalidoss rajendran

Reputation: 640

Try this:

1.Create list_selector.xml in drawable folder

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

2.Then set list_selector.xml as background in listview custom designed layout.

android:background="@drawable/list_selector"

Upvotes: 1

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Create xml file in drawble

list_selector.xml

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

</selector>

list_item_bg_normal.xml

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

  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />

</shape>

list_item_bg_pressed.xml

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

 <gradient

      android:startColor="#aed200"

      android:endColor="#aed200"

      android:angle="90" />

</shape>


<ListView
        android:id="@+id/listview_drawer"
        android:layout_width="match_parent"
         android:layout_gravity="start"
        android:layout_height="match_parent"

        android:background="@drawable/list_selector"
        android:dividerHeight="1dp" />

Upvotes: 2

Related Questions