Home Stop EditText from gaining focus at Activity startup

# Stop EditText from gaining focus at Activity startup

Mark
1#
Mark Published in 2009-10-12 15:03:22Z
 I have an Activity in Android, with two elements: EditText ListView When my Activity starts, the EditText immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried: EditText.setSelected(false);  No luck. How can I convince the EditText to not select itself when the Activity starts?
Pavel Ismailov
2#
Pavel Ismailov Reply to 2015-12-01 10:12:02Z
 Excellent answers from Luc and Mark however a good code sample is missing:  
3#
 Try clearFocus() instead of setSelected(false). Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
4#
 The only solution I've found is: Create a LinearLayout (I dunno if other kinds of Layout's will work) Set the attributes android:focusable="true" and android:focusableInTouchMode="true" And the EditText won't get the focus after starting the activity
bofredo
5#
 Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:  dummy.setNextFocusDownId(et.getId()); dummy.setNextFocusUpId(et.getId()); et.setNextFocusUpId(et.getId());  a lot of work just to get rid of focus on a view.. Thanks
Alexander Abakumov
6#
Alexander Abakumov Reply to 2017-08-11 17:02:33Z
 Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText? I don't really see an issue with the EditText having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText (and open the keyboard as a result). If it's the problem of the virtual keyboard, see the AndroidManifest.xml element documentation. android:windowSoftInputMode="stateHidden" - always hide it when entering the activity. or android:windowSoftInputMode="stateUnchanged" - don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
Hans Olsson
7#
Hans Olsson Reply to 2011-11-23 10:09:22Z
 try edit.setInputType(InputType.TYPE_NULL); edit.setEnabled(false); 
Jim
8#
 Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus() in onCreate()   That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Ziem
9#
 I use the following code to stop an EditText from stealing the focus when my button is pressed. addButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { View focused = internalWrapper.getFocusedChild(); focused.setVisibility(GONE); v.requestFocus(); addPanel(); focused.setVisibility(VISIBLE); } });  Basicly, hide the edit text and then show it again. This works for me as the EditText is not in view so it doesn't matter whether it is showing. You could try hiding and showing it in succession to see if that helps it lose focus.
bofredo
10#
 If you have another view on your activity like a ListView, you can also do: ListView.requestFocus();  in your onResume() to grab focus from the editText. I know this question has been answered but just providing an alternative solution that worked for me :)
Ziem
11#
 using the information provided by other posters, I used the following solution: in the layout XML   in onCreate() private AutoCompleteTextView mAutoCompleteTextView; private LinearLayout mLinearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); //get references to UI components mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete); mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus); }  and finally, in onResume() @Override protected void onResume() { super.onResume(); //do not give the editbox focus automatically when activity starts mAutoCompleteTextView.clearFocus(); mLinearLayout.requestFocus(); } 
Jack Slater
12#
Jack Slater Reply to 2011-06-12 00:41:21Z
 Try this before your first editable field:  ---- findViewById(R.id.dummyfocus).setFocusableInTouchMode(true); findViewById(R.id.dummyfocus).requestFocus(); 
bofredo
13#
 You can just set "focusable" and "focusable in touch mode" to value true on the first TextView of the layout. In this way when the activity starts the TextView will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...
rallat
14#
 None of this solutions worked for me. The way I fix the autofocus was:  
Jon
15#
 A simpler solution exists. Set these attributes in your parent layout:   And now, when the activity starts this main layout will get focus by default. Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this: findViewById(R.id.mainLayout).requestFocus();  Good comment from Guillaume Perrot: android:descendantFocusability="beforeDescendants" seems to be the default (integer value is 0). It works just by adding android:focusableInTouchMode="true". Really, we can see that the beforeDescendants set as default in the ViewGroup.initViewGroup() method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000; Thanks to Guillaume.
Satan Pandeya
16#
Satan Pandeya Reply to 2017-03-31 18:41:46Z
  
17#
 The problem seems to come from a property that I can only see in the XML form of the layout. Make sure to remove this line at the end of the declaration within the EditText XML tags:   That should give something like that :  // /* <-- without this line */ 
Ziem
18#
 The following will stop edittext from taking focus when created, but grab it when you touch them.   So you set focusable to false in the xml, but the key is in the java, which you add the following listener: etBonus.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.setFocusable(true); v.setFocusableInTouchMode(true); return false; } });  Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
jakeneff
19#
 I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition. myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); myLayout.setFocusableInTouchMode(true);  That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
Community
20#
 I had tried serval answer individually but the focus is still at the EditText. I only manage to solve it by using two of the above solution together.   ( Reference from Silver https://stackoverflow.com/a/8639921/15695 ) and remove   at EditText ( Reference from floydaddict https://stackoverflow.com/a/9681809 )
Lumis
21#
 The simplest thing I did is to set focus on another view in onCreate:  myView.setFocusableInTouchMode(true); myView.requestFocus();  This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.
Takhion
22#
 Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary! public static View preventInitialFocus(final Activity activity) { final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content); final View root = content.getChildAt(0); if (root == null) return null; final View focusDummy = new View(activity); final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { view.setOnFocusChangeListener(null); content.removeView(focusDummy); } }; focusDummy.setFocusable(true); focusDummy.setFocusableInTouchMode(true); content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0)); if (root instanceof ViewGroup) { final ViewGroup _root = (ViewGroup)root; for (int i = 1, children = _root.getChildCount(); i < children; i++) { final View child = _root.getChildAt(i); if (child.isFocusable() || child.isFocusableInTouchMode()) { child.setOnFocusChangeListener(onFocusChangeListener); break; } } } else if (root.isFocusable() || root.isFocusableInTouchMode()) root.setOnFocusChangeListener(onFocusChangeListener); return focusDummy; } 
Compaq LE2202x
23#
Compaq LE2202x Reply to 2013-05-23 10:42:18Z
 At onCreate of your Activity, just add use clearFocus() on your EditText element. For example, edittext = (EditText) findViewById(R.id.edittext); edittext.clearFocus();  And if you want to divert the focus to another element, use requestFocus() on that. For example, button = (Button) findViewById(R.id.button); button.requestFocus(); 
Piyush
24#
 Make sure to remove the line "" from the EditText tag in xml.  
John
25#
 EditText within a ListView does not work properly. It's better to use TableLayout with automatically generated rows when you are using EditText.
Mary's
26#
 You can specify focus to some other widget by using request focus and use the keyboard hiding code as well.
Piyush
27#
 Simple solution: In AndroidManifest in Activity tag use android:windowSoftInputMode="stateAlwaysHidden" 
Piyush
28#
 The following worked for me in Manifest. Write ,  
Nishal
29#
 You can use this   I got it from here http://tips.androidhive.info/2013/10/android-remove-edittext-border-on-foucs/
 For me, what worked on all devices is this:   Just put this as a view before the problematic focused view, and that's it.
 Disable it in onCreate() final KeyListener edtTxtMessageKeyListener = edtTxtMessage.getKeyListener(); edtTxtMessage.setCursorVisible(false); edtTxtMessage.setKeyListener(null);  And finally enable it in onClick() of EditText edtTxtMessage.setCursorVisible(true); edtTxtMessage.setKeyListener(edtTxtMessageKeyListener);  But the problem is with we have to click twise to bring OnScreenKeyboard for very first time. @Workaround InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);  Try this also in onClick() :)