After applying Gravity properties to my XML items, they don't seem to be positioned in the right place. How can these issues be fixed so that the following conditions are met?:
- The 'move up' button needs to be at the top of the screen
- The 'move down' button needs to be at the bottom of the screen
- The
GridView
needs to be in the vertical centre of the screen
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_moveup"
android:text="move up"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="top"
/>
<GridView
android:id="@+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:numColumns="auto_fit"
/>
<Button
android:id="@+id/btn_movedown"
android:text="move down"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="bottom"
/>
</LinearLayout>
UPDATE (akshay_shahane's suggestion)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_moveup"
android:text="move up"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="5"
android:onClick="moveup_click"
/>
<GridView
android:id="@+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="90"
android:layout_gravity="center_vertical"
android:numColumns="auto_fit"
/>
<Button
android:id="@+id/btn_movedown"
android:text="move down"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="5"
android:onClick="movedown_click"
/>
</LinearLayout>
Either set the weight of the
GridLayout
to 1 andlayout_height
to 0dp like thisOr wrap the grid layout in another container like this.
The second option would be better because it allows the
GridLayout
to be centered by usingandroid:layout_gravity="center"
.