• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Progress Bar won't show

23tony

Well-Known Member
I am unable to get a progress bar to show up dynamically on my activity.

If I set it to VISIBLE at the beginning, it shows. But if I change the visibility in any way, I can't get it to show up programmatically.

I have tried making the initial setting INVISIBLE and GONE, and I have tried making the initial setting VISIBLE and then changing it in onCreate, but when I try to get it to show up, it won't. Pretty much all other manipulations I'm doing on the screen are working as expected.

I am using a constraint layout. The progressbar is centered on the parent. I have tried placing it first and last in the XML, none of this changes the behavior.

Here is the relevant parts of the code:
Code:
public class MainActivity extends AppCompatActivity {
    // var declarations
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextBox = findViewById(R.id.textBox);

        mLoadButton = findViewById(R.id.loadButton);
        mLoadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadHtml();
            }
        });
        mProgressBar = findViewById(R.id.progressBar);
        mProgressBar.setVisibility(View.INVISIBLE); // Also tried setting INVISIBLE and GONE in XML
    }

    private void loadHtml() {
        mProgressBar.setVisibility(View.VISIBLE);
        try {
            Thread.sleep(2000);
        }
        catch (java.lang.InterruptedException interruptedException) {
            // handle the exception
        }
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://10.0.2.2";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        mTextBox.setText(response);
                        mProgressBar.setVisibility(View.GONE);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        String err = error.toString();
                        mTextBox.setText(err);
                        mProgressBar.setVisibility(View.GONE);
                    }
        });
        queue.add(stringRequest);
    }
}

So as I was writing this, I tried something, and got an interesting result: I removed the lines mProgressBar.setVisibility(View.GONE); - and the progress bar showed up after the value for mTextBox was updated. So it looks like it has something to do with an interaction with the Volley request - but I'm not quite sure what, nor how to get past it.

Appreciate any feedback, and if I figure it out on my own I'll post back what I learn.
 
That experiment seemed to have been the clue I needed.

So I created an inner class HTMLLoader extending AsyncTask, and moved my loadHTML code into doInBackground. Then I changed the original loadHTML to
Code:
private void loadHTML() {
    mProgressBar.setVisibility(View.VISIBLE);
    new HTMLLoader().execute();
}

This is working just fine, exactly what I was looking for :)

But I would still be interested in knowing if there are any other approaches that would accomplish the same end result. Thanks!
 
I removed the lines mProgressBar.setVisibility(View.GONE); - and the progress bar showed up after the value for mTextBox was updated
In your xml, did you set the visibility to Visible?
 
I have tried making the initial setting INVISIBLE and GONE, and I have tried making the initial setting VISIBLE and then changing it in onCreate, but when I try to get it to show up, it won't.

1. In xml file, set progress bar visibility to GONE.
2. In onCreate

Java:
mProgressBar = findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.VISIBLE);

It should be show now.
 
1. In xml file, set progress bar visibility to GONE.
2. In onCreate

Java:
mProgressBar = findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.VISIBLE);

It should be show now.

That makes the progress bar show when the page loads. That isn't the behavior I wanted.

Also, as mentioned, I have it working as desired now.
 
Back
Top Bottom