Search code examples
androidandroid-softkeyboard

Open soft keyboard programmatically


I have an activity with no child widgets for it and the corresponding xml file is,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
>
</LinearLayout>

and I want to open soft keyboard programmatically while the activity gets start.and what I've tried upto now is,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null) {
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }

Give me some guidance.


Solution

  • I have used the following lines to display the soft keyboard manually inside the onclick event, and the keyboard is visible.

    InputMethodManager inputMethodManager =
        (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(
        linearLayout.getApplicationWindowToken(),
        InputMethodManager.SHOW_FORCED, 0);
    

    But I'm still not able to open this while the activity gets opened, so are there any solution for this?