This is part of my main xml layout. I have a compound view(SingleDrinkView) and I want to set a different src for my image button for each 'SingleDrinkView'.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_below="@id/profilePhoto"
android:background="#d0d0d0"
android:orientation="horizontal" >
<com.ziepa.drinkparty.SingleDrinkView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
custom:drinkImage="@drawable/beer_icon"
/>
<com.ziepa.drinkparty.SingleDrinkView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
custom:drinkImage="@drawable/shot_icon"
/>
<com.ziepa.drinkparty.SingleDrinkView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
custom:drinkImage="@drawable/wine_icon"
/>
</LinearLayout>
I've added a custom attribute, but I couldn't figure out a way to set it to my image button :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/singleDrinkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:padding="0dp"
android:scaleType="centerInside"
android:src="???" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="x0"
android:textColor="#000"
android:textSize="32sp" />
</RelativeLayout>
So I ended up trying to set it via code by fetching this custom attribute :
case R.styleable.SingleDrinkView_drinkImage:
//This string contains "res/drawable-xhdpi/beer_icon.png"
String drinkImage = a.getString(attr);
ImageButton imageBt = (ImageButton) this.findViewById (R.id.singleDrinkImage);
//createFromPath returns null.
imageBt.setImageDrawable(Drawable.createFromPath(drinkImage));
break;
Hence I don't know what to do right now, I'm also not sure if there's a smarter way of doing it. I wanted to have the same custom widget with different 'settable' attributes so to speak.