Reputation: 1767
I have a ListView and inside a DrawerLayout and the background is being applied through a selector, I have specified background color for selected item but that color is not being applied, A green color (no idea where its coming from, its not in my colors.xml or anywhere)
Here is what I have
menuitem_style.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/mainColor"/>
<!--mainColor is #4CAEE3-->
<item
android:drawable="@android:color/white" />
The ListView:
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/menuitem_style"
android:choiceMode="singleChoice"
android:divider="@android:color/white"
android:dividerHeight="1dp"
android:paddingTop="10dp" />
related Java code
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
This is the outcome:
The mainColor is definitely blue and not green (#4CAEE3) I am doing something wrong, not sure what. any help will be apprciated. Thanks in advance
Upvotes: 1
Views: 325
Reputation: 4121
Keep in mind the concept that in list views and recyclerview the views are recycled.
You can keep track the position of the current selected element:
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
And override the getView method of your adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}
For me it did the trick.
Upvotes: 2
Reputation: 4121
android:listSelector="@drawable/listitem_selector"
<?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/black"/>
<item android:state_enabled="true" android:drawable="@color/gray"/>
</selector>
try this one. It may help you.
Upvotes: 0
Reputation: 4678
Use this background as textview background rather listview background. Go to the row xml file you are using for list view populating and set the background there. Hope that helps you.
Upvotes: 0