0

I have the following xml file in my android app. When user opens the app, the keyboard becomes active at the same time. Even though user has not even started in the EditText.

How could I control keyboard? I want keyboard to appear when a user tap on the EditText.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mList"
        android:minWidth="25px"
        android:minHeight="25px">
    <EditText
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search" />
   <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/mListView" />
   </LinearLayout>
casillas
  • 16,351
  • 19
  • 115
  • 215

2 Answers2

1

Modify your axml file as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mWellList"
    android:minWidth="25px"
    android:minHeight="25px"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >

or do it with code:

var root = FindViewById<LinearLayout>(Resource.Id.mWellList);
root.RequestFocus();

as described here: https://forums.xamarin.com/discussion/1856/how-to-disable-auto-focus-on-edit-text

To hide the Keyboard:

Include:

 using Android.Views.InputMethods;  

and then:

InputMethodManager imm = (InputMethodManager) this.GetSystemService(Context.InputMethodService); 
imm.HideSoftInputFromWindow(YourEditTextHere.WindowToken, 0);

Also check this thread for clearing focus on touch outside: EditText, clear focus on touch outside

Community
  • 1
  • 1
CDrosos
  • 2,418
  • 4
  • 26
  • 49
  • Ok that works perfectly fine. In my example, I have `ListView` and `EditText`. Lets assume that user click on `Edittext` and keyboard focus and then user change his mind not to type anything. How you dismiss keyboard? By the way I have marked your answer and upvoted. – casillas Oct 08 '15 at 22:28
  • Thanks CDRosos, but I am getting the following error the `GetSystemService is not in the current context` – casillas Oct 08 '15 at 22:36
  • I have edit the code, try again please. Are you using fragments or Activity? – CDrosos Oct 08 '15 at 22:40
  • I am still having the same issue, I am using fragments – casillas Oct 09 '15 at 14:55
  • try with this: InputMethodManager inputManager = (InputMethodManager) this.GetSystemService(Context.InputMethodService); inputManager.HideSoftInputFromWindow(this.CurrentFocus.WindowToken, HideSoftInputFlags.NotAlways); – CDrosos Oct 09 '15 at 16:02
0

Add in the manifest file inside the activity tag

android:windowSoftInputMode="stateAlwaysHidden"

like Below

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:windowSoftInputMode="stateAlwaysHidden" >
  <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46