Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus on a view when a layout is created and displayed?

Currently, I have a layout which contains a Button, a TextView and an EditText. When the layout is displayed, the focus will be automatically put on the EditText, which will trigger the keyboard to show up on Android phone. It is not what I want. Is there any way that I can set the focus on TextView or on nothing when a layout is displayed?

like image 826
user256239 Avatar asked Jan 27 '10 22:01

user256239


People also ask

How do you set focus on a view?

If you are using java code to generate layout then you can use methods setFocusable(boolean) and setFocusableInTouchMode(boolean). You can then set the focus to a view using public method requestFocus(). The framework will handle routine focus movement in response to user input.

What is focusable in Android Studio?

Focusable in touch mode is a property that you can set yourself either from code or XML. However, it should be used sparingly and only in very specific situations as it breaks consistency with Android normal behavior.

What is the use of requestFocus in Android?

RequestFocus() Call this to try to give focus to a specific view or to one of its descendants.


2 Answers

This works:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
like image 28
Charles Mosndup Avatar answered Oct 17 '22 20:10

Charles Mosndup


Set focus: The framework will handled moving focus in response to user input. To force focus to a specific view, call requestFocus()

like image 181
Mark B Avatar answered Oct 17 '22 19:10

Mark B