15

I am trying to show the progress bar in the center of the screen, but it doesn't show up in the center. Below is my layout. What am i doing wrong here? Basically this is a layout to show image and android gallery widget on top. When the user clicks on the thumbnails, I am loading an image from the web and I want to show the progress dialog while the image is loading.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/container" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

    <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:gravity="top" />

    <ImageView android:id="@+id/actualimage" android:scaleType="fitCenter"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:visibility="gone" android:layout_gravity="center_vertical|center_horizontal|center"
        android:adjustViewBounds="true" />

    <ProgressBar android:id="@+android:id/progress_small1"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:visibility="visible" android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical|center_horizontal" />

</LinearLayout>
ImR
  • 787
  • 6
  • 8
Chandu
  • 1,049
  • 3
  • 11
  • 18

6 Answers6

46

Using a LinearLayout will stack your views vertically. Right now, your ImageView and ProgressBar are fighting for the centered position. For this type of situation, I would definitely recommend a RelativeLayout.

With RelativeLayout, you use android:layout_centerInParent="true".

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
5

Do android:gravity="center" in the root LinearLayout. Set the android:visibility of all other elements apart from the ProgressBar to gone when you want to show the ProgressBar

500865
  • 6,920
  • 7
  • 44
  • 87
4

Try giving the progress bar a layout_weight of 1

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Going to have to agree with Andro Selva on this one. The progress dialog is going to b a lot easier to work with. – Bradley Uffner Oct 24 '11 at 04:23
  • Thanks for your comments. Thought I will update on what I tried adding layout_weight. For some reason it expanded the progressbar size to ugly pixelated wheel spinning. layout_weight is more so for the ratio of the screen space to be allocated among the views than their placement I think. – Chandu Oct 24 '11 at 18:36
3

Maybe you should go for progress dialog, since I believe you are trying to show a progress indicator until the images are loaded for the gallery.

This will get you started.

http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

http://www.androidpeople.com/android-progress-dialog-example

And still if you are looking for a way to make use of your progressbar itself then maybe you should give a try to the answer provided by Bradley Uffner

Community
  • 1
  • 1
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
2

Add following:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Taner
  • 4,511
  • 3
  • 18
  • 14
1

I know this question is answered, but considering performance of laying out views/Subviews, RelativeLayout is expensive(it requires two pass). If your view hierarchy is complex, go for Relative Layout, or if your view has just simple views like below please go for Linear layout/Frame layout.

From my experience, when my app grown, UI performance with Relative layout was worse, ended up in reworking on all layout files :( Below is sample for putting a view(ProgressView in my case) in exactly center of view.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">
    <android.support.v7.widget.AppCompatImageView android:layout_gravity="center"  android:layout_width="match_parent"
        android:layout_height="0dp" app:srcCompat="@drawable/check" android:scaleType="fitCenter"
        android:layout_weight=".7"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" android:layout_weight=".3">
        <ProgressBar android:layout_gravity="center|center_horizontal" android:indeterminateTint="@color/textColorPrimary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:indeterminate="true"/>
    </FrameLayout>

</LinearLayout>
Itzdsp
  • 872
  • 11
  • 22