Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct format for android:gravity in attrs.xml?

I have the following situation:

in styles.xml:

<style name="fooStyle">
    <item name="android:padding">?fooView.padding</item>
    <item name="android:background">?fooView.background</item>
    <item name="android:gravity">?fooView.gravity</item>
</style>

in attrs.xml:

<attr name="fooView.padding" format="dimension" />
<attr name="fooView.background" format="color|reference" />
<attr name="fooView.gravity" format="????"/>

in themes.xml:

<style name="fooViewTheme" parent="android:Theme">
    <item name="fooView.padding" >2dip</item>
    <item name="fooView.background" >#AA000000</item>
    <item name="fooView.gravity">right|bottom</item>
</style>

The problem is that I cannot figure out what the format for the fooView.gravity should be. I've already tried with string, enum and flag but none seem to work: I always get a java.lang.NumberFormatException: unable to parse 'right|bottom' as integer as soon as the view that uses this theme gets loaded.

All answers are appreciated.

like image 644
Mopper Avatar asked Mar 02 '12 09:03

Mopper


People also ask

What is gravity in android XML?

Gravity and layout_gravity both are the XML attributes. The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget). The android:layout_gravity is used to arrange the position of the entire View relative to it's container.

What is Attrs XML in android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.

What is difference between gravity and layout_gravity?

So in general android:layout_gravity attribute is used by child views to tell their parent how they want to be placed inside it, while android:gravity is used by the parent layout to tell the child views how they should be placed inside it.


2 Answers

Those are the gravity values used by Android. You can use that in your attrs.xml:

<resources>
    <declare-styleable name="MyCustomView">
        <attr name="gravity">
            <flag name="bottom" value="80" />
            <flag name="center" value="17" />
            <flag name="center_horizontal" value="1" />
            <flag name="center_vertical" value="16" />
            <flag name="clip_horizontal" value="8" />
            <flag name="clip_vertical" value="128" />
            <flag name="end" value="8388613" />
            <flag name="fill" value="119" />
            <flag name="fill_horizontal" value="7" />
            <flag name="fill_vertical" value="112" />
            <flag name="left" value="3" />
            <flag name="right" value="5" />
            <flag name="start" value="8388611" />
            <flag name="top" value="48" />
        </attr>
    </declare-styleable>
</resources>

In your layout XML you can use it this way:

<MyCustomView
        custom:gravity="center|bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

And in Java code you can read the value using this:

int gravity = a.getInt(R.styleable.MyCustomView_gravity, Gravity.NO_GRAVITY);

and directly set it to a sub view, if that makes sense for you:

someSubView.setGravity(gravity);

You can look up those gravity values in the source of android.view.Gravity or here

like image 171
Michael Geier Avatar answered Sep 18 '22 16:09

Michael Geier


It's a flag attribute. You need to define it in your attributes xml like this:

    <attr name="gravity">
      <flag name="right" value="0x01" />
      <flag name="bottom" value="0x02" />
      <flag name="left" value="0x04" />
      <!-- etc. -->
    </attr>

...and then access the values using bit masks, like this:

    boolean right = (array.getInt(R.styleable.fooView_gravity, 0) & 0x01) == 0x01;
    boolean bottom = (array.getInt(R.styleable.fooView_gravity, 0) & 0x02) == 0x02;
    boolean left = (array.getInt(R.styleable.fooView_gravity, 0) & 0x04) == 0x04;
    // etc.

...and of course, these values can be used from XML as well:

<style name="fooViewTheme" parent="android:Theme">
    <item name="fooView.gravity">right|bottom</item>
</style>
like image 27
Lorne Laliberte Avatar answered Sep 17 '22 16:09

Lorne Laliberte