I’m trying to create a custom Android compound view, here is the code:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/linearLayout1">
<ImageView android:src="@drawable/icon"
android:id="@+id/action_imageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/action_text" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:text="TextView">
</TextView>
</LinearLayout>
</merge>
ActionWidget.java (the component I’m working on):
public class ActionWidget extends LinearLayout
{
...
public ActionWidget(Context context, AttributeSet attributeSet,
int defStyle)
{
super(context, attributeSet);
LayoutInflater inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.action, this);
setClickable(true);
}
public void setLabel(String label)
{
TextView text = (TextView)findViewById(R.id.action_text);
text.setText(label);
}
public String getLabel()
{
return this.label;
}
...
}
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ActionWidget">
<attr name="label" format="string" />
<attr name="image" format="integer" />
</declare-styleable>
</resources>
Usage:
<com.someapp.form.ActionWidget
android:id="@+id/actionWidget1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:label="Text changed">
</com.someapp.form.ActionWidget>
When I launch the application, the text is still “TextView”.
What should I do to make it load from the xml?
Rewrite your constructor as follows: