How to move a button programmatically in an absolute layout

ddarellis picture ddarellis · Feb 4, 2012 · Viewed 8.2k times · Source

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

Answer

deepak kumar picture deepak kumar · Feb 4, 2012

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)