Reputation:
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 ?
Upvotes: 1
Views: 2259
Reputation: 401
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;
}
});
Upvotes: 6