1
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int)width, (int)height);

    lp.width = (int) (width * 0.75f);
    lp.height = (int) (height * 0.5f);

    lp.setMargins((int) width/8, (int) height / 4, (int) (width - width/8) , (int) (height - height/4));
    addContentView(img, lp);

This should display my (ImageView) img in the center of the screen. It works as expected in my phone running API 16 but when I debug in emulator with API 8, the img is displayed in the top left corner.

I'm fed up of trying RelativeLayout.LayoutParams.setMargins() or AddRule for center aligning my view in the parent. Couldn't get it. Any help appreciated.

I'm not using any XML file to inflate my viewgroup. I'm adding views programmatically in my activity.

enter image description here enter image description here

This is how I want it, works in OS 4.0. & .... . Second picture is of OS2.0 -> This I want to fix.

Khulja Sim Sim
  • 3,469
  • 1
  • 29
  • 28

1 Answers1

2

Fix:

lp.gravity = Gravity.FILL_HORIZONTAL;
Khulja Sim Sim
  • 3,469
  • 1
  • 29
  • 28