1

In my app I need to use one click and long press on a button

on click for some thing(calling Itemclick()) and long press for record sound

and use this code:

 send.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
            {
                recording =false;
                t=new Timer();
                t.scheduleAtFixedRate(new TimerTask() {

                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                if(count>=2)
                                    t.cancel();
                                    count++;

                            }
                        });
                    }
                }, 1, 1000);
                break;
            }    
            case MotionEvent.ACTION_MOVE:
            {
                if(count >=2&&!recording)
                {

                     recording=true;
                     count=0;
                     Rec=new Recorder();
                     Rec.startRecord();
                }
                break;

            }
            case MotionEvent.ACTION_UP:
            { 
                t.cancel();
                if(!recording)
                {
                    count=0;
                    Itemclick();
                    break;
                }
                recording=false;
                Rec.StopRecord();
                db.open();
                db.insert_offline(Rec.getName(), true, false);
                listItems.add(Rec.getName());
                type.add("v");
                adapter.notifyDataSetChanged();
                db.close();
                Rec=null;
                break;
            }
           }
            return false;
        }
    });

In this code I use a timer for finding long press but I want an easily and faster way to do this.

Do you have any idea or better solution ?

1 Answers1

6

You can probably replace the timer and use android's built in event listeners for click, and hold events:

View.OnLongClickListener
View.OnClickListener

Going this route, your code will look something like the following.

send.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //code for click event
    }
});

send.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        //code for hold event... which sounds like you want to begin recording here
        return true; //indicate we're done listening to this touch listener
    }
});

send.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_UP:
            { 
            //stop recording voice if a long hold was detected and a recording started
            return true; //indicate we're done listening to this touch listener
            }
        }
        return false;
    }
});
  • one question :why "return true" for "onLongClick" in your why "return false".which different? –  Jul 15 '16 at 07:34
  • Taken from this SO post: http://stackoverflow.com/questions/7333608/ontouchevent-onclick-onlongclick-calls onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners. – Nicholas Ackerman Jul 15 '16 at 12:49