2

I created a compound view consisting on an EditText and a TextView... I want make any developer using my view able to do the following

<MyCustomView
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:setInputType="textPassword"/>

Pay close attention to the last line of that xml. How can I wire that property to the EditText that lives inside MyCustomView

PD: I already created a on Attrs.xml and tried to do this

<declare-styleable name="ValidateEditText">
<flag name="none" value="0x00000000" />
            <flag name="text" value="0x00000001" />
            <flag name="textCapCharacters" value="0x00001000" />
            <flag name="textCapWords" value="0x00002000" />
            <flag name="textCapSentences" value="0x00004000" />
            <flag name="textAutoCorrect" value="0x00008000" /> ...

This does not work.. EditText.setContentType is not working for some reason

I alredy set other custom attributes, my question is how to set this particular one "InputType" doesnt seem to be working.

protected void init(Context context, AttributeSet attrs) {

    if (attrs != null) {
        TypedArray typedArray;
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.ValidateEditText);
        try {
            mHint = typedArray.getString(R.styleable.ValidateEditText_hint);

            mInputType = typedArray.getInt(R.styleable.ValidateEditText_inputType, EditorInfo.TYPE_CLASS_TEXT);
            mMaxLength = typedArray.getInt(R.styleable.ValidateEditText_maxLength, -1);
            mSingleLine = typedArray.getBoolean(R.styleable.ValidateEditText_singleLine, false);
            mPassword = typedArray.getBoolean(R.styleable.ValidateEditText_password, false);
            mImeOption = typedArray.getInt(R.styleable.ValidateEditText_imeOptions, EditorInfo.IME_ACTION_NEXT);
            mEditable = typedArray.getBoolean(R.styleable.ValidateEditText_editable, true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            typedArray.recycle();
        }
    }
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    //Initialise views
    mErrorMessage = (TextView) findViewById(R.id.error_msg);
        mEditText = (EditText) findViewById(R.id.edit_text);


        mEditText.setInputType(mInputType); 
    ...
}
FRR
  • 10,772
  • 13
  • 43
  • 80
  • Possible duplicate of [Android - custom UI with custom attributes](http://stackoverflow.com/questions/7608464/android-custom-ui-with-custom-attributes) – muratgu Feb 08 '16 at 23:15
  • 1
    It's not a duplicate, I;ve updated my question. – FRR Feb 08 '16 at 23:33

1 Answers1

2

You can declare styleable attr like this:

<declare-styleable name="MyCustomView">
    <attr name="android:inputType"/>
</declare-styleable>

And get this attr in your custom view like this:

int inputType = a.getInt(R.styleable.MyCustomView_android_inputType, EditorInfo.TYPE_NULL);

You can get more detail answer from here

Community
  • 1
  • 1
lovefish
  • 547
  • 4
  • 18