Custom background color for selected item with "activatedBackgroundIndicator" Navigation Drawer Custom background color for selected item with "activatedBackgroundIndicator" Navigation Drawer android android

Custom background color for selected item with "activatedBackgroundIndicator" Navigation Drawer


I have run into this exact problem. The issue is that the background of R.layout.fragment_navigation_drawer_list_item is what needs to be changed to your drawable. Simply add android:background="@drawable/activated_background" to the layout.


I wasn't able to find any full clear answer to this question, so here it is:

Step 1. Specify the list item layout your adapter will use, in this example we're specifying:R.layout.fragment_navigation_drawer_list_item

Like so:

mDrawerListView.setAdapter(new ArrayAdapter<String>(        // First parameter - Context        getActionBar().getThemedContext(),        // Second parameter - Layout for the row        R.layout.fragment_navigation_drawer_list_item,        // Third parameter - ID of the TextView to which the data is written        android.R.id.text1,        // Forth - the Array of data        new String[]{                getString(R.string.title_section1),                getString(R.string.title_section2),                getString(R.string.title_section3),                getString(R.string.title_section4),        }))`

Step 2. Create and customize fragment_navigation_drawer_list_item.xml to specify a drawable that will have a selector like so: android:background="@drawable/activated_background" Full example:

<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:minHeight="?android:attr/listPreferredItemHeightSmall"    android:background="@drawable/activated_background" />

Step 3. Create and customize the selector in your activated_background.xml file just like in the question, it will look like this:

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