3

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); 
    ...
}
2

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

instead of " EditorInfo.TYPE_NULL" use " EditorInfo.TYPE_TEXT"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.