11

I need to know when the user touches/taps/clicks the edittext in my activity.

How can I do this without interrupting the events, so the keypad still displays properly?

(And I need to know about it before the OS displays the keypad...if possible)

just_another_coder
  • 1,156
  • 2
  • 13
  • 22

5 Answers5

29
txtEdit.setOnTouchListener(new View.OnTouchListener(){
    public boolean onTouch(View view, MotionEvent motionEvent) {                                                       
         // your code here....
         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);                
         return false;
    }
});
Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
1
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_UP){

...


}
letsdev-cwack
  • 99
  • 4
  • 10
  • Thank you for posting an answer to this question! Code-only answers are discouraged on Stack Overflow, because it can be difficult for the original poster (or future readers) to understand the logic behind them. Please, edit your question and include an explanation of your code so that others can benefit from your answer. Thanks! – Maximillian Laumeister Nov 30 '15 at 00:56
0

You should be able to do this by attaching an OnClickListener to your EditText. If you're concerned about blocking the UI thread in your OnClickListener, you can spawn a new Thread and do your work in there - though, if you do that, there's no guarantee the work will be done before the keypad shows up.

iandisme
  • 6,346
  • 6
  • 44
  • 63
  • If I attached a new onFocusChangeListener, it interrupts the keypad, and it has to be pressed twice to show it. When I override, is there a way to call super or something? Maybe I'm reaching... – just_another_coder Aug 02 '10 at 16:10
  • 1
    OK. Try looking at this... http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext – iandisme Aug 02 '10 at 16:19
  • Actually, that was the example I was following. I also tried getting the activity window and setSoftInputMode, but it always takes 2 taps to reveal the keypad. – just_another_coder Aug 02 '10 at 16:30
0

Less verbosity

The same Mathias Conradt's approach, but using kotlin:

txtEdit.setOnTouchListener({ view, motionEvent ->
    // your code here....
    false
})

This line getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); is unnecessary since the keyboard shown when you tap the editText view.

Filipe Brito
  • 5,329
  • 5
  • 32
  • 42
0

OnTouchListener was getting called twice for me (on Pixel 2 running Pie). So I used OnFocusChangeListener instead:

txtEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Log.d(TAG,"Focused Now!");
                }
            }
        });
Mohit Singh
  • 1,452
  • 1
  • 18
  • 27