4

I have set a button to focusable and focusableInTouchMode to true (I need it to be focusable). Now when I click the button, the onCilck event cannot be triggered (it is gaining focus and ignore onClick event), only when I cilck again the onClick event will be triggered.

I found a solutions like this:

input.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { v.performClick(); } } }); 

But this solution will cause some side effect as well, for example, if we click 'next' in the keyboard and jump focus to the button, onClick will fire. This is not what I want.

This issue is really anoying, is this a bug? Or it is intentional behaviour? Anyway to solve this?

2
  • possible duplicate of Android Button needs two click for action Commented Dec 6, 2013 at 7:49
  • I already mentioned I need the button to be focusable for some reason. Commented Dec 6, 2013 at 8:53

2 Answers 2

2

set focusableInTouchMode false. Or just don't include that tag at all. Let it take its default value, whatever it is supposed to.

Sign up to request clarification or add additional context in comments.

1 Comment

I already mentioned I need the button to be focusable for some reason.
1

I have found a solution to avoid this issue by using the following code.

final View.OnTouchListener requestFocusTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN && !v.isFocused()) { v.requestFocus(); } return false; } }; findViewById(R.id.xxx1).setOnTouchListener(requestFocusTouchListener); findViewById(R.id.xxx2).setOnTouchListener(requestFocusTouchListener); findViewById(R.id.xxx3).setOnTouchListener(requestFocusTouchListener); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.