Take the 2-minute tour ×

I made my UI with .xml, But I want to give the direction of the Button by dynamically. For Example;

 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="100px" />

Then is my activity class I changed something by dynamically like that.

    AbsoluteLayout al = new AbsoluteLayout(this);
    Button t = new Button(this);
    // x y ???

I've changed the height and widht..but I couldn't find the way to changed x and y directions..

Anyone knows it ? Would you please help me how can I do that??

share|improve this question

2 Answers 2

up vote 1 down vote accepted

try setPadding(int top,int left,int right,int bottom) method.

AbsoluteLayout al = new AbsoluteLayout(this);
    Button t = new Button(this);

I am not sure how your other elements of the screen are fitting, but I just assumed that button is the only element you have in your screen.

share|improve this answer
could you please give an example recording to my codes? –  Dapina May 9 '11 at 20:31
what does top, left, right, bottom mean??? –  Dapina May 9 '11 at 20:35
They are padding parameters. –  yogsma May 9 '11 at 20:38
thank you soo muchhhhhhhhhh :) By the way do you know 1 dip= how many px? –  Dapina May 9 '11 at 20:41
You can use button.setLayoutparams(params) method also. Create a params object and use myparams.x = x coordinate and myparams.y = y coordinate. –  yogsma May 9 '11 at 20:43


From http://developer.android.com/guide/practices/screens_support.html:

Density-independent pixel (dp) - A virtual pixel unit that applications can use in defining their UI, to express layout dimensions or position in a density-independent way.


share|improve this answer
I think it might be appropriate to accept the other answer as correct instead of mine. Since it answered the actual orginal question. Mine really could've been a comment but I wasn't sure if I'd have enough characters so I did it as an answer. –  FoamyGuy May 10 '11 at 13:09

Your Answer


