2

I'm try to do some app which has to unlock when user moved image to a certain place... But the problem is when the user touch somewhere else except the image, it still tries to move and it gives this error: java.lang.ClassCastException: android.widget.AbsoluteLayout$LayoutParams

Here's my codes:

public class Main extends Activity {

private View selected_item; private int offset_x = 0; private int offset_y = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ViewGroup vg = (ViewGroup) findViewById(R.id.lout); vg.setOnTouchListener(new View.OnTouchListener() { @SuppressWarnings("deprecation") public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: int x = (int) event.getX() - offset_x; int y = (int) event.getY() - offset_y; Log.e("SONUC", "SONUC1: " + x + ", " + y); int w = getWindowManager().getDefaultDisplay().getWidth() - 25; int h = getWindowManager().getDefaultDisplay().getHeight() - 25; if (x > w) x = w; if (y > h) y = h; Log.e("SONUC", "SONUC2: " + x + ", " + y); AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams( new ViewGroup.MarginLayoutParams( AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT)); lp.x = x; lp.y = y; Log.e("SONUC", "SONUC3: " + lp); selected_item.setLayoutParams(lp); break; case MotionEvent.ACTION_UP: offset_x = (int) event.getX() - offset_x; offset_y = (int) event.getY() - offset_y; Log.e("SONUC", "SONUC5: " + offset_x + ", " + offset_y); selected_item = v; if (offset_x < 220 && offset_x > 150 && offset_y < 330 && offset_y > 300) startActivity(new Intent("com.yahya.GeneralTraining.UNLOCKED")); break; default: break; } return true; } }); ImageView img = (ImageView) findViewById(R.id.imageView1); img.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: offset_x = (int) event.getX(); offset_y = (int) event.getY(); Log.e("SONUC", "SONUC4: " + offset_x + ", " + offset_y); selected_item = v; break; default: break; } return false; } }); } 

}

4
  • set your image container size same as image size if the image is smaller then the device screen, if the image size is larger than the device screen then crop that image or set that image as full screen Commented Jan 27, 2012 at 8:49
  • There is a small picture on my screen, and user drags it to a certain place on the screen and then other page shows up... It's kind of lock issue. So it should not be the same size with the screen :/ Commented Jan 27, 2012 at 9:49
  • please send the logcat.. Commented Jan 30, 2012 at 11:40
  • .check in the debugger or in log that what is the selected_item at the time of dragging the other things of screen. Commented Jan 30, 2012 at 11:47

2 Answers 2

2

You can provide a check in the onTouchListener for viewgroup to confirm that the image is clicked initially.

vg.setOnTouchListener(new View.OnTouchListener() { @SuppressWarnings("deprecation") public boolean onTouch(View v, MotionEvent event) { if(selected_item.getId() == R.id.image){ switch (event.getActionMasked()) { //your code } } } return true; } }); 
Sign up to request clarification or add additional context in comments.

Comments

0

1st thing don't create new object of the LayoutParam just get LayoutParam object from the container like image or your layout

suppose getting for ImageView then

LayoutParam lp = imageView.getLayoutParam(); lp.width = newWidth; lp.height = newHeight; 

now change the size and set it in imageView.setLayoutParam()

imageView.setLayoutParam(lp); 

and another thing is you also set the touch listener for ViewGroup just set for the ImageView not set the touch listener for the ViewGroup and in the touch listener implementation return true to indicate the touch listener was implemented

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.