Skip to content Skip to sidebar Skip to footer

How Can I Use Android Databinding To Dynamically Change Android:layout Property?

I have the XML layout code:

The layout XML attribute values can be of a variety of types. Sometimes, this is obvious, such as android:enabled="false" or android:layout_width="30sp". And, sometimes, the type really is a string or string resource reference (e.g., android:text="Foo"). But sometimes the attribute value looks like an English-language string, but it is really one of a set of enumerated values (e.g., gravity values).

I didn't realize that gravity was actually an enumerated attribute

A rough-cut rule of thumb: if the build would fail if you translated the value into another language, then it is an enumerated attribute (e.g., android:gravity="fin" instead of android:gravity="end", assuming that Google Translate gave me reasonable Spanish there...). If the translated value would work just fine, then it's any valid string or (usually) string resource.

I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.

You will have to transmogrify the string values into equivalent Gravity constants like Gravity.START.

Solution 2:

you can definitely use android:gravity="@{info.mygravity}" in layout file but make sure that mygravity should be int value.

you should refer this

like if you want center gravity, value of info.mygravity should be Gravity.CENTER

Post a Comment for "How Can I Use Android Databinding To Dynamically Change Android:layout Property?"