Question

In calculator application, when the back/delete button is on pressed, not releasing it, the phone will vibrate and it will delete the entered numbers very quickly.

I have a EditText view, when the back/delete button is on pressed, it only deletes one character, I have to release and press it again in order to delete the next character. How can I make back/delete button to delete quickly when it is on pressed, and make the phone vibrate, the same behavior as the back/delete button in the calculator application?

Here is my EditText config

    <EditText
        android:id="@+id/left_value"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="5"
        android:background="@drawable/border"
        android:hint="@string/enter_numerial"
        android:inputType="numberDecimal|numberSigned"
        android:textSize="14sp"
        android:paddingLeft="2dip"
        android:maxLength="15"/>
Was it helpful?

Solution

I guess this is not the best solution but it will do what you need.

1). Let your Activity implement the View.OnTouchListener and set it

   yourButton.setOnTouchListener(this);

2). Create a class field of the CountDownTimer type. I call it mTimer.

Add the following to the OnTouch() method:

 @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

            deleteChar();

                break;

            case MotionEvent.ACTION_MOVE:
                /*--- no action required ---*/
                break;

            case MotionEvent.ACTION_UP:

                mTimer.cancel();

                break;
            }


            return false;
        }
    });

3). deleteChar() method:

    private void deleteChar(){
    // set the desired interval. I'm gonna use 500ms before last char deletion.
mTimer = new CountDownTimer(9999999, 500) {

        @Override
        public void onTick(long millisUntilFinished) {
            if(yourEditText.getText().toString().length() > 0){
            String input = yourEditText.getText().toString();
            input = input.substring(0, input.length()-1); 
            yourEditText.setText(input);
            }

        }

        @Override
        public void onFinish() {

        }
    };
    mTimer.start();
}

The above example deletes last character of an EditText with an interval of 1/2 sec. Use a Vibrator object when you need it.

For sure this code can be optimized. This should just give you the basic idea how to perform what you are asking for.

OTHER TIPS

Use OnKeyListener. The method the listener implements contains a KeyEvent object. You can check the event type to track if it is up or down, and use the keyCode to determine if it is the delete button.

If the action is down and the key code is the delete button, delete a character (perhaps in a runnable). When the action is up (or cancelled) and the del key was pressed, stop the deleting (maybe cancelling a runnable).

You will probably need to use an OnTouchListener to catch when the user presses and releases the button. You can then use a Handler callback to periodically delete a character/word/etc and use android.os.Vibrator service to do the vibrating.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top