14

I'm writing an application that uses the following code to draw an edittext on the screen over running applications:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                PixelFormat.TRANSLUCENT);

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

windowManager.addView(mEditText, params);

The xml for the edittext is:

<EditText
            android:id="@+id/mEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:inputType="textAutoComplete|text"
            android:focusable="true"
            android:focusableInTouchMode="true" />

However focusing on this does not bring up the keyboard. I've also tried programmatically bringing it up with an onFocusListener:

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    Log.d("", "Has focus");
                    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
                } else {
                    Log.d("", "Lost focus");
                }
            }
        });

But although that is called, as seen from the logcat, nothing happens. The only method I've found so far to display the keyboard is using:

getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);

But that seems to type onto the screen and not into the edittext. I've also tried clear focusable when the edittext is displayed but to no avail.

I'm guessing the issue is because I'm using a "floating window" but there must be a way to make this work as apps such as floating calculators exist on the playstore which take input.. Anyone have any ideas? I'm stumped :(

crazyfool
  • 1,433
  • 4
  • 22
  • 38

4 Answers4

21

My bad.. I realized if I remove the WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE it works fine.. silly mistake

crazyfool
  • 1,433
  • 4
  • 22
  • 38
  • 1
    But using this flag prevents outside home buttom clickable – Ankesh kumar Jaisansaria Aug 19 '16 at 10:14
  • 1
    but what flag did you use – Richard Aguirre Dec 18 '18 at 22:30
  • @Ankesh kumar Jaisansaria, you are right, setting 0 instead of WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE , will solve the problem, but it will prevent system clicks, what is the solution for that? – eawedat Sep 18 '19 at 03:42
  • You to have use two flag settings. one when you want to write in WindowManager layout and other for when you are not using it. update the flag according to your settings at run time. for this you need to add window again to window manager. or may be it may work with reference to that params that you already provided to window manager. not checked my self. – Ali Imran Dec 13 '21 at 23:16
9

Use WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    PixelFormat.TRANSLUCENT);

For more detail and example follow the link: https://github.com/atifmahmood29/overlays-floating-window-like-facebook-messenger

Atif Mahmood
  • 8,882
  • 2
  • 41
  • 44
0

i resolved this issue by using FLAG_NOT_FOCUSABLE

Syed Hussain Mehdi
  • 1,168
  • 1
  • 11
  • 15
-2

Programatically:

editText.requestFocus();

Or through XML:

<EditText...>
    <requestFocus />
</EditText>