i am working on android api8. i want to position/place a view on screen dynamically. but to use setX and setY we need API level 11 and above. how can we use it in API 8 or is there any alternative for this? need help
3 Answers
You can do this by using the LayoutParams
.
These can be added to components of the android interface to set their bounds and position.
An example (setting the LayoutParams on a child view of a RelativeLayout)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option)
params.leftMargin = 50; //Your X coordinate
params.topMargin = 60; //Your Y coordinate
childView.setLayoutParams(params);
Take note that the type of the LayoutParams must be equal to the parent of the childview you want to add them to. (LinearLayout.LayoutParams
for a LinearLayout
, RelativeLayout.LayoutParams
for a RelativeLayout
etc.).
Also, instead of childView.setLayoutParams(params);
you can also use parentView.addView(childView,params);
to set the Layoutparams when the item is added to the parent container.
NOTE! The values for the coordinates are in pixels. Since it's best practice to use DP values to define your interface sizes you could use this piece of code to convert dp to pixels:
private int getPixels(int dipValue){
Resources r = getResources();
int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
return px;
}

- 9,541
- 4
- 50
- 70
-
@manojmore can u pls send me demo code for this.I tried this but not getting the result.Something is wrong i think. – Chirag Shah Sep 30 '13 at 11:26
-
are these value in DP or Pixels ? – Adil Malik Oct 14 '13 at 15:14
-
@AdilMalik The values are in pixels. To convert dp to pixels at runtime, use this piece of code:`private int getPixels(int dipValue){ Resources r = getResources(); int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics()); return px; }` – Leon Lucardie Oct 15 '13 at 09:55
-
1@LeonLucardie I am slightly confused xD - you say the last piece of code converts from pixel to dip, but the used variable names indicate the opposite? It looks like you have to pass the dip which are then converted to pixel? How do I convert the pixel (what I get from params.width/params.height) to dip? Thanks in advance! – AgentKnopf Oct 23 '13 at 07:09
-
1@Zainodis I never said it was meant for pixel to dp converting. I stated in my answer and comment the code is meant for converting DP values to pixels. This code is used for the reverse: `public float convertPixelsToDp(float px){ DisplayMetrics metrics = getResources().getDisplayMetrics(); float dp = px / (metrics.densityDpi / 160f); return dp; }` – Leon Lucardie Oct 23 '13 at 07:44
-
@LeonLucardie Thank you very much for the clarification, I misunderstood your original post - just saw that now, thanks again :) ! – AgentKnopf Oct 23 '13 at 07:50
aditionally to above(Leon Lucardie's answer)if the view is a GroupView and when its being supposed the view edges to go outside the screen , you also have to put :
params.rightMargin=currentRightMargin -yourX;
params.bottomMargin=currentBottomMargin -yourY;
to avoid distortions within view's inner layout

- 3,672
- 2
- 41
- 29
You can use http://nineoldandroids.com/ and have API support from level 1, in this way:
ViewPropertyAnimator.animate(view).translationYBy(-yourY).setDuration(0);

- 4,740
- 3
- 28
- 31