Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indeterminate Horizontal ProgressBar BELOW ActionBar using AppCompat?

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?

like image 306
user2382843 Avatar asked Feb 06 '14 01:02

user2382843


1 Answers

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!

like image 77
zoltish Avatar answered Nov 04 '22 16:11

zoltish