2

I have a ProgressBar in a ScrollView, and I'd like it to stay centered vertically despite the user scrolling down or up. However, I'm not sure how to accomplish this.

Here's the code that I have, simple as it is. I have an indeterminate ProgressBar:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ocr_scroll_view"
android:layout_width="match_parent"
android:layout_height="fill_parent">

    <RelativeLayout android:id="@+id/image_holder"
    android:layout_gravity="end"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <ImageView android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

        <ProgressBar android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

And I call it as such:

    spinningProgressBar = (ProgressBar)findViewById(R.id.progressBar1);
    spinningProgressBar.setIndeterminate(true);
    spinningProgressBar.setVisibility(View.GONE);

Setting its visibility at the appropriate times.

All suggestions very much appreciated.

mikeappell
  • 363
  • 4
  • 17

3 Answers3

9

You need the ScrollView to be at same level as ProgressDialog in your Layout.

Here it is in concept:

<RelativeLayout>
    <ProgressDialog>      
    <ScrollView>      
<RelativeLayout>
Booger
  • 18,579
  • 7
  • 55
  • 72
7

Put it outside of the ScrollView. Just have the ScrollView and the ProgressBar overlap.

You can read about how to overlap views here: Overlapping Views in Android

There's a couple of ways to do it, ranging from GridLayouts to RelativeLayouts and then programatic ways to do it.

Community
  • 1
  • 1
Alex K
  • 8,269
  • 9
  • 39
  • 57
  • Great idea. However, as the ScrollView is the highest view, I'd need to create a new layout outside of it to hold the ProgressBar, wouldn't I? – mikeappell Dec 15 '14 at 02:36
  • OH. Yes - just put them both inside of a RelativeLayout. Then you can use `android:layout_centerInParent="true"` to make the ProgressBar be in the center of the layout. – Alex K Dec 15 '14 at 02:37
  • Hell yes, works like a charm. Though the ProgressBar had to come after the ScrollView in order to be visible, which I neglected in my first attempt. And then I got a strange classcast error, but rebuilding the project did the trick. Thanks! – mikeappell Dec 15 '14 at 03:08
  • Hopefully adding another View as a holder won't do anything significant toward Activity efficiency, shouldn't be a big deal. – mikeappell Dec 15 '14 at 03:09
  • @mikeappell it won't do anything. you won't notice anything from something as small as that – Alex K Dec 15 '14 at 03:32
2

you can insert this into the scrollview tag

android:fillViewport="true"