How to manage the duration of LongClick? - java

my problem is :
As in the Instagram's Video recording activity, I whant to perform an action(record audio) for the time that a button is pressed..
than when the button is released i whant to perform another action (the saving of the audio file and the release of the resoruces)
thaks you in advantage

You can use OnTouchListener :
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
//start recording
}
else if(arg1.getAction() == MotionEvent.ACTION_UP) {
//stop recording
}
return true;
}
});

Related

Android - BottomSheet not closing properly

Situation : I have a BottomSheet that expands on a button click (fig. 1), on the upper right corner of the BottomSheet I put an ImageView that works as a closing button, in other words when the ImageView is clicked the Bottomsheet state will be changed to HIDDEN
what I tried :
I tried to assign the imageview an onTouchListener (check code take #1) but I got a NullPointerException mainly because the bottomsheet was hidden and the imageview is not visible to user so I tried adding an if to check if the bottomsheet state is expanded and if so do the closing stuff (check code take #2)
Code take #1
//this is where I got the NullPointerException
CloseSheet.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
}
else if(event.getAction() == MotionEvent.ACTION_UP){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL){
}
return true;
}
});
Code take #2
//this is where I dont get a response on click
if(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED ){
CloseSheet.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
}
else if(event.getAction() == MotionEvent.ACTION_UP){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL){
}
return true;
}
});
}

android - how to run some code after the "power button" is pushed

I have an app that plays some music file while the application is open but when i press the power button the sound keeps playing for some reason. Is there a way to override the power button so it will also stop the media player when pushed ?
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
...
...
mp = MediaPlayer.create(this, mAudio[0]);
mp.start();
...
...
}
I would like to call mp.stop() when the power button is pressed.
Override onKeyDown and check if it's KEYCODE_POWER.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// Stop the media player here
return true;
}
return super.onKeyDown(keyCode, event);
}
Add this method:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// stop the media from playing
return true;
}
return super.onKeyDown(keyCode, event);
}
This method is being activated when a key is being pressed. It checks what key is pressed and reacts for it.
First of all you need to understand, when you want to pause or stop the sound. If you want to do it, when you exit/hide an application, then overriding onKeyDown won't help. I think better approach in this case will be:
protected void onCreate(Bundle onSaveInstanceState) {
super.onCreate(onSaveInstanceState);
mp = MediaPlayer.create(this, mAudio[0]);
}
protected void onStart() {
super.onStart();
mp.start();
}
protected void onStop() {
super.onStop();
mp.stop();
}
But if you want to stop your sound only when Power button is pressed, then yes, you need to override onKeyDown

App stops unexpectedly when using OnTouchListener, MotionEvent.ACTION_UP

So I want to create buttons that play music as long as it is pressed, but once it is released, the music stops.
In my createListeners(), I have the following:
b1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
startBeat(1, m1);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
m1.stop();
}
return false;
}
});
m1 is a MediaPlayer, and in the method startBeat, m1.start() has been called.
When running the app, the music plays fine, as long as I don't release the button. However, the moment I release the button, the app says it stopped unexpectedly. What could be causing this problem?
Is there another way I could go about implementing this feature?
Maybe you try play again the MediaPlayer ??
Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.
Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.
If you don't, try m1.pause(); and m1.seekTo(0) to simulate the stop...
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
startBeat(1, m1);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
m1.pause();
m1.seekTo(0);
return true;
}
return false;
}
EDIT: I found out what I was doing wrong. I was initializing and starting the MediaPlayer in my startBeat method, and stopping the player in the onTouch method. Once I moved it all into onTouch, it worked. So I guess something weird was happening before. Thanks for the answers!

How to detect when button pressed and released on android

I would like to start a timer that begins when a button is first pressed and ends when it is released (basically I want to measure how long a button is held down). I'll be using the System.nanoTime() method at both of those times, then subtract the initial number from the final one to get a measurement for the time elapsed while the button was held down.
(If you have any suggestions for using something other than nanoTime() or some other way of measuring how long a button is held down, I'm open to those as well.)
Thanks!
Andy
Use OnTouchListener instead of OnClickListener:
// this goes somewhere in your class:
long lastDown;
long lastDuration;
...
// this goes wherever you setup your button listener:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
lastDown = System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
}
return true;
}
});
This will definitely work:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
return true;
}
});
In onTouchListener start the timer.
In onClickListener stop the times.
calculate the differece.

How do I set an OnTouchListener for a relativeLayout?

I want to capture two events from a relativeLayout. When the user clicks it and when the user releases his click (like mousedown/mouseup)
rl.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//
}
});
I tried setting up something like:
if(event == MotionEvent.ACTION_DOWN) {
}
But eclipse just throws an error about an incompatible operand type. Anyone who knows how to do this?
Try...
if (event.getAction() == MotionEvent.ACTION_DOWN) {
...
}

Categories