3

I was needing to get the value of the gravity of a LinearLayout. I checked the documentation and I only found how to set it ( setGravity(value) ). Anyone knows if there is a way to get a LinearLayout gravity?

Thanks

Daniel
  • 639
  • 8
  • 24
  • This is a good question. I was trying to answer it myself but I couldn't. Here is what I did learn though: http://stackoverflow.com/questions/27086486/how-to-set-both-gravity-and-layout-gravity-of-a-linearlayout-programatically/27086487#27086487 – Suragch Nov 23 '14 at 07:10

3 Answers3

6

I haven't tried this myself, but logically, it should work.

The problem is that the variable mGravity(that holds the current gravity info for the LinearLayout) is private. And no accessor methods exist to provide you access to it.

One way of solving this would be by using Reflection API.

Another (and much much cleaner) way would be to extend LinearLayout and override setGravity(int). For instance, like this:

public class LinearLayoutExposed extends LinearLayout {

    // Our own gravity!
    private int mGravityHolder = Gravity.START | Gravity.TOP;

    public GravLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setGravity(int gravity) {

        if (mGravityHolder != gravity) {

            // We don't want to make changes to `gravity`
            int localGravity = gravity;

            // Borrowed from LinearLayout (AOSP)
            if ((localGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
                localGravity |= Gravity.START;
            }

            if ((localGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
                localGravity |= Gravity.TOP;
            }

            mGravityHolder = localGravity;
        }

        super.setGravity(gravity);
    }

    // And now, we have an accessor
    public int getGravityVal() {
        return mGravityHolder;
    }
}

As you can tell, calling getGravityVal() on the custom LinearLayout will get you the gravity info.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • 1
    Great answer! Thanks. Why do you think that mGravity for the standard `LinearLayout` is private? Would people not normally need to access this? – Suragch Nov 25 '14 at 02:04
  • @Suragch `Would people not normally need to access this?` I was rather surprised to find that the gravity value cannot be accessed. Surely, someone, somewhere, might need it right? But, I have been working with `LinearLayouts` (and android, in general) for over an year now. And I haven't had the need to access this value.. not even once. Its an interesting question though. Do you have a use-case that requires access to this value? Or was the bounty purely for learning purposes? – Vikram Nov 26 '14 at 18:59
  • 1
    The bounty was for learning purposes, or rather, because I tried to answer the question myself and couldn't. – Suragch Nov 28 '14 at 00:21
3

API >= 24

getGravity ()

https://developer.android.com/reference/android/widget/LinearLayout.html#getGravity()

API < 24

using reflection:

int gravity = -1;

try {
    final Field staticField = LinearLayout.class.getDeclaredField("mGravity");
    staticField.setAccessible(true);
    gravity =  staticField.getInt(linearLayout);

    //Log.i("onFinishInflate", gravity+"");
} 
catch (NoSuchFieldException e) {} 
catch (IllegalArgumentException e) {}
catch (IllegalAccessException e) {}
Community
  • 1
  • 1
wiz
  • 321
  • 2
  • 4
-2

You should get it from the LayoutParams of the LinearLayout:

mLinearLayout.getLayoutParams().gravity

@see doc of LinearLayout.LayoutParams:

public int  gravity Gravity for the view associated with these LayoutParams.
carlol
  • 2,220
  • 1
  • 16
  • 11
  • 1
    i've read about this in the documentation but it seems to be associated to the android:layout_gravity not the android:gravity http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html , am i wrong? – Daniel Oct 25 '14 at 18:56
  • I also agree that this gets the layout_gravity rather than the gravity. – Suragch Nov 23 '14 at 07:10