22

My activity class extends android.support.v7.app.ActionBarActivity. I am requesting window feature and calling setSupportProgressBarIndeterminateVisibility() in the onCreate() method as follows:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);
    setSupportProgressBarIndeterminateVisibility(true);
}

I cannot grasp what's wrong with my code and why I am getting java.lang.NullPointerException raised by the setSupportProgressBarIndeterminateVisibility().

My gradle dependencies contains:

compile 'com.android.support:appcompat-v7:21.0.0'

Does anybody know how to use the indeterminate progress bar in the support.v7 action bar with the API 21?

Lubos Ilcik
  • 2,176
  • 1
  • 16
  • 12

2 Answers2

32

You need to use Toolbar instead of ActionBar and add the ProgressBar into the Toolbar.

Here is an easy solution to insert indeterminate ProgressBar into Toolbar; turns out it's not difficult at all :) Just put your ProgressBar xml element inside your Toolbar like this:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <ProgressBar
        android:id="@+id/progress_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:indeterminate="true"
        android:visibility="gone" />

</android.support.v7.widget.Toolbar>

And then in your AppCompatActivity, simply retrieve the ProgressBar after retrieving the Toolbar and set the ProgressBar to visible or invisible when you need it.

protected void onCreate(Bundle savedInstanceState) 
{
    setContentView(R.layout.toolbar);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    progressBar = (ProgressBar) findViewById(R.id.progress_spinner);

    //Make progress bar appear when you need it
    progressBar.setVisibility(View.VISIBLE);

    //Make progress bar disappear
    progressBar.setVisibility(View.INVISIBLE);
}

Hope this helps :)

EDIT: replaced ActionBarActivity with AppCompatActivity as per the latest Android support libraries guidelines.

Joen93
  • 1,197
  • 1
  • 10
  • 8
ABVincita
  • 8,676
  • 2
  • 18
  • 16
  • Excellent answer! On a perhaps unrelated note, I have the exact same layout code as you have above (maybe I got it from one of your earlier answers), but the "visibility=gone" is ignored on a Galaxy S5 running 4.4.2. I have to programmatically find the ProgressBar view by id and set its visibility to View.GONE. Do you think this is some sort of an appcompat issue? – swooby Jun 04 '15 at 22:45
  • @swooby I have Galaxy S5 running 5.0 and the visibility=gone works just fine (using latest support library appcompat-v7:22.2.). Maybe it's a specific older appcompat or 4.4.2 specific bug. Nevertheless, you can also use "visibility=invisible" if you don't need to use the toolbar space for something else :) – ABVincita Jul 10 '15 at 05:04
  • 1
    The xml code should be improved with: `android:layout_gravity="end"`. – drindt Aug 01 '15 at 10:47
  • Has someome experienced any problem using a searchView with that solution? The problem is that is you expand the searchView and then collapse it the toolbar is magically made visible by the support library – lujop Aug 20 '15 at 17:10
  • I'm trying this with API 26, android:layout_gravity="end" doesn't seem to exist for ProgressBar. The result ends up being a progressbar in the middle of the toolbar. – amitavk Aug 30 '17 at 12:26
21

Per this comment on Chris Banes' (the author of AppCompat) AppCompat v21 announcement post:

Either way, progress bar's are not supported on Toolbar anymore.

On why:

Because Toolbar is a highly focused widget. If you want a ProgressBar, add it to the Toolbar yourself (it's just a ViewGroup).

I'd expect more information as the official documentation gets updated here shortly.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443