Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get an attribute value of a widget as declarated in the android layout.xml file?

Is there a way in an android application to retrieve the attribute value of a widget declared in a layout.xml?

for example in my layout.xml file i have :

<CheckBox
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/clean_filesWithoutVideo"
     android:checked="@{uiprefs.switchButtonEditable}"
     android:id="@+id/checkbox_updateable" />

I would like to retrieve the literal value for android:checked, eg I want to retrieve "@{uiprefs.switchButtonEditable}" at runtime.

I searched in android.content.res.Resources class using getResources() from an Activity without success :

  • I cant get the xml file as stream.
  • Parsing attributes throught the Resources.getLayout() doesn't restitute the attribute value.

Maybe this value is hidden somewhere in the CheckBox instance but I can't find it using inspection at debug time...

Note : The value I want to retrive are especially databinding literals. Maybe I could retrieve this through the databinding API ?

like image 901
TofferBreeze Avatar asked Dec 13 '25 20:12

TofferBreeze


1 Answers

Every UI component has it’s own getters and setters through which you can retrieve attributes that are currently set or set what you want to. If the attributes are common among the views, for example height/width, the getter will be available in View class.
In your case, you can get checked attribute through:

CheckBox checkbox =  (CheckBox) view.findViewById(R.id.checkbox_updateable);
boolean initialState = checkbox.isChecked();

In Android Studio, you can get the methods available for a particular view. Enter var name (say checkbox) then enter dot (.) and it will show you all available methods. Start typing that you think you are looking for, it will show you all relevant methods.

UPDATE

I think AttributeSet is what you are looking for. You can do this :

 XmlPullParser parser = resources.getXml(myResource);
 AttributeSet attributes = Xml.asAttributeSet(parser);

From doc:

The implementation returned here, unlike using the implementation on top of a generic XmlPullParser, is highly optimized by retrieving pre-computed information that was generated by aapt when compiling your resources. For example, the getAttributeFloatValue(int, float) method returns a floating point number previous stored in the compiled resource instead of parsing at runtime the string originally in the XML file.

This interface also provides additional information contained in the compiled XML resource that is not available in a normal XML file, such as getAttributeNameResource(int) which returns the resource identifier associated with a particular XML attribute name.

like image 85
cgr Avatar answered Dec 15 '25 10:12

cgr



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!