39

I am using both onClickListener and onLongClickListener for a TextView in a ListView. I see that in Android 1.6, the long click listener is fired along with the on click listener meaning both are fired when I long click. But this is not the case in the future versions. Is there any fix for this?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

  if (convertView == null) {
    LayoutInflater inflater = getLayoutInflater();
    row = inflater.inflate(R.layout.row, parent, false);
  }

  TextView tv = (TextView) row.findViewById(R.id.tv);

  tv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        showMessage();
      }
  });

  tv.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        showLongMessage();
      }
  });
}
ישו אוהב אותך
  • 22,515
  • 9
  • 59
  • 80
dcanh121
  • 4,509
  • 10
  • 34
  • 80

4 Answers4

125

Did you return boolean true at the end of OnLongClickListener to indicate you don't want further processing?

winklerrr
  • 6,735
  • 2
  • 45
  • 60
Tsan-Kuang Lee
  • 1,434
  • 1
  • 14
  • 9
14

I think you you should use OnItemLongClickListener() instead of OnLongClickListener().

See developers website for further response

Mutawe
  • 6,399
  • 3
  • 43
  • 88
varun bhardwaj
  • 1,492
  • 13
  • 23
13
TextView t1 = (TextView) findViewById(R.id.textView1);
t1.isClickable();

t1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();
    }
});

t1.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
});
alexgophermix
  • 3,785
  • 5
  • 30
  • 54
selva_pollachi
  • 4,047
  • 4
  • 26
  • 40
3
itemToClick.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v)      { 
      //do your logic on click 
     });
itemToClick.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
      // do your logic for long click and remember to return it 
        return true; }});
Nouman Shah
  • 514
  • 8
  • 22