Search code examples
androidandroid-arrayadapternavigation-drawertextcolor

Navigation drawer selected item textColor by Custom Adapter


May i know how to change the selected item's text color in navigation drawer.

The android:textColor for listview is not working after install appcompat_v7 22.1.1 so i think i cannot change using selector drawable.

I manage to change the default color of drawer items by using custom adapter.

But this only manage to set the first item to green when initialized.

May i know how to change the color of selected items(onclick) to the color i want when item is selected.

Adapter:

public class NavigationDrawerAdapter extends ArrayAdapter<String> {
    private int mSelectedItem;
    private final Context context;
    private String[] sections;

    public NavigationDrawerAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
        this.context = context;
        sections = objects;
    }
    public int getSelectedItem() {
        return mSelectedItem;
    }
    public void setSelectedItem(int selectedItem) {
        mSelectedItem = selectedItem;
    }

    private String getMenuItemTitle(int position) {
        if (position >= 0 && position <= sections.length)
            return sections[position];
        return "Invalid section requested";
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Get item TextView
        TextView view = (TextView) super.getView(position, convertView, parent);

        view.setText(getMenuItemTitle(position));

        if (position == mSelectedItem) {
            view.setTextColor(getContext().getResources().getColor(R.color.green));
        } else {
            view.setTextColor(getContext().getResources().getColor(R.color.white));        }
        return view;
    }
}

Navigation drawer:

mDrawerListView.setAdapter(new NavigationDrawerAdapter(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                new String[]{
                        getString(R.string.title_home),
                        getString(R.string.title_stamp),
                        getString(R.string.title_scanner),
                        getString(R.string.title_setting),}) {

        });
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
..
...
private void selectItem(int position) {
        mCurrentSelectedPosition = position;

        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }

Solution

  • I had found the solution.

    Since i have extend the adapter. I'm just need to call and change the selected Item by position when item clicked to make changes.

    ListDrawer onItemClickListener:

     mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectItem(position);
    
                    myadapter.setSelectedItem(position);
    
                }
    

    And From the custom adapter:

    public void setSelectedItem(int selectedItem) {
            mSelectedItem = selectedItem;
        }
    
        private String getMenuItemTitle(int position) {
            if (position >= 0 && position <= sections.length)
                return sections[position];
            return "Invalid section requested";
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //Get item TextView
          TextView view = (TextView) super.getView(position, convertView, parent);
    
            view.setText(getMenuItemTitle(position));
    
            if (position == mSelectedItem) {
                view.setTextColor(getContext().getResources().getColor(R.color.green));
            } else {
                view.setTextColor(getContext().getResources().getColor(R.color.normal));
            }
    
            return view;
        }
    

    *adapter class is on question.

    source : samsaodev.com