shaoping wang
shaoping wang

Reputation: 53

Keyboard auto on in Android

I want to implement a tablet in Android app that keyboard auto on the screen. Now my situation is I add a edit-text in a xml file, and when clicking the edit-text, keyboard appears, I want to show keyboard auto on. When users click on the check mark, the keyboard disappears, but I want that keyboard will not be dismissed and clicking on check mark means checking the input. Any samples or help is appreciated!

Upvotes: 0

Views: 258

Answers (5)

lakshmi
lakshmi

Reputation: 21

You can use the following code,

editext.requestfocus();

this will automatically brings the keyboard.

Do not use, android:windowSoftInputMode="stateHidden" inside your android manifest file...

just remove "android:windowSoftInputMode" from your Activity.

Upvotes: 0

rana_sadam
rana_sadam

Reputation: 1226

Below are the methods to show and hide keyboard. Have a look.

public static void ShowKeyboard(Activity activity, View view) {
    InputMethodManager inputMethodManager =
            (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(
            view.getApplicationWindowToken(),
            InputMethodManager.SHOW_FORCED, 0);
}

public static void HideKeyBoard(Activity activity) {
    try {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isAcceptingText())
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

If you want to open keyboard on activity start, you can add following code in <activity> tag in manifest:

android:windowSoftInputMode="stateVisible"

this will show the keyboard whenever you start activity.

Upvotes: 0

Chandana Kumara
Chandana Kumara

Reputation: 2645

Check with this: Add this on your checkbox click

To Show Soft Keyboard:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);     
imm.showSoftInput(EDITABLE_VIEW,InputMethodManager.SHOW_IMPLICIT);

OR

 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

To Hide Soft Keyboard:

 InputMethodManager imm = InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(FOCUSABLE_VIEW.getWindowToken(), 0);

Here, “FOCUSABLE_VIEW” can be any view which is visible on screen like

Refer:http://chintanrathod.com/show-hide-soft-keyboard-programmatically-in-android/

Upvotes: 0

Rajakumar
Rajakumar

Reputation: 937

This will work and also always try to use etText.requestfocus(); it will automatically open a keyboard and public static void ShowKeyboard(Activity activity, View v) {

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

}

Upvotes: 0

Viral Gauswami
Viral Gauswami

Reputation: 174

Use below method with activity

//To show the keyboard
public void showKeyboard(Activity activity) {
    if(activity.getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
    }
}

//To hide the keyboard
public void hideKeyboard(Activity activity) {
        if(activity.getCurrentFocus()!=null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }

OR

Use below method with EditText

//To show the keyboard
public void showSoftKeyboard(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }

//To hide the keyboard
    `public void hideSoftKeyboard(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

Use above method as per your requirement

Upvotes: 2

Related Questions