0

I want to place spinning wheel progress bar at (x, y) location in relative layout. To do that, I have to know the size of progress bar. How do I know size of progress bar? Is there better way than this?

ProgressBar pb = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = x - pb.getWidth() / 2;  // pb.getWidth() returns 0
params.topMargin = y - pb.getHeight() / 2;
addView(pb, params);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
user1301568
  • 2,103
  • 4
  • 25
  • 32

1 Answers1

0

The width and height are not defined until the view rendered to the screen. In your case it should look like this:

addView(pb, params);
leftMargin = x - pb.getWidth() / 2;  // pb.getWidth() returns 0
topMargin = y - pb.getHeight() / 2;

Duplicate 1 Duplicate 2

Community
  • 1
  • 1
Arkady
  • 1,178
  • 14
  • 35