Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android show softkeyboard with showSoftInput is not working?

I have created a trivial application to test the following functionality. When my activity launches, it needs to be launched with the softkeyboard open.

My code does not work?!

I have tried various "state" settings in the manifest and different flags in the code to the InputMethodManager (imm).

I have included the setting in the AndroidManifest.xml and explicitly invoked in the onCreate of the only activity.

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.mycompany.android.studyIme"       android:versionCode="1"       android:versionName="1.0">     <uses-sdk android:minSdkVersion="7" />      <application android:icon="@drawable/icon" android:label="@string/app_name">         <activity android:name=".StudyImeActivity"                   android:label="@string/app_name"                    android:windowSoftInputMode="stateAlwaysVisible">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>      </application> </manifest> 

... the main layout (main.xml) ...

<?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"     >     <TextView           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="@string/hello"         />     <EditText         android:id="@+id/edit_sample_text"         android:layout_width="fill_parent"          android:layout_height="wrap_content"         android:hint="@string/hello"         android:inputType="textShortMessage"     /> </LinearLayout> 

... and the code ...

public class StudyImeActivity extends Activity {     private EditText mEditTextStudy;      /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          mEditTextStudy = (EditText) findViewById(R.id.edit_study);         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);         imm.showSoftInput(mEditTextStudy, InputMethodManager.SHOW_FORCED);     } } 
like image 615
mobibob Avatar asked Apr 01 '11 23:04

mobibob


People also ask

How to show soft keyboard Android?

By default, the soft keyboard may not appear on the emulator. If you want to test with the soft keyboard, be sure to open up the Android Virtual Device Manager ( Tools => Android => AVD Manager ) and uncheck "Enable Keyboard Input" for your emulator.

How to show and hide soft keyboard in Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

What is Android soft keyboard?

The Android system shows an on-screen keyboard — known as a soft input method — when a text field in your UI receives focus. The keyboard takes about half the screen; in other words, you only have half of the screen to display any information.


1 Answers

When the activity launches It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):

First Method

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); editText.postDelayed(new Runnable() {     @Override     public void run()     {         editText.requestFocus();         imm.showSoftInput(editText, 0);     } }, 100); 

Second method

in onCreate to launch it on activity create

new Handler().postDelayed(new Runnable() {     @Override     public void run()      {     //  InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);     //    inputMethodManager.toggleSoftInputFromWindow(EnterYourViewHere.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);          if (inputMethodManager != null)         {             inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);         }      } }, 200); 

Third Method ADD given code to activity tag in Manifest. It will show keyboard on launch, and set the first focus to your desire view.

android:windowSoftInputMode="stateVisible" 
like image 136
Zar E Ahmer Avatar answered Oct 07 '22 16:10

Zar E Ahmer