user1302569
user1302569

Reputation: 7191

Change position of progressbar in code

I have a progress bar which I create in code my activity. This is code:

progressDialog = new ProgressDialog(this); 
// Progress bar message.
String progressBarMessage = "Loading, please wait...";
progressDialog.getWindow().setGravity(Gravity.BOTTOM);

progressDialog.setCancelable(false);
progressDialog.setMessage(progressBarMessage);
// set the progress to be horizontal
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// reset the bar to the default value of 0
progressDialog.setProgress(0);
// convert the text value to a integer
int maximum = 100;
// set the maximum value
progressDialog.setMax(maximum);
// display the progressbar
progressDialog.show();  

As you can see position my progressbar is bottom. How I can change position this progressbar to 3/4 of my page? I want to move that position will be between center and bottom? Maybe can I setMarginTop, but how?

Upvotes: 0

Views: 5752

Answers (1)

user
user

Reputation: 87064

Try something like this:

// ...
progressDialog.getWindow().setGravity(Gravity.CENTER);
WindowManager.LayoutParams wmlp = progressDialog.getWindow().getAttributes();
wmlp.y = height / 4;
progressDialog.getWindow().setAttributes(wmlp);
progressDialog.setCancelable(false);
// ...

Where height is an int value(the height of the screen) that you initialize in the onCreate method like this:

height = getResources().getDisplayMetrics().heightPixels;

Upvotes: 2

Related Questions