How to change icon and text color in TextView for navigationDrawer?

I'm trying to follow the material design guidelines, but don't know how to change the icon and text color for the navigation drawer.

(Scroll down to select state): http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-content

This is from my MainActivity (in onCreate ()):

    protected void onCreate(Bundle savedInstanceState) {
    ...
    ListAdapter adapter = new MyAdapter(this, menu, imageID);
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mAdapter.setSelectedItem(position);
        }
    });
   ...
   }

      

from MyAdapter ():

public class MyAdapter extends ArrayAdapter<String>{

private Integer[] imageID;
private int mSelectedItem;

public MyAdapter(Context context, String[] menu, Integer[] imageID) {
    super(context, R.layout.row_layout, menu);
    this.imageID = imageID;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());

    View view = inflater.inflate(R.layout.row_layout, parent, false);

    String menu = getItem(position);
    TextView textView = (TextView) view.findViewById(R.id.textView1);
    textView.setText(menu);

    if (position == getSelectedItem()) {
        textView.setTextColor(getContext().getResources().getColor(R.color.primaryColor));
    }
    else {
        textView.setTextColor(getContext().getResources().getColor(R.color.navDrawerTextColor));
    }

    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
    imageView.setImageResource(imageID[position]);

    return view;
}

public int getSelectedItem() {
    return mSelectedItem;
}

public void setSelectedItem(int selectedItem) {
    mSelectedItem = selectedItem;
}

      

}

and my TextView from xml file:

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:fontFamily="sans-serif-medium"
    android:paddingStart="72dp"
    android:paddingEnd="16dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/color_selector"
    android:textSize="14sp" />

      

Any help is appreciated, thanks in advance!

EDIT: I used color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primaryColor" android:state_pressed="true" />
    <!-- pressed -->
    <item android:color="@color/primaryColor" android:state_selected="true" />
    <!-- selected -->
    <item android:color="#DE000000" />
    <!-- default -->
</selector>

      

But when an item is selected, it won't stay in that color for long

+3


source to share


1 answer


You should be using NavigationView , part of the Android Design Support Library , that does all of the item selection and styling of the navigation drawer for you.



0


source







All Articles