Hold and click actions on same View - android

In an Android app, I have a feature to record audio. The idea is to have a button that has 2 types of actions.
I can click on button and start recording, and when I click again it stops recording.
I can hold the button and while its being held the app records, when I release it, the app stops recording.
I tried with a OnTouchListener
private static int CLICK_ACTION_THRESHHOLD = 250;
public long lastTouchDown;
public boolean isClick;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()){
case R.id.main_record_button:
case R.id.main_record2:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastTouchDown = System.currentTimeMillis();
//... do stuff
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - lastTouchDown < CLICK_ACTION_THRESHHOLD) {
isClick = true;
//...do other stuff
}
Whats the best way to achieve this ?

OnTouchListener is not very intuitive to work with.
I have used GestureDetector to track userbehavior. I think that is what you should use. Check out the implementation and callbacks here: https://developer.android.com/training/gestures/detector.html

You should go through this library to understand whats he is doing or you can use it too.
Also the this Thread.

Related

How to perform a Long Click event on a button where onclick is disabled in Android?

Consider an Activity where button named "Scan", is disabled as soon as Bluetooth device gets connected, for preventing further scanning.
mScan.setEnabled(false);
But as soon as it is connected I need the same button mScan to show some dialog which has additional functionalities when it is Long Pressed.
The problem is as I disabled the mScan button I can't perform onLongClick function.
How Can I achieve the same?
Seeking your help.
You can use a custom OnTouchListener:
long lastDown;
long lastDuration;
public class YourOnTouchListener implements View.OnTouchListener
{
public YourOnTouchListener(some parameters) {
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
if (lastDuration > the duration you want) {
//do what you want here
return true;
}
return false;
}
}
Then you can control what you want to do according to a duration, and it works with a OnClickListener as well.

Detect ongoing touch event

I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}

how to speed up the forward/rewind process when button pressed continuously

I want to increase the forward/rewind speed if the button is pressed continuously.
I created my customized media controller from here.
What would be the good way to speed up the forward/rewind process?
I got some idea form here. Finally I implemented it too but the touch event worked successfully in my emulator or and android mobile too but didn't worked for android stb. Other solutions rather than using it would be appreciated.
How about you monitor the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP and see how long the button is pressed.
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// the button is down monitor time to see how long it is down or anything you want
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the button is up reset your timer
}
}
};

Android 15 second long click with ACTION_UP always called upon release

I am pulling my hair out trying to find a reliable way to consistently allow a user to long press on a gridview item for up to 15 seconds and ALWAYS ALWAYS receive a ACTION_UP when the users finger is released. I also would like to allow unlimited movement once a long press is detected NOTHING besides a finger being lifted should interrupt this process.
Some additional info this is all inside of a gridview which is inside of a pager , but ik what i want to do is possible bc ik of other apps that do such a thing flawlessly
Here is my most recent attempt, but ive tried dozens:
public class recordVideo implements OnLongClickListener,OnTouchListener {
private int position;
private String clicked_uid;
public recordVideo(int position){
this.position=position;
}
#Override
public boolean onLongClick(View v){
//do things
// Do something when your hold starts here.
isSpeakButtonLongPressed = true;
Log.i("START_RECORD","longClick");
return true;
}
#Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
if (pEvent.getAction() == MotionEvent.ACTION_DOWN) {
Log.i("START","video");
}
// We're only interested in when the button is released.
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
// We're only interested in anything if our speak button is currently pressed.
Log.i("STOP_RECORD","video");
if (isSpeakButtonLongPressed) {
// Do something when the button is released.
isSpeakButtonLongPressed = false;
// Log.i("STOP_RECORD","video");
}
}
return true;
}

How to detect if a device is tapped with a pen/stick or something

Hi I want to find out if a tablet is hit with a stick lightly. I thought maybe implementing accellerometer should do it. But all I see people are trying to find shake movements with it. Is there any way I can find if a tablet is tapped? Thanks.
EDIT: By saying tapping I mean tapping on top of the tablet. Not in screen.
I'd say the easier route for detecting a tap would be by measuring the time between a MotionEvent.ACTION_DOWN and the corresponding ACTION_UP - if it's less than, say, 350 milliseconds, the user was probably doing a tap and not a press or some other sort of gesture.
Consider something like the following, where TAP_CONSTANT would be an integer constant for the tap duration:
private class TapListener implements OnTouchListener
{
long time;
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
time = System.currentTimeMillis();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
if ((System.currentTimeMillis() - time) <= TAP_CONSTANT)
{
// We have a tap event, call our tap logic methods!
return true;
}
}
return false;
}
}

Categories

Resources