How to change the position of a imageButton when onClick
Posted
by
Amber
on Stack Overflow
See other posts from Stack Overflow
or by Amber
Published on 2012-06-11T04:10:04Z
Indexed on
2012/06/11
4:40 UTC
Read the original article
Hit count: 166
I am trying to shift the position of a image button when on click. I tried the following but it crashes at "rlOut.addView(imgP, p);" I have no issues when running them in onCreate().
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(62, 62);
ImageButton imgP = (ImageButton)findViewById(R.id.imgP);
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
LayoutParams par = (LayoutParams)imbBtn.getLayoutParams();
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.d("ok","down");
mX = (int) event.getX() - imbBtn.getWidth() / 2;
mY = (int) event.getY() - imbBtn.getHeight() / 2;
et1.setText("mX down: "+String.valueOf(mX));
et2.setText("mY down: "+String.valueOf(mY));
}
else if (event.getAction()==MotionEvent.ACTION_UP)
{
Log.d("ok","up");
p.leftMargin=mX;
p.rightMargin=mY;
rlOut.addView(imgP, p);
Log.d("ok","p");
}
© Stack Overflow or respective owner