0

I have a custom dialog (inherited from android.app.Dialog) with no title or border. To this dialog I add a normal ProgressBar, and would like to center it in the dialog. The dialog itself has a background image that stretches for the full screen. Everything seems to work except that the progress bar is not getting centered in the dialog. It looks like this:

ProgBar Not Centered in Dialog

This is the code I have used within the customDialog class, to add the progress bar to the dialog:

ProgressBar progBar = new ProgressBar(context);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
addContentView(progBar, layoutParams);

getWindow().setGravity(Gravity.CENTER);

Is there something I'm missing, or getting wrong?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Rajath
  • 11,787
  • 7
  • 48
  • 62

3 Answers3

2

Try this..

    ProgressBar progBar = new ProgressBar(context);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL, progBar.getId());
    lp.addRule(RelativeLayout.CENTER_VERTICAL, progBar.getId());

    parentLayout.addView(progBar, lp);
swetha kini
  • 564
  • 1
  • 6
  • 23
2

Try this,

      LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)b.getLayoutParams();    
ll.gravity = Gravity.CENTER;
progBar.setLayoutParams(ll);
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
0

Try this, Set layout hight and width FILL_PARENT

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
progBar.setLayoutParams(params);
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22