8

I have been looking for answers on how to place the indeterminate horizontal progress bar below the action bar using AppCompat. I'm able to get the horizontal progress bar to appear, but it is at the top of the action bar. I want it under/below the action bar kind of like how gmail does it (except without the pull to refresh).

I used the following code to have the progress bar appear:

supportRequestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main_activity);
setSupportProgressBarIndeterminate(Boolean.TRUE);
setSupportProgressBarVisibility(true);

but this places the horizontal progress bar at the top of the action bar. Anyone know how to place the progress bar below the action bar?

user2382843
  • 93
  • 2
  • 8
  • 1
    There is a work around suggested by : http://stackoverflow.com/questions/13934010/progressbar-under-action-bar/15073680#15073680 but this solution pretty much defeats the whole purpose of trying to use AppCompat. Anyone able to position the progress bar below the action bar using AppCompat? – user2382843 Feb 06 '14 at 23:10
  • Unbelievable how such a common problem has no simple solution... – SpaceMonkey Jul 18 '15 at 13:57

1 Answers1

6

I faced a similar problem recently and solved it by creating my own progressbar and then aligning it by manipulating getTop() of the content view.

So first create your progressbar.

final LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 20); //Use dp resources


mLoadingProgressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
mLoadingProgressBar.setIndeterminate(true);
mLoadingProgressBar.setLayoutParams(lp);

Add it to the window (decor view)

final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
decor.addView(mLoadingProgressBar);

And in order to get it to its correct position Im using a ViewTreeObserver that listens until the view has been laid out (aka the View.getTop() isnt 0).

final ViewTreeObserver vto = decor.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        final View content = getView(android.R.id.content);

        @Override
        public void onGlobalLayout() {
            int top = content.getTop();

            //Dont do anything until getTop has a value above 0.
            if (top == 0)
                return;

            //I use ActionBar Overlay in some Activities, 
            //in those cases it's size has to be accounted for
            //Otherwise the progressbar will show up at the top of it
            //rather than under. 

            if (getSherlock().hasFeature((int) Window.FEATURE_ACTION_BAR_OVERLAY)) {
                top += getSupportActionBar().getHeight();
            }

            //Remove the listener, we dont need it anymore.
            Utils.removeOnGlobalLayoutListener(decor, this);

            //View.setY() if you're using API 11+, 
            //I use NineOldAndroids to support older 
            ViewHelper.setY(mLoadingProgressBar, top);
        }
    });

Hope that makes sense for you. Good luck!

zoltish
  • 1,952
  • 16
  • 35