0

I want to show the word "STATS" above a Listview containing either 2 or 3 list items. I want the ListView at the bottom edge of the screen and the remaining space to be filled by the "STATS" TextView and the text centered vertically and horizontally. Basically a single word floating lonely in the exact middle of the remaining space.

What I need it to look like

I cannot do this in the "drag and drop" layout editor because it shows 8 placeholder items in the ListView, so I can't snap to the edges of things properly.

I've got the ListView how I want it, but the "STATS" is causing problems.

I've tried all sorts of things (apart from the right one, obviously). Here's my currently broken layout...

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    >

    <TextView
        android:id="@+id/tv_reboot_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:text="@string/stats"
        />

    <ListView
        android:id="@+id/lv_app_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/reboot_screen_items"
        android:layout_gravity="center_horizontal|center_vertical"
        />

</LinearLayout>

I'm sure it's something trivial to do but its driving me mad.

3 Answers3

0

Can you try this?

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >

<TextView
    android:id="@+id/tv_reboot_message"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="30sp"
    android:textStyle="bold"
    android:textAlignment="center"
    android:gravity="center"
    android:text="STATS"
    android:layout_above="@+id/lv_app_info"
    />

<ListView
    android:id="@+id/lv_app_info"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/values"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    />

 </RelativeLayout>

You can use RelativeLayout to fill up the remaining space to show your TextView.

Bharath Mg
  • 1,117
  • 1
  • 9
  • 18
0

Try this, it works well

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_reboot_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:text="STATE"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center" />

    <ListView
        android:id="@+id/lv_app_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/reboot_screen_items"
        android:layout_gravity="bottom" />
</LinearLayout>

Result

enter image description here

Context
  • 1,873
  • 1
  • 20
  • 24
  • thank you. I didn't realise there were more than one type of gravity. This is exactly what I wanted. –  Jan 13 '16 at 15:00
0

Use gravity and layout_gravity attributes.

Gravity and layout_gravity

Community
  • 1
  • 1
Nicolas Cortell
  • 659
  • 4
  • 16