user6798766
user6798766

Reputation: 65

How to put icons for items in navigation drawer

I am devleoping an app in which i am using navigation drawer,in that when i am trying to put icon for items it is giving me the path fof the image like this: " res/drawale/bg.jpg". I am not getting how to do that.

Below is my code for main activity:

 private void init_navigator() {
    // Navigation Drawer
    mTitle = mDrawerTitle = getTitle();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark));
    mScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.main_activity_navigation_drawer_rootLayout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerItmes = getResources().getStringArray(R.array.drawer_titles);
  mDrawerItmes = getResources().getStringArray(R.array.nav_drawer_icons);

Here is my code for string.xml

   <string-array name="drawer_titles">
    <item>About Us</item>
    <item>FeedBack</item>
    <item>Setting</item>
    <item>Share App</item>
    <item>Rate Us</item>
    <item>Logout</item>
</string-array>

<array name="nav_drawer_icons">
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/b</item>
    <item>@drawable/bg</item>

</array>

Upvotes: 3

Views: 4140

Answers (2)

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

Just try to follow this way,this is exactly what you looking for

http://www.journaldev.com/9958/android-navigation-drawer-example-tutorial

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

public class DrawerItemCustomAdapter extends ArrayAdapter<DataModel> {

    Context mContext;
    int layoutResourceId;
    DataModel data[] = null;

    public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, DataModel[] data) {

        super(mContext, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItem = convertView;

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        listItem = inflater.inflate(layoutResourceId, parent, false);

        ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
        TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

        DataModel folder = data[position];


        imageViewIcon.setImageResource(folder.icon);
        textViewName.setText(folder.name);

        return listItem;
    }
}

Upvotes: 0

Vladimir Marton
Vladimir Marton

Reputation: 576

In your navigationview in xml add this:

app:menu="@menu/drawer_menu_icons"

Then create a new xml called drawer_menu_icons in menu folder and do this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/homegroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/homeicon"
            android:title="Main screen" />
        <item
            android:id="@+id/nav_branches"
            android:icon="@drawable/backspace"
            android:title="Branch selection" />

    </group>
</menu>

And add whatever icons you want :)

Upvotes: 3

Related Questions