3

I cannot get my drawer to show images, even though they are being set in the drawer adapter. It shows the text just fine, so it's not a problem of drawing it all, it's just setting the icon.

This is the code for the adapter:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v==null){
        v=LayoutInflater.from(getContext()).inflate(R.layout.drawer_item, null);
    }
    DrawerPair dp = getItem(position);
    TextView description = (TextView) v.findViewById(R.id.drawer_description);
    if(description!=null){
        description.setText(dp.title);
        description.setCompoundDrawables(dp.icon, null, null, null);
    }
    return v;
}

And the layout is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="right"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <TextView 
        android:id="@+id/drawer_description"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

And this is how it's set. The icons are png images:

mDrawerListView.setAdapter(new DrawerAdapter(
        getActionBar().getThemedContext(),
        R.layout.drawer_item,
        new DrawerPair[]{
                new DrawerPair("title1",getResources().getDrawable(R.drawable.icon1)),
                new DrawerPair("title2",getResources().getDrawable(R.drawable.icon2)),
                new DrawerPair("title3",getResources().getDrawable(R.drawable.icon3))
        }));

1 Answers1

1

Before set to the setCompoundDrawables, do the following line of code, it will work fine.

Drawable drawable = dp.icon;

drawable.setBounds(0, 0, 50, 50); //For ex, i given 50, based on your design you can change.

then set to text view like this.

description.setCompoundDrawables(drawable, null, null, null);

Now it will work fine.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41