Possible Duplicate:
How to set margin of ImageView using code, not xml
I was wondering if there was a way where I could center a view by using SetMargins (Or any other alternatives). I know that you can use gravity but if I do, the MotionEvent also applies with it (So when I drag the image, image goes to the new "center" from where I touched).
OnTouchListener dragt = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) v.getLayoutParams();
FrameLayout.LayoutParams spawn = (FrameLayout.LayoutParams) v.getLayoutParams();
setMargins only lets me use numbers and it won't have the same results for all devices.
spawn.setMargins(170, 350,0 ,0);
switch (v.getId()) {
case R.id.randomView:
}
switch(me.getAction()) {
case MotionEvent.ACTION_MOVE:
    par.gravity = 0;
    par.setMargins((int)me.getRawX() - (color.getWidth())/2, (int)me.getRawY() - (color.getHeight())/2, 0, 0);
    color.setLayoutParams(par);
    break;
case MotionEvent.ACTION_UP:
    score++;
    textScore.setText(String.valueOf(score));
    color.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);
Sets the position (refers from above)
    color.setLayoutParams(spawn);       
    break;
        }
return true;
    }
};
 
    