Muhammad Maqsoodur Rehman
Muhammad Maqsoodur Rehman

Reputation: 34267

How to show virtual keypad in an android activity

Why am i not able to show the virtual keyboard in my activity. Here is my code:

package som.android.keypad;

import android.app.Activity;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class ShowKeypad extends Activity {
    InputMethodManager imm;

    @Override
    public void onCreate(Bundle icicle) {

    super.onCreate(icicle); 
    EditText editText = (EditText)findViewById(R.id.EditText);

    ((InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE)).showSoftInput(editText, 0); 



    }

}

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

    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest> 

Upvotes: 2

Views: 15069

Answers (2)

Harshid Vasoya
Harshid Vasoya

Reputation: 5721

First setContentView

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

Sometime in android 4.2 keyboard not automatically open.

You have to used this way to open and close keyboard programmatically.

//For open keyboard
public void OpenKeyBoard(Context mContext){
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    }
    //For close keyboard
    public void CloseKeyBoard(Context mContext){
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
    }

Upvotes: 7

Alex Volovoy
Alex Volovoy

Reputation: 68444

 setContentView(R.layout.main); 

seems to be missing. Manifest setting should be enough to show keyboard

Upvotes: 1

Related Questions