2

I am new to android, please help me to get the solution for this question.

I need to update TextView of my android application when the user clicks volume up button, each time when user clicks the button the count has to increment like 1,2,3,4,5... for this I have done as follows

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
            switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {

                    int current =  Integer.parseInt((String)counter_view.getText());
                    counter_view.setText(current+1+"");

                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
            }
        }

This code is working but the problem with this is textview updates in very slow,if I press the button continuously then it takes little time to update textview .is there any another method to do this ?

programr
  • 25
  • 8
  • hey...@programr....did you notice, case KeyEvent.KEYCODE_VOLUME_UP: will be fired when you click on volume up..then how can you check it if() inside of it? – GvSharma Nov 20 '14 at 12:25
  • Have you tried `action == KeyEvent.ACTION_UP` instead? – ChuongPham Nov 20 '14 at 12:29
  • Guys, I don't have any problem with the key press event. only about updating TextView – programr Nov 20 '14 at 12:31
  • @BadAttemtsFirst I have referred the code from this link http://stackoverflow.com/questions/2874743/android-volume-buttons-used-in-my-application – programr Nov 20 '14 at 12:31
  • You can use either `runOnUiThread` or `AsyncTask` to update components in a UI thread directly. – ChuongPham Nov 20 '14 at 12:33
  • @ChuongPham I am also looking the same, I think AsyncTask would be better can you gimme any link or tutorial about this , then it would be a great help. – programr Nov 20 '14 at 12:38
  • You can read about `AsyncTask` [here](http://developer.android.com/reference/android/os/AsyncTask.html) in the official Android documentation. – ChuongPham Nov 20 '14 at 12:48

3 Answers3

0

Try to updated textview inside runOnUiThread() :

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
   switch (event.getKeyCode()) {
     case KeyEvent.KEYCODE_VOLUME_UP:
          if (event.getAction() == KeyEvent.ACTION_DOWN) {
              runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                   int current =  Integer.parseInt((String)counter_view.getText());
                   counter_view.setText(current+1+"");        
                 }
              });
          }
          return true;
     default:
          return super.dispatchKeyEvent(event);
    }
 }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

Try something like:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int current = Integer.parseInt((String) counter_view.getText());
                        counter_view.setText(current + 1);        
                    }
                });
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }
}
ChuongPham
  • 4,761
  • 8
  • 43
  • 53
0
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(KEYCODE_VOLUME_UP==keyCode)
        {
            //do what you want to do here
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

In this your key event will be detected very fast and also you can change your textbox value fast. just enter keycode of volume up in KEYCODE_VOLUME_UP so every time when you press that key than your textbox value will be changed not and your volume app function won't work at that time because we are returning true instead of parent class event.

Aiyaz Parmar
  • 562
  • 6
  • 17