3

I have an Android activity and there is one EditText in the whole layout. For some reason, whenever the activity starts, the keyboard comes up. I have tried all of the following things:

  • Placing these two in OnStart:

    FindViewById<EditText> (Resource.Id.searchText).ClearFocus ();
    FindViewById<EditText> (Resource.Id.searchText).Selected = false;
    
  • Adding a LinearLayout with these two properties:

    android:focusable="true" android:focusableInTouchMode="true"
    
  • Adding the following tag inside another View on the layout:

    android:focusable="true" android:focusableInTouchMode="true"
    
  • Adding this to my activity manifest:

    android:windowSoftInputMode="stateHidden"
    

But still, the keyboard opens when the activity opens. What could I be doing wrong?

poupou
  • 43,413
  • 6
  • 77
  • 174
James Monger
  • 10,181
  • 7
  • 62
  • 98
  • To whoever is viewing this and considering flagging it as a duplicate, please don't. I've read through [this question](http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup) and have tried everything. – James Monger Aug 23 '13 at 16:20

5 Answers5

7

Try these for Xamarin.Android (Cross Platform)

this.Window.SetSoftInputMode (SoftInput.StateHidden);

Or

Add this to manifest,

[Activity(Label = "SampleApp", MainLauncher = true, Icon = "@drawable/icon", WindowSoftInputMode = SoftInput.StateHidden)]
Baris Demiray
  • 1,539
  • 24
  • 35
Nitin V. Patil
  • 185
  • 2
  • 2
4

Try this -this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

kirankk
  • 628
  • 1
  • 9
  • 22
2

Add this to manifest file...

<activity android:name=".YourActivity" android:windowSoftInputMode="stateHidden"  />
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
0

Android by default targets the first focusable object. Whilst kirankk's answer works for standard activities, if you're using a DialogFragment with a custom view, the much easier option (which incidentally also works for standard activities) is to make the ViewGroup (ie LinearLayout/RelativeLayout) that your text view is contained in focusable and focusable in touch mode. This can be done from the AXML and prevents the TextView being the first focusable view.

0

I know this is super old, but find that adding this to your root view in the XML file usually helps to prevent this issue:

 android:descendantFocusability="beforeDescendants"
 android:focusableInTouchMode="true"
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Zee
  • 1,592
  • 12
  • 26