Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected item in navigation drawer

I implemented the navigation drawer in my app (http://developer.android.com/training/implementing-navigation/nav-drawer.html).

How can I change the display of the actually selected item? I want the selected item to be bold and a different color.

In the official example, they use "setItemChecked", but how can I modify the aspect of the single checked item? The "setOnItemSelectedListener" method doesn't work.

So far, what I do is this piece of code when I open a fragment:

for (int i = 0; i < mDrawerList.getChildCount(); i++){
    TextView t = (TextView) mDrawerList.getChildAt(i);

    if(i == fragmentPosition) t.setTextColor(getResources().getColor(R.color.blue));
    else t.setTextColor(getResources().getColor(R.color.black));
}

It works, but on app launch, no item is selected, and I can't set any item because the listview isn't created yet (it's created when we open it the first time, I think).

I tried creating a selector, but don't know how to set the different attributes correctly (item checked, item selected, item clicked??), searched the docs but I don't understand.

Thanks for any help you can provide!

like image 510
Chris B. Avatar asked Jan 04 '14 20:01

Chris B.


People also ask

How to use navigation drawer in android app?

Add a navigation drawerThe drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen. Figure 3. An open drawer displaying a navigation menu. The drawer icon is displayed on all top-level destinations that use a DrawerLayout .

Which method is used to handle click on the menu items of the navigation view?

You have to use OnNavigationItemSelectedListener(MenuItem item) method.

How do I get navigation drawer in all activity?

The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.


2 Answers

I would do it in adapter.

mAdapter.setSelectedItem(position);

public View getView(...) {
    ...
    if (position == mSelectedItem) {
        text.setTypeface(...);
        text.setBackgroundColor(...);
    } else {
        text.setTypeface(...);
        text.setBackgroundColor(...);
    }
}

And if you don't need to set typeface you can use selector with

setTextColor(@drawable/my_selector).

Or put it to your xml file with TextView

android:textColor="@drawable/my_selector"
like image 136
wnc_21 Avatar answered Oct 02 '22 19:10

wnc_21


Here's my solution to change the color and typeface of the selected item in the navigation drawer...

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
     setNavDrawerItemNormal();
     TextView txtview = ((TextView) view.findViewById(R.id.txtNav));
     txtview.setTypeface(null, Typeface.BOLD);
     txtview.setTextColor(R.color.Red);
}

public void setNavDrawerItemNormal()
{
    for (int i=0; i< mDrawerListView.getChildCount(); i++)
    {
        View v = mDrawerListView.getChildAt(i);
        TextView txtview = ((TextView) v.findViewById(R.id.txtNav));
        Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf");
        txtview.setTypeface(font);
        txtview.setTextColor(R.color.Red);
    }
}

And to bold the first item in the navigation drawer upon initialization of the app, I did that in the list adapter get view method...

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.navdrawer_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtNav);
    textView.setText(values[position]);
    if (position == 0) 
    { 
       textView.setTypeface(null, Typeface.BOLD);
       textView.setTextColor(R.color.Red);
    }

    return rowView;
}

So, in here i checked if the position of the item is 0 (meaning its the first item), then make it bold. This whole thing works perfectly for me!~

like image 23
Abdul Rahman Avatar answered Oct 02 '22 19:10

Abdul Rahman