Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the Soft Keyboard from a Service?

Short question: Is it possible (and how) to display the soft-keyboard from a Service?

Long question: I wrote a service which creates a "top bar", displayed on top of all activities, containing an EditText. I want to display the soft-keyboard when that EditText is clicked, but this is not happening.

Of course I've tried this from the Service's onFocusChange() and onClick():

InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);

The workaround I came up with is to request the current activity to show the keyboard, by extending the Activity class and adding an AIDL interface. The drawback is that each key event has to be sent back to the Service (via another AIDL inteface) and manually converted into Unicode.

Moreover, if the current activity contains an EditText, the soft-keyboard only works for the activity and doesn't show up anymore when the service's EditText is selected.

What prevents the service's soft-keyboard to be displayed if the current activity has an EditText? Could it be an Android limitation?

like image 679
rippeltippel Avatar asked Aug 03 '11 10:08

rippeltippel


People also ask

How do I show keyboard on Android?

To enable your latest Android keyboard, scroll to the bottom and hit the System entry. Then, click Languages & input. Pick Virtual keyboard on the following page. You will find a list of all the existing keyboards on your smartphone here.

How do I show soft keyboard when EditText is focused?

android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears.

How do I know if my keyboard is displayed?

Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.

How do I turn off soft keyboard on Android?

Hiding the Soft Keyboard Programmatically 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.


1 Answers

I have faced a similar issue. I have solved it now. Maybe it's too late but may it helps someone. Possible Cause of Error: Setting FLAG_NOT_FOCUSABLE during LayoutParams

Window flag: this window won't ever get key input focus, so the user can not send key or other button events to it. Those will instead go to whatever focusable window is behind it.

Simply Replace FLAG_NOT_FOCUSABLE with 0

Use this below code:

InputMethodManager imm = null;

mView.findViewById(R.id.editText).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    if(imm==null){
                        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    }

                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0 );
                }
            }
        });

2nd When creating LayoutParams

private WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    0,
                    PixelFormat.TRANSLUCENT);
like image 184
Muhammad Hashim Shafiq Avatar answered Oct 19 '22 02:10

Muhammad Hashim Shafiq