Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to reuse attr inputType?

Tags:

android

attr

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!

like image 706
android_su Avatar asked Sep 22 '14 08:09

android_su


Video Answer


1 Answers

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);
}
like image 137
android_su Avatar answered Nov 03 '22 08:11

android_su