3

i make a test application in eclipse with adt plugin. I want when a button is clicked to move it in a random position in the screen, i found some example around the internet by nothing works

Button noBtn = (Button) findViewById(R.id.NoBtn);
noBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
    Button noBtn = (Button) findViewById(R.id.NoBtn);
    AbsoluteLayout.LayoutParams OBJ=new
    AbsoluteLayout.LayoutParams(35,35,408,160);
    noBtn.setLayoutParams(OBJ);
}
});

and i get application error

ddarellis
  • 3,912
  • 3
  • 25
  • 53
  • I was using RlativeLayout in the xml. So i change it `RelativeLayout.LayoutParams OBJ=newRelativeLayout.LayoutParams(50, 80); OBJ.topMargin = randomHeight; int a = ApplicationUtils.getScreenHeight( getWindowManager()); int aa = ApplicationUtils.getScreenWidth( getWindowManager()); OBJ.leftMargin = randomWidth; noBtn.setLayoutParams(OBJ); – ddarellis Feb 05 '12 at 09:38

1 Answers1

4

by using the layoutparam in android, u can specify the position of your widget through the java code rather than specifying it in the xml file in android.

AbsoluteLayout.LayoutParams OBJ = new AbsoluteLayout.LayoutParams(35,35,408,160);
button.setLayoutParams(OBJ);

here-

AbsoluteLayout.LayoutParams(width,height,X-position,Y-position) 

is what i have specified as (35,35,408,160)

Nate
  • 31,017
  • 13
  • 83
  • 207