1

Hello I made this compound view that contains a text view (to display error or advice) and a edit text (for input)

 <TextView android:id="@+id/guidanceOrError" android:gravity="center" android:padding="10dp" android:text="Please input 6 characters and 1 number" android:layout_marginBottom="10dp" android:background="@drawable/input_guidance_background" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/inputField" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/rectangle_border" android:padding="@dimen/login_editText_padding" tools:hint="@string/user_name"/> </merge> 

And this is me using it in an activity layout

 <com.ersen.test.widgets.ValidationInputField android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/login_editText_top_margin" android:hint="@string/password" android:inputType="textPassword" /> 

My problem is that attributes like hint and inputType are being ignored. This is because in my init(AttributeSet attrs) method I am not getting the attributes out

 if(attrs != null){ TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.???); a.recycle(); } 

My question is how can I use attributes that already exist? I do not want to re-create them

Please help me and thanks for reading

Edit 1 My compound view extends LinearLayout

1 Answer 1

1

Guess you're talking about a CustomView.

However you should declare-styleable in attrs.xml and use it like this:

<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ValidationInputField"> <attr name="android:hint"/> <attr name="android:inputType"/> </declare-styleable> </resources> 

So, edit your init method like this:

 if(attrs != null){ TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ValidationInputField); String hint = a.getString(R.styleable.ValidationInputField_android_hint); int inputType = a.getInt(R.styleable.ValidationInputField_android_inputType,0); // set these two values in your EditText programmatically EditText editText = (EditText) findViewById(R.id.inputField); editText.setHint(hint); editText.setInputType(inputType); a.recycle(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.