Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

android - setProgressBarIndeterminateVisibility(true) not working

I'm trying to add a progress bar to my actionBar. I'm talking about the spinning circle. I did a request and tried to set is vissible, but nothing happens. I have read many likely questions but is still couldn't figure out what i'm doing wrong.

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//Above setContentView, very important
    setContentView(R.layout.activity_main);
    //...other stuff
}

In an other method which i don't call in on create.(It's an onClick method)

public void plus(View view){
    setProgressBarIndeterminateVisibility(true);//Nothing happens
    //...other stuff
}

I do not understand what's wrong, please help me.

NOTE: I never set it to false

Edit: I tried mmlooloo second part, but absolutly noting happend. Not even part 3. So I tried part 4, but i gave me an exception.

"This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead."

I removed the Window.FEATURE_ACTION_BAR request, but it gave me the same exception. I don't think i need to set windowActionBar to false, but I did and it still gave me the same exception.

Any other options?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First:

Window provided Progress Bars are now deprecated with Toolbar.

Second:

you must use:

setSupportProgressBarIndeterminateVisibility(true);

instead of

setProgressBarIndeterminateVisibility(true);

because you extends ActionBarActivity. (because you are using supportRequestWindowFeature instead of requestWindowFeature)

Third:

If it crashes this is a known issue. If your library is updated sorry but it is now just a no-op:

setSupportProgressBarIndeterminateVisibility() crashing has been fixed for a future release, in that it will be a no-op.

Fourth:

My solution:

use toolbar with progressBar widget:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
        progressBar.setVisibility(View.VISIBLE);
    }

Layouts:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

and

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...