johnblanco
johnblanco

Reputation: 197

Why is my progress bar not centered vertically?

I have the following layout...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/bg_generic_portrait">
  <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/issue_list_item_background_normal"
            android:listSelector="@drawable/issue_background_selector"
            android:divider="@drawable/separator"
    >
  </ListView>
  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:id="@+id/preloader"
    style="@android:style/Widget.ProgressBar.Large"/>

  <TextView android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF0000"/>
</LinearLayout>

And it looks like this...https://i.sstatic.net/Ra5c6.png

I want the preloader to be centered vertically too, what is wrong?

Upvotes: 6

Views: 8629

Answers (5)

Ben Lefebvre
Ben Lefebvre

Reputation: 379

I don't know how much space is your Listview taking, but you might want to use a <RelativeLayout> instead of <LinearLayout> if you want to really define where you want x item.

Upvotes: 0

vivek.m
vivek.m

Reputation: 3331

There seems some problem with what you are trying to do. ListView on top of progress bar in your layout can have a variable height depending on its contents. So, it's not possible to ensure progress bar stays in center of screen using linear layout.

You may want to look at ProgressDialog class that will show a progress dialog while you load data into list in background.

Alternatively, you can make ProgressBar and ListView overlap by using RelativeLayout as top level layout and specify android:layout_centerInParent="true" attribute for ProgressBar

Upvotes: 1

Machine
Machine

Reputation: 472

Somehow the layout_gravity does not work well. What you need to do is nest the progress_bar in a linear_layout and set the gravity of that layout to center, like this:

<LinearLayout android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:gravity="center">
<ProgressBar
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/preloader"
          style="@android:style/Widget.ProgressBar.Large"/>
</LinearLayout>

I do not suggest setting the gravity to the topmost linear layout because you could have funny things happening if you change the list or add something.

Upvotes: 12

rizzz86
rizzz86

Reputation: 3990

Set android:gravity="center" in your parent LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:gravity="center"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

Upvotes: 0

Raykud
Raykud

Reputation: 2484

thats because you are using a LinearLayout, I suggest that you use a RelativeLayout and have the attibute center using layout_centerVertical like this:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:id="@+id/preloader"
    style="@android:style/Widget.ProgressBar.Large"/>

Upvotes: 1

Related Questions