Skip to content Skip to sidebar Skip to footer

Changing Navigation Drawer Selected Item Color From Default Blue

Hi and thanks for reading. I have a problem with my android apps navigation drawer where I cannot change the color from blue - I have went over all the other questions from SO, re

Solution 1:

First, I would try removing the android:listSelector attribute, as I don't believe it is necessary.

Next, I would double check you have all these steps:

  • In your application's theme, try adding

themes.xml

<stylename="Theme.mytheme"parent="android:Theme.Holo"><itemname="android:activatedBackgroundIndicator">@drawable/activated_background</item></style>
  • The drawable should refer to a file containing a selector like (like you have)

activated_background.xml

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@color/my_color"android:state_activated="true" /><itemandroid:drawable="@android:color/transparent" /></selector>

colors.xml

<resources><itemname="my_color"type="color">#ff0000</item></resources>
  • Finally, make sure you are applying the theme in your manifest's application tag using

AndroidManifest.xml

android:theme="@style/Theme.mytheme"

Solution 2:

ListDrawer onItemClickListener:

final CustomListAdapter myadapter;
     mDrawerListView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
        @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
            myadapter.setSelectedItem(position);
        }

In custom adapter:

publicclassCustomListAdapterextendsArrayAdapter<String> {
    privatefinal Activity context;
    privatefinal String[] itemname;

    int mSelectedItem;

    publicCustomListAdapter(Activity context, String[] itemname ) {
        super(context, R.layout.drawer_list_item, itemname);
        // TODO Auto-generated constructor stubthis.context = context;
        this.itemname = itemname;

    }

    publicvoidsetSelectedItem(int selectedItem) {
        this.mSelectedItem = selectedItem;
    }

    public View getView(finalint position, View view, ViewGroup parent) {
        LayoutInflaterinflater= context.getLayoutInflater();
        ViewrowView= inflater.inflate(R.layout.drawer_list_item, null, true);
        TextViewtxtTitle= (TextView) rowView.findViewById(R.id.textitem);
        Stringiname= itemname[position];
        txtTitle.setText(iname);
        txtTitle.setTypeface(tf);
        if (position == mSelectedItem) {
            txtTitle.setTextColor(getContext().getResources().getColor(R.color.white));
        } else {
            txtTitle.setTextColor(getContext().getResources().getColor(R.color.normal));
        }
        return rowView;
    }
}

In colors.xml

<resources><colorname="white">#ffffff</color><colorname="normal">#ef3272</color></resources>

Post a Comment for "Changing Navigation Drawer Selected Item Color From Default Blue"