Osborne Cox
Osborne Cox

Reputation: 466

Horizontally and vertically center a ProgressBar in Relative Layout

I am trying to achieve the following progress dialog (without the text)

enter image description here

My current XML code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background"
        >

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerVertical="true"
            android:visibility="gone"
            />

I am programatically hiding and showing the progress bar when required (and hiding/showing other screen elements), all of which are under the Relative Layout in the XML code above.

However, whenever the Progress Bar shows, it appears centered at the top of the screen instead of being centered horizontally AND vertically.

Upvotes: 0

Views: 866

Answers (1)

Jose Augusto Montiel
Jose Augusto Montiel

Reputation: 774

Setting the ScrollView's attribute "fillViewport" to true should do it.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background"
android:orientation="vertical"
android:fillViewport="true"
>

also make sure its child's height is set to "fill_parent" and the ProgressBar is properly centered

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

Upvotes: 2

Related Questions