OnLongClickListener not working
Asked Answered
I

2

6

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;
    }
});
    }


 });
Inerrable answered 25/6, 2013 at 5:16 Comment(4)
once View.OnLongClickListener replace it with OnLongClickListener and try.Typeset
@Harish: I tried. Is it possible to use OnTouchListener, OnLongClickListener for same imageView?Inerrable
instead of OnTouchListener use OnClickListener.Typeset
Finally got it..Use return false for OnTouchListener, both will works fineInerrable
M
21

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.

Mineralogy answered 25/6, 2013 at 5:19 Comment(4)
As per his requirement, only setLongClickable() is enough.Fennessy
Is it possible to test longclick in emulator? I press mouse longclick nothing happen.Inerrable
I just started a project with longclick in the emulator it works for me.Mineralogy
I used imageView.setLongClickable(true); above. But toast also won't appear.Inerrable
T
12

Its not necessary to do setLongClickable in the code, its better when you define it in the XML

 android:longClickable="true"
Tantara answered 25/6, 2013 at 5:29 Comment(1)
UI stuff in XML mostly always better +1Solfeggio

© 2022 - 2024 — McMap. All rights reserved.