How can I change the color of the selected list in the navigation drawer?
I am starting with the Android Studio Browser Browser example. I customize whatever I want, everything is fine, but I cannot find how to change the selected color of the item in the navbox ... It is green at the moment and I cannot manage to change that.
I tried with the selected selector but couldn't, I was able to change the entire background, but not just the selected one.
Sorry if this is a noob question, but I spent hours yesterday on Google and here without finding: /
Finally I managed to get it to work. The problem was "android.R.layout.simple_list_item_activated_1".
This is android built in xml so you can't deal with the colors you want.
So, I created a file named nav_drawer_layout.xml
that contains the same as android.R.layout.simple_list_item_activated_1.
Then I changed it by changing the android: background attribute as follows:
<?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 selector nav_drawer_colors.xml
which 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, press_drawer and backgroung_drawer are defined in the \ colors.xml values as always.
Everything is fine now, except for the drop shadow effect, which works all over the place, but not on the list. ^^
EDIT: Just specify the background color in your fragment drawer navigator layout (fragment_navigation_drawer.xml) and remove
<item android:drawable="@color/backgroung_drawer" />
in file nav_drawer_colors.xml
:)
Hope this helps others who have the same problem :)
source to share
Through coding, you can achieve this when clicking the ListView item:
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 the selector:
Make one file named select.xml
in a folder drawable
.
<?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 as Listselector for ListView in xml file. How,
android:listselector="@drawable/select"
or use as background also
android:background="@drawable/select"
source to share
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" />
source to share
Try the following:
1.Create list_selector.xml in a portable 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 your custom layout listview.
android:background="@drawable/list_selector"
source to share