Overlay button not working in Android 4.3

917 Views Asked by At

I have an overlay view with a button on WindowManager.LayoutParams.TYPE_TOAST level layer. The button works well in Android 4.2, but when I run the same code in Android 4.3, the button gets unclickable - nothing happens when I click it.

Here's the relevant code.

ButtonOverlayService.java

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public class ButtonOverlayService extends Service {
    WindowManager wm;
    View home_button_view;

    @Override
    public void onCreate() {
        super.onCreate();

        WindowManager.LayoutParams home_params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                -10000,
                10000,
                WindowManager.LayoutParams.TYPE_TOAST,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        home_button_view = layoutInflater.inflate(R.layout.home_button_overlay, null);
        home_button_view.findViewById(R.id.home_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("myApp", "clicked")
            }
        }

        wm.addView(home_button_view, home_params);
    }
}

home_button_overlay.xml

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/home_button"
    android:layout_width="100dp"
    android:layout_height="100dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#85ff3f"
    android:padding="0dp"
    android:paddingEnd="0dp"
    android:paddingStart="0dp"
    android:gravity="fill"></Button>

Anybody have any idea?

1

There are 1 best solutions below

0
On

Use WindowManager.LayoutParams.TYPE_SYSTEM_ALERT to make the view clickable in 4.3.

TYPE_TOAST no longer allows clicks.