3

I am trying to get a ProgressBar and TextView to be centered within a ViewSwitcher. They can be positioned vertically above each other although I have no problem if they were centered horizontally next to each other. I am able to get them to center horizontally but not vertically. It seems that the height of the outer view (the ViewSwitcher) doesn't fill vertically although every parent view has android:layout_height="fill_parent"

Here is the xml:

<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:background="#ff000000">
  <ProgressBar
    android:indeterminate="true"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/marker_progress"
    style="?android:attr/progressBarStyle"
    android:gravity="center_vertical" />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="18sp"
    android:text="Loading data..."
    android:gravity="center" />
</LinearLayout>
<ListView
  android:id="@+id/android:appCategoriesList"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ff918e8e"
  android:divider="#ff000000"
  android:dividerHeight="1dp"
  android:cacheColorHint="#00000000"
  android:scrollingCache="true"
  android:clickable="false"
  android:focusable="false" />
</ViewSwitcher>
Johann
  • 1,977
  • 3
  • 19
  • 18

2 Answers2

2

Set your LinearLayout's height to be wrap_content and it should also center it vertically. Also you should add android:layout_gravity="center" on the LinearLayout to center it inside the ViewSwitcher

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • It doesn't work. I even tried wrap_content on the ViewSwitcher as well. They always remain centered at the top of the view. – Johann Jul 14 '11 at 17:06
  • Oops, forgot to mention you should set `android:layout_gravity="center"` on the LinearLayout as well. I'll update my answer. – Jason Robinson Jul 14 '11 at 17:21
1

Use the code below:

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loadSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="#ff000000">
        <ProgressBar
            android:indeterminate="true"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/marker_progress"
            style="?android:attr/progressBarStyle"
            android:layout_gravity="center"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Loading data..." 
            />
    </LinearLayout>
    <ListView
        android:id="@+id/android:appCategoriesList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff918e8e"
        android:divider="#ff000000"
        android:dividerHeight="1dp"
        android:cacheColorHint="#00000000"
        android:scrollingCache="true"
        android:clickable="false"
        android:focusable="false" />
</ViewSwitcher>
Mandel
  • 2,968
  • 2
  • 22
  • 19
  • Problem solved using this solution: http://stackoverflow.com/questions/1957831/centre-a-button-in-a-linear-layout – Johann Jul 14 '11 at 17:11
  • Sure. RelativeLayout works as well with appropriate settings. I assumed you want a solution with LinearLayout. – Mandel Jul 14 '11 at 17:13