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