sonu thomas
sonu thomas

Reputation: 2161

Android:Keyboard not shown using code in emulator

This code is to show and hide Android Keyboard on a button click.

public void keyClickHandler(View v) {
    EditText editText = (EditText) findViewById(R.id.KeyBoard);
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    if (keyboard) {
        mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        keyboard = false;
    } else {
        mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        keyboard = true;
    }

    Log.d("SET", "Focus");
}

But it is not working in emulator

I got found that it is working in the phone,but not in the emulator

Upvotes: 0

Views: 239

Answers (1)

user990230
user990230

Reputation: 307

I don't know how the rest of your code is, but you can try something like this:

public void onClick(View v)
{
    EditText editText = (EditText) findViewById(R.id.KeyBoard);
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

     switch(v.getId())
     {
         case R.id.yourButtonId:
            if(keyboard)
            {
                mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                keyboard = false;
            } 
            else 
            {
                mgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                keyboard = true;
            }

            Log.d("SET", "Focus");
            break;
     }
}

For this to work you have to implement your class with onClickListener and in onCreate set your button to something like this:

Button yourButton = (Button) findViewById(R.id.yourButtonId);
yourButton.setOnClickListener(this);

Upvotes: 1

Related Questions