I have a customized ViewGroup and it contains a EditText. I want to set inputType for EditText in xml. But I don't want to re-define input type. How can I do that?
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);
a.getInt(android.R.attr.inputType, EditorInfo.TYPE_NULL);
Caused by: java.lang.ArrayIndexOutOfBoundsException:
Error, because there's no inputType attr in my ViewGroup.
I also have tried this:
int[] attrsReuse = new int[] { android.R.attr.inputType /* index 0 */};
Resources.Theme theme = context.getTheme();
TypedArray ta = theme.obtainStyledAttributes(attrsReuse);
The same error.
Any ideas? Thanks!
I find a way to do this.
Add this to attrs.xml:
<declare-styleable name="MyEditText" >
<attr name="android:inputType"/>
</declare-styleable>
Add this to view in layout:
android:inputType="textPassword"
Then I can get this attr in my cusom view class:
int inputType = a.getInt(R.styleable.MyEditText_android_inputType, EditorInfo.TYPE_NULL);
if (inputType != EditorInfo.TYPE_NULL) {
mInputEditText.setInputType(inputType);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With