0

What is the correct way to detect the gravity flags used for an XML item? I'm trying to achieve the following:

  1. If the gravity of my TableLayout (myTbl) is set to Gravity.TOP, then I want the gravity to be set to Gravity.CENTER_VERTICAL
  2. Else if the gravity of my TableLayout (myTbl) is set to Gravity.CENTER_VERTICAL, then I want the gravity to be set to Gravity.BOTTOM

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_up"
        android:text="move up"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        android:onClick="moveup_click"
        />

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myTbl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        >
        <TableRow
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            >

            <Button
                android:text="@string/btn_A"
                android:layout_column="0"
                android:layout_height="match_parent"
                android:layout_width="0dip"
                android:layout_weight="1"
                />

            <Button
                android:text="@string/btn_B"
                android:layout_column="1"
                android:layout_height="match_parent"
                android:layout_width="0dip"
                android:layout_weight="1"
                />
        </TableRow>

        <TableRow
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            >

            <Button
                android:text="@string/btn_C"
                android:layout_column="0"
                android:layout_height="match_parent"
                android:layout_width="0dip"
                android:layout_weight="1"
                />

            <Button
                android:text="@string/btn_D"
                android:layout_column="1"
                android:layout_height="match_parent"
                android:layout_width="0dip"
                android:layout_weight="1"
                />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/btn_down"
        android:text="move down"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:onClick="movedown_click"
        />

</LinearLayout>

java

private Button myBtn;
private TableLayout myTbl;

public void movedown_click(View view) {
    if (myTbl.getGravity() == Gravity.TOP) {
        myTbl.setGravity(Gravity.CENTER_VERTICAL);
    } else if (myTbl.getGravity() == Gravity.CENTER_VERTICAL) {
        tblMain.setGravity(Gravity.BOTTOM);
    }
}

I tried using getGravity() but API 24+ is required, which is not what I want.The API used in my project is lower than 24.

SD826E
  • 6,673
  • 7
  • 45
  • 92

1 Answers1

0

You should use getLayoutParams().gravity:

private TableLayout myTbl;

public void btn_click(View view) {
    TableLayout.LayoutParams params = (TableLayout.LayoutParams) myTbl.getLayoutParams();
    if (params.gravity == Gravity.TOP) {
        myTbl.setGravity(Gravity.CENTER_VERTICAL);
    } else if (params.gravity == Gravity.CENTER_VERTICAL) {
        myTbl.setGravity(Gravity.BOTTOM);
    }
}
Marcin Mrugas
  • 649
  • 6
  • 13