Android call event continously when press button - android

I have to develop one apps, in that i want to fire one action, like button is vibrate contentiously during press,i leave press it stop vibration.
In onTouch - event occur only during press,but when i press button contentiously -> event occur contentiously
like i press button for 1 minute it vibrate for same time,if leave press it stop.
i don't know which method used for this, so, please any one help me to do like this.
my code is below to do that : but it not work for continuous action during press (using thread it work for vibrate only, if i join more code with that it give error see in following.
**Edit : **
i got solution for vibrate using like below but when i write code (below vibrator.vibrate(100); ) for some animation that continuous during press then i got error : Only the original thread that created a view hierarchy can touch its views. i also try with runOnUIThread also but with that it not work.
img_laser_ballpen.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MainActivity.this.vibrating = true;
img_laser_light.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
while (MainActivity.this.vibrating) {
// ADD CODE FOR MAKING VIBRATION
vibrator.vibrate(100); // it works properly
//Animation shake = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.shake);
//img_laser_light.startAnimation(shake); //if it open then give error
}
//
}
}).start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
MainActivity.this.vibrating = false;
img_laser_light.setVisibility(View.INVISIBLE);
}
return vibrating;
}
});

You could like this
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
// Start vibrating
}else if (event.getAction() == MotionEvent.UP){
// Stop vibrating
}
}
});

This answer has a good example of catching these events; namely - you should use the OnTouchListener instead of OnClickListener.
// 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) {
// START VIBRATE HERE
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// STOP VIBRATE HERE
return true;
}
}
};
Edit:
You should return true after handling your vibrate so that other events are fired for this view. From the documentation:
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

You could opt for the following:
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
boolean vibrating = true;
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View view, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
YOUR_CLASS_NAME.this.vibrating = true;
new Thread(
new Runnable(){
public void run(){
while(YOUR_CLASS_NAME.this.vibrating){
//ADD CODE FOR MAKING VIBRATION
}
}
}
).start();
}else if (event.getAction() == MotionEvent.UP){
YOUR_CLASS_NAME.this.vibrating = false;
}
}
});

Related

Android OnTouchEvent not fire at first touch

OnTouchEvent is being fired after the second time touch; yet, I want it to be fired after the first touch itself.
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
//too avoid touch being detected continuously
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (Function.system[5] == "Gravity: on") {
//some action...
}
}
return super.onTouchEvent(event)
}
You need to tell Android to continue to respond to touch events. Returning true let's you capture events from action down , action move and then action up.
Please add the return true for each event
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
//too avoid touch being detected continuously
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
return true;
}
Please try and seee if this works for you

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.

Click, Press and Release event on Button

How can I detect clicked, pressed and released states of a Button. I want to perform different functions on these states. On click I want to call function1, on press I want to call function2 and on receive I want to call function3.
We can detect click state using View.OnClickListener. We can detect Pressed and Released states of a Button using View.OnTouchListener and handling ACTION_DOWN and ACTION_UP. I am able to detect these states individually, however, not together.
Below is code for OnCLickListener.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" clicked ");
}
});
Below is code for OnTouchListener.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(" pressed ");
return true;
case MotionEvent.ACTION_UP:
System.out.println(" released ");
return true;
}
return false;
}
});
When I set click and touch listeners on a Button, Click event never gets called. Instead I receive pressed and released state.
How can I handle these three states together?
EDIT:
I added the OnClickListener and OnTouchListener code I have used.
Easy since Button is a View:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Released
}
return true;
}
});
Change the return true; inside case MotionEvent.ACTION_DOWN: and case MotionEvent.ACTION_UP:to return false; or break;
See this link.
You can handle click manually in MotionEvent.ACTION_UP event by
button.performClick();
clicked event include pressed and released state,if you want to fire clicked event,put method after ACTION_UP

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;
}

Keep pressed a button in onClick()

I want to keep a button displayed pressed. I want to use another way instead of onPressed() in onTouch(). I add setPressed(), setSelected() but not worked. when i add these methods in onTouch() the program is good but my animation is very slow.
Can i use these method in onClick() method but works in this?
Please explain for me
Instead of using the onClick-event which returns the button to the non-pressed state, you can set the pressed state in the onTouch-event:
yourbutton.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
((Button) v).setPressed(true);
//TODO: Add the code of your onClick-event here
}
return true;//Return true, so there will be no onClick-event
}
});
If this affects any animation, you should look for the problems there.
This piece of code makes the button work as a toggle button. It sets the pressed property to its opposite.
If the button is pressed, it's set as unpressed.
If the button is unpressed, it's set as pressed.
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Button b = ((Button) v);
b.setPressed(!b.isPressed());//if pressed, unpress; if unpressed, press
}
return true;
}
What you are describing seems to be a toggle button. Maybe you should check this out, instead of doing a hackish solution :
Toggle Buttons
Extend ToggleButton or:
Button button = (Button) view.findViewById(R.id.button1);
button.setTag(false);
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
if ((boolean) v.getTag()) {
v.setPressed(false);
v.setTag(false);
return false;
} else {
v.setPressed(true);
v.setTag(true);
return true;
}
return false;
}
});

Categories

Resources