This question already has an answer here:
I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?
You can add this to
onCreate
and it will hide the keyboard everytime the activty starts.You could also programatically change the focus to another item.
Add
LinearLayout
beforeEditText
in your XML.Or you can do this same thing by adding these lines to view before your 'EditText'.
In the comments you asked if another view can be focused instead of the EditText. Yes it can. Use
.requestFocus()
method for the view you want to be focused at the beginning (in onCreate() method)Also focusing other view will cut out some amount of code. (code for hiding the keyboard for example)
I had the same problem. It made me more than crazy.
I had an extended Dialog with a ScrollView that had a TableLayout with extended LinearLayout that contained a SeekBar and a EditText.
The first EditText had always autofocus after showing the Dialog and after finishing editing the text over the keyboard the EditText still had the focus and the keyboard was still visible.
I tried nearly all solutions of this thread and none worked for me.
So here my simple solution: (text = EditText)
By the way I didn't used any of the following snippets to solve it:
AND I didn't used a spacer item like a View with width and height of 1dp.
Hopefully it helps someone :D
This is my very first answer on SO, so don't be too harsh on me if there are mistakes. :D
There are few answers floating around the SO, but I feel the urge to post my complete solution cause this drove me nuts. I've grabbed bits and pieces from all around so forgive me if I don't give respective credits to everyone... :)
(I'll simplify my result cause my view has too many elements and I don't wanna spam with that and will try to make it as generic as possible...)
For your layout you need a parent your EditText and parent view defined something like this:
So, I needed a few things here. I needed to have a Placeholder for my EditText - which is that -
Also,
made it happen for EditText not to be focused on entering the Activity and later on in the Activity itself when setting it this setting helps so you can set onTouchListener on it to steal the focus away from EditText.
Now, in the Activity:
Few of the answers for bits I found on this question page, and the part with the Activity solution I found on this blog. The rest I missed which I had to figure out myself was clearing focus on the EditText which I added to both inside the setOnEditorActionListener and onTouchLister for the parent view.
Hope this helps someone and saves their time. :)
Cheers, Z.
remove autofocus edittext android
It's working for me
Edit In the link they suggest to use LinearLayout, but simple View will work
Then if this "thief" is placed at the top of the layout (to be first focusable item) calls to
clearFocus()
will work.