Create and show ProgressBar programmatically

user6304758 picture user6304758 · Jul 2, 2016 · Viewed 32k times · Source

I have created a ProgressBar programmatically as below in my activity. How do I make it show?

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

Answer

motis10 picture motis10 · Jul 3, 2016

You can try this code for adding progressBar programaticlly to your layout.

RelativeLayout layout = new RelativeLayout(this);
ProgressBar progressBar = new ProgressBar(YourActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);

setContentView(layout);