I have an ImageView
. I need to use onLongClickListener
for ImageView. When I use this code nothing happen.
Code:
gallery=(Gallery)findViewById(R.id.gall1);
gallery.setAdapter(new ImageAdapter(MainActivity.this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(
AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
final ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
imageView.setLongClickable(true);
imageView.setOnLongClickListener(new View.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Long Press work", Toast.LENGTH_SHORT).show();
return true;
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v,MotionEvent event) {
// TODO Auto-generated method stub
layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
{
parms = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
dx = event.getRawX() - parms.leftMargin;
dy = event.getRawY() - parms.topMargin;
}
break;
case MotionEvent.ACTION_MOVE :
{
x = event.getRawX();
y = event.getRawY();
parms.leftMargin = (int) (x-dx);
parms.topMargin = (int) (y - dy);
imageView.setLayoutParams(parms);
}
break;
case MotionEvent.ACTION_UP :
{
}
break;
}
return true;
}
});
}
});
You need to enable long click by code:
imageView.setLongClickable(true);
imageView.setClickable(true);
The last line is probably not necessary but it also makes no trouble.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With