Android Open Source - AndSpinner Auto Repeat Button






From Project

Back to project page AndSpinner.

License

The source code is released under:

GNU General Public License

If you think the Android project AndSpinner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor./*ww  w  . j  a  va 2s.co  m*/
 */
package com.deje.andspinnercontrol;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

/**
 *
 * @author detlef
 */
public class AutoRepeatButton extends Button {

  private long initialRepeatDelay = 500;
  private long repeatIntervalInMilliseconds = 50;

  private Runnable repeatClickWhileButtonHeldRunnable = new Runnable() {
    @Override
    public void run() {
      //Perform the present repetition of the click action provided by the user
      // in setOnClickListener().
      performClick();

      //Schedule the next repetitions of the click action, using a faster repeat
      // interval than the initial repeat delay interval.
      postDelayed(repeatClickWhileButtonHeldRunnable, repeatIntervalInMilliseconds);
    }
  };

  private void commonConstructorCode() {
    this.setOnTouchListener(new OnTouchListener()
    {
      @Override
      public boolean onTouch(View v, MotionEvent event)
      {
    int action = event.getAction(); 
    if(action == MotionEvent.ACTION_DOWN) 
    {
      //Just to be sure that we removed all callbacks, 
      // which should have occurred in the ACTION_UP
      removeCallbacks(repeatClickWhileButtonHeldRunnable);
    
      v.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
      //Perform the default click action.
      performClick();
    
      //Schedule the start of repetitions after a one half second delay.
      postDelayed(repeatClickWhileButtonHeldRunnable, initialRepeatDelay);
    }
    else if(action == MotionEvent.ACTION_UP) {
      //Cancel any repetition in progress.
      removeCallbacks(repeatClickWhileButtonHeldRunnable);
      v.getBackground().setColorFilter(Color.LTGRAY,PorterDuff.Mode.MULTIPLY);
    }
    
    //Returning true here prevents performClick() from getting called 
    // in the usual manner, which would be redundant, given that we are 
    // already calling it above.
    return true;
      }
    });
  }

    public AutoRepeatButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        commonConstructorCode();
    }


    public AutoRepeatButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        commonConstructorCode();
    }

  public AutoRepeatButton(Context context) {
    super(context);
    commonConstructorCode();
  }
}




Java Source Code List

com.deje.andspinnercontrol.AutoRepeatButton.java
com.deje.andspinnercontrol.MainActivity.java
com.hoho.android.usbserial.BuildInfo.java
com.hoho.android.usbserial.driver.CdcAcmSerialDriver.java
com.hoho.android.usbserial.driver.FtdiSerialDriver.java
com.hoho.android.usbserial.driver.UsbId.java
com.hoho.android.usbserial.driver.UsbSerialDriver.java
com.hoho.android.usbserial.driver.UsbSerialProber.java
com.hoho.android.usbserial.driver.UsbSerialRuntimeException.java
com.hoho.android.usbserial.util.HexDump.java
com.hoho.android.usbserial.util.SerialInputOutputManager.java