0

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;
        }
    });
Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42

3 Answers3

0

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.

Community
  • 1
  • 1
CodeMonkey
  • 1,426
  • 1
  • 14
  • 32
0

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
         }
     }
});
bhargavg
  • 1,383
  • 9
  • 12
0

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;
         }
     }
});
  • thanks @Hiten it works for vibrate but i have also other things to put on that to occur continuously, i try to do that it give **Only the original thread that created a view hierarchy can touch its views. ** error, i try with **runOnUiThread** but with that not work – Nirav Mehta Sep 01 '14 at 09:14
  • Maybe you can make your class implement runnable and put the run method in the class itself. So the thread code will become new Thread(this).start() – Hiten Naresh Vasnani Sep 01 '14 at 09:19
  • no its give the same error,vibrate work proper but due to extra code i include in that so it give error, so any other method to work with both ? – Nirav Mehta Sep 01 '14 at 09:30
  • oh.. hmm. what other task specifically do you want to do? maybe you can declare the UI object variables globally in the class and access the global UI objects using the same YOUR_CLASS_NAME.this.variableName – Hiten Naresh Vasnani Sep 01 '14 at 09:34
  • In which function call is it showing the error. getApplicationContext() or R.anim.shake or what? – Hiten Naresh Vasnani Sep 01 '14 at 09:51
  • at line of call ** img_laser_light.startAnimation(shake);** – Nirav Mehta Sep 01 '14 at 09:56