Reputation: 83537
I am creating a custom compound view with the following layout
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"/>
</merge>
As you can see, it is simply a TextView
and a EditText
. I want to be able to provide attributes to my custom view that are forwarded on to either the TextView
or EditText
. For example
<codeguru.labelededittext.LabeledEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:label="@string/label"
app:hint="@string/hint"/>
I have figured out how to forward these string attributes to the TextView
and EditText
, repsectively:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabeledEditText,
0, 0);
try {
label.setText(a.getString(R.styleable.LabeledEditText_label));
edit.setHint(a.getString(R.styleable.LabeledEditText_hint));
} finally {
a.recycle();
}
Now I also want to set the inputType
of the EditText
. If I create a <attr name="inputType" format="flag">
tag, will I have to populate it with all the possible flag values? Is there a way to reuse the values already declared by EditText
?
Upvotes: 15
Views: 2226
Reputation: 31438
You can get this with:
int[] values = new int[]{android.R.attr.inputType};
TypedArray standardAttrArray = getContext().obtainStyledAttributes(attrs, values);
try {
mInputType = standardAttrArray.getInt(0, EditorInfo.TYPE_NULL);
} finally {
standardAttrArray.recycle();
}
Upvotes: 3