Android 4.x - System Overlay - Unable to Capture Touch Events

I am trying to create a simple Android 4.4 overlay app.

I found an example to draw a button above the screen, everything works fine, but the touch event listener does not fire.

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class HUD extends Service {
    Button mButton;

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

    @Override
    public void onCreate() {
        super.onCreate();
        //mView = new HUDView(this);
        mButton = new Button(this);
        mButton.setText("My Overlay Button");
        mButton.setClickable(true);
        mButton.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View arg0, MotionEvent arg1) {
            mButton.setText("CLICKED!!!");
            return true;
          }
        }); 

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.CENTER;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mButton, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mButton != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
            mButton = null;
        }
    }
}

      

What am I doing wrong?

+2


source to share


2 answers


It sounds like you rather need an OnClickListener instead of an OnTouchListener for your button.



0


source


Use WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

to make browsing available in 4.3.



TYPE_SYSTEM_OVERLAY

no longer allows clicks.

0


source







All Articles