Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:windowSoftInputMode="stateVisible" doesn't work

I would like to open the soft keyboard on when the Activity starts and I found that

android:windowSoftInputMode="stateVisible" 

doesn't work.

To make sure, I created a new project (the default "Hello world") and did the following:

  1. added the windowSoftInputMode to the manifest.
  2. After that didn't work, I added an EditView field to the layout
  3. After that didn't work, I added

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) to the onCreate procedure.

I compiled it with Android2.3.3 and tried to run it on my Galaxy S2 device and the Android4 emulator and still - no keyboard.

My manifest file:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="9" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".HelloworldActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateVisible">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

My main.xml layout:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />

</EditText>

My code:

public class HelloworldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    }
}
like image 316
Asaf Pinhassi Avatar asked Mar 18 '12 16:03

Asaf Pinhassi


People also ask

How to auto hide 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 edit field. This will force the keyboard to be hidden in all situations.

How to make keyboard disappear on Android studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code when you click on the button it will hide keyboard.

What is soft input mode in Android?

The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.


3 Answers

You can force first focus on the EditText view by running:

final EditText edit = (EditText) findViewById(R.id.editText1);
edit.post(new Runnable() {
    @Override
    public void run() {
        edit.requestFocus();
    }
});

This should open the keyboard on activity start.

like image 141
marmor Avatar answered Nov 19 '22 03:11

marmor


Its simple thing. I have done it and it works as your requirement.

  1. Don't do anything with the manifest, leave it as it is while you creating the new new project.

  2. Now define inputmanager.

    public static InputMethodManager imm;
    imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    
  3. Now, here salary EditText is my EditText and i am showing the keyboard on the start of that activity.

    salaryEditText.setHint("select Salary/Wage");
    
    salaryEditText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(2)}); // Ignore this line
    
    if(!(imm==null))
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,1);
    

That will help you to show the keyboard while activity start.

To Close keyboard on the activity finish see below code:

white this code while you finish the activity.

imm.hideSoftInputFromWindow(salaryEditText.getWindowToken(), 0);

Hope it will solve your issue. If not then let me know.

Enjoy. :)

like image 43
Shreyash Mahajan Avatar answered Nov 19 '22 04:11

Shreyash Mahajan


Do you use the default android keyboard? If you do, try it on a different device, I know it has some issues

like image 22
Muky Avatar answered Nov 19 '22 03:11

Muky