Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Hint attribute Value from AttributeSet

i have overridden EditText as given in this link.

Now while declaring this field inside layout i am using

<com.and.ab1209.ClearableEditText
android:id=”@+id/edit_text_clearable”
android:layout_width=”fill_parent”
android:hint="My Hint Goes here"
android:layout_height=”wrap_content” />

How do i retrieve this hint value inside any of these constructor.

 public ClearableEditText(Context context, AttributeSet attrs, int defStyle){...}
 public ClearableEditText(Context context, AttributeSet attrs){...}

How do i do this?

like image 669
RPB Avatar asked Jan 19 '26 12:01

RPB


1 Answers

You can access the standard xml attributes by doing the following inside your views constructor:

final String xmlns="http://schemas.android.com/apk/res/android";
//If you had a background attribute this is the resource id
int backgroundResource = attrs.getAttributeResourceValue(xmlns, "background", -1);
//This is your views hint
String hint = attrs.getAttributeValue(xmlns, "hint");

It does not matter if your view inherits from TextView, if you designate a hint using android:hint it will be accessible in your custom view.

like image 152
Hrafn Avatar answered Jan 21 '26 02:01

Hrafn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!