What is the correct way to detect the gravity flags used for an XML item? I'm trying to achieve the following:
- If the gravity of my
TableLayout
(myTbl) is set toGravity.TOP
, then I want the gravity to be set toGravity.CENTER_VERTICAL
- Else if the gravity of my
TableLayout
(myTbl) is set toGravity.CENTER_VERTICAL
, then I want the gravity to be set toGravity.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.
You should use
getLayoutParams().gravity
: