0

My code:

Display display = getWindowManager().getDefaultDisplay(); 
final int width = display.getWidth();
final int height = display.getHeight();

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200,200);
relativeLayout.addView(img1,params);
params.leftMargin = width/2;
params.topMargin = height/2;
setContentView(relativeLayout);

Could somebody help me to fit my img1 to the center of the screen, at every resolution?
I think that the display.getwidth value is not associated with params.leftmargin.
Please instruct me to get the margin value to any use by dividing it.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

try this

RelativeLayout.LayoutParams params =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
img1.setLayoutParams(params);
relativeLayout.addView(img1);

EDIT : if u want to add more views then u should create layoutparams for every view and use addRule method I m just giving u an example how to place a view under a view u can place that view right , left or above accoridngly change this RelativeLayout.ALIGN_BOTTOM and u can use RelativeLayout.LAYOUT_BELOW instead of RelativeLayout.ALIGN_BOTTOM

p.addRule(RelativeLayout.ALIGN_BOTTOM, ur_view.getId());

if u create it in xml it will be much more easier

if u want to set margin for any view do something like this

p.setMargins(left margin, top margin, right margin, bottom margin);

left margin, top margin, right margin, bottom margin are Integer value, which value u want to set for ur view

Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • Yessss , Definitely the answer I want.. HThank you so much ... :) – Leonar Aung Feb 11 '14 at 14:25
  • But, don't annoy me sir. I still want to locate other pics on the relativelayout with different positions. How can i ? – Leonar Aung Feb 11 '14 at 14:28
  • can u post a screen shot of and whole scenario what u want and sorry for late reply – Kaushik Feb 11 '14 at 15:34
  • My scenario is to create a game about finding items. I set main images of puzzle at background image alternatively. And locate items (to find) will be on the background image so to click and find it(to create Onclick)... So, I need to locate these items on the background with the different locations. (Fixed location can cause my item to be uncovered) . So I need to know how to set the margin value with resolution proportionally ... Thank You so much !!:) – Leonar Aung Feb 11 '14 at 18:03