title: How to change the position of a imageButton when onClick
body: <p>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().</p>
  
  <p>public class Delet3Activity extends Activity {</p>
  
  <pre><code>RelativeLayout rlOut;
  ImageButton imbBtn;
  
  EditText et1, et2;
  ImageView ivB;
  ImageButton imgP;
  RelativeLayout.LayoutParams p;
  RelativeLayout.LayoutParams params;
  
  int mX, mY;
  </code></pre>
  
  <p>@Override</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  
  et1 = (EditText)findViewById(R.id.et1);
  et2 = (EditText)findViewById(R.id.et2);
  
  imbBtn = (ImageButton)findViewById(R.id.imgBtn);
  rlOut = (RelativeLayout)findViewById(R.id.rlOut);
  imgP = (ImageButton)findViewById(R.id.imgP);
  
  ivB = new ImageView(this);
  ivB.setBackgroundColor(Color.RED);
  
  Display display = getWindowManager().getDefaultDisplay();
  int width = display.getWidth();
  int height = display.getHeight();
  
  width = width/2-62;
  height = height/2-62;
  p = new RelativeLayout.LayoutParams(62, 62);    
  
  imbBtn.setOnTouchListener(new View.OnTouchListener() {
  
  
  
      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");
              rlOut.addView(ivB, params);
              p.leftMargin=4;
              params.leftMargin = 60;
              params.topMargin = 20;
              rlOut.addView(imgP, p);
              Log.d("ok","p");
          }
          else if (event.getAction()==MotionEvent.ACTION_MOVE)
          {
              Log.d("ok","move");
              mX = (int) event.getX() - imbBtn.getWidth() / 2;
              mY = (int) event.getY() - imbBtn.getHeight() / 2;
              et1.setText("mX move: "+String.valueOf(mX));
              et2.setText("mY move: "+String.valueOf(mY));
          }
          return false;
      }
  });
  </code></pre>
  
  <p>}
  }</p>