Hi, acopernicus.
Since in your blog you asked for somebody who knows how to pass & retrieve custom XML attributes, here are my 2 cents.
I have a custom view called ValueButton, which is basically an ImageButton with a value.
Using java Syntax Highlighting
public class ValueButton extends ImageButton {
private String mValue;
public ValueButton (Context context, String value)
{
super(context);
mValue = value;
}
public ValueButton (Context context, AttributeSet attrs,Map inflateParams) {
super(context, attrs, inflateParams);
Resources.StyledAttributes a = context.obtainStyledAttributes(attrs,
R.styleable.ValueButton);
String s = a.getString(R.styleable.ValueButton_value);
if (s != null)
setValue(s.toString());
}
public String getValue() {
return mValue;
}
public void setValue(String value) {
mValue = value;
}
Parsed in 0.032 seconds, using
GeSHi 1.0.8.4
In /res/values I created a file called attrs.xml
Using xml Syntax Highlighting
<resources>
<declare-styleable name="ValueButton">
<attr name="value" />
</declare-styleable>
</resources>
Parsed in 0.001 seconds, using
GeSHi 1.0.8.4
and in layout, I use my ValueButton this way:
Using xml Syntax Highlighting
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res/com.cadlg.android.calcdroid" <!-- Here you tell android where to look for your app xml parameters (i.e. your package) -->
android:id="@+id/first"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<com.cadlg.android.calcdroid.ValueButton
android:id="@+id/divide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:value="/" <!-- and here is where you set the custom parameter -->
android:src="@drawable/divide"/>
...
</LinearLayout>
Parsed in 0.002 seconds, using
GeSHi 1.0.8.4
summarizing:
1) In attrs.xml I declare the custom xml parameters for my custom widget,
2) in ValueButton class I implement a constructor which inflate the view based on the xml parameters (the second constructor) and retrieve the custom parameter,
3) and in layout I use the custom parameter as "app:<custom_parameter>="..."
I hope I did not forget anything...
Regards,
cadlg