Home » Android » move – Android WindowManager TYPE_SYSTEM_ALERT Layout on touch

move – Android WindowManager TYPE_SYSTEM_ALERT Layout on touch

Posted by: admin June 15, 2020 Leave a comment

Questions:

my app displays a button to user at certain times as TYPE_SYSTEM_ALERT and I am trying to let user to move it around. Touch and move action does register with below code but button doesn’t move. What am I missing?

    //create button
    mbtRec = new Button(ACR.getContext()); 
    mbtRec.setText(R.string.start_recording);
    mbtRec.setTypeface(null, Typeface.BOLD);
    mbtRec.setBackgroundColor(Color.RED);       //mbtRec.setBackgroundResource(R.drawable.ic_launcher); 
    mbtRec.setOnClickListener(recButtonOnClickListener);
    mbtRec.setOnTouchListener(recButtonOnTouchListener);
    //create layout params
    recButtonLayoutParams = new WindowManager.LayoutParams();
    recButtonLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED; //8
    recButtonLayoutParams.format = PixelFormat.RGBA_8888; //PixelFormat.TRANSLUCENT
    recButtonLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    recButtonLayoutParams.gravity = Gravity.TOP | Gravity.CENTER; //51
    recButtonLayoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
    recButtonLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    paramWindowManager.addView(this.mbtRec, recButtonLayoutParams);


OnTouchListener recButtonOnTouchListener = new View.OnTouchListener() {
        @TargetApi(Build.VERSION_CODES.FROYO)
        @Override
        public boolean onTouch(View v, MotionEvent event) {
             switch(event.getActionMasked())
                {
                    case MotionEvent.ACTION_DOWN:
                        Tools.Log("Drag", "Started Dragging");
                        break;
                    case MotionEvent.ACTION_UP:
                        Tools.Log("Drag", "Stopped Dragging");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Tools.Log("Drag", "Dragging");


                        recButtonLayoutParams.x =(int) event.getRawX();
                        recButtonLayoutParams.y= (int) event.getRawY();


                        mbtRec.setLayoutParams(recButtonLayoutParams);
                        mbtRec.invalidate();

                        break;
                    default:
                        break;
                }
                return false;
        }               
    };
How to&Answers:

I have managed to get what i want with @yoah’s tip and more research for 10 days. I must add @pingpongboss’s (https://github.com/pingpongboss/StandOut) replies to my email helped a lot.

Here is what I came up with and implemented to my call recording app ACR

private WindowManager recButtonWindowManager;
private Button recButton = null;
WindowManager.LayoutParams prms;
boolean touchconsumedbyMove = false;
int recButtonLastX;
int recButtonLastY;
int recButtonFirstX;
int recButtonFirstY;

OnTouchListener recButtonOnTouchListener = new View.OnTouchListener() {
    @TargetApi(Build.VERSION_CODES.FROYO)
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        WindowManager.LayoutParams prm = getRecbuttonLayout();
        int totalDeltaX = recButtonLastX - recButtonFirstX;
        int totalDeltaY = recButtonLastY - recButtonFirstY;

         switch(event.getActionMasked())
            {
                case MotionEvent.ACTION_DOWN:
                    recButtonLastX = (int) event.getRawX();
                    recButtonLastY = (int) event.getRawY();
                    recButtonFirstX = recButtonLastX;
                    recButtonFirstY = recButtonLastY;                   
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    int deltaX = (int) event.getRawX() - recButtonLastX;
                    int deltaY = (int) event.getRawY() - recButtonLastY;
                    recButtonLastX = (int) event.getRawX();
                    recButtonLastY = (int) event.getRawY();
                    if (Math.abs(totalDeltaX) >= 5  || Math.abs(totalDeltaY) >= 5) {
                        if (event.getPointerCount() == 1) {
                            prm.x += deltaX;
                            prm.y += deltaY;
                            touchconsumedbyMove = true;                
                            recButtonWindowManager.updateViewLayout(getManualRecButton(), prm); 
                        }
                        else{
                            touchconsumedbyMove = false;
                        }
                    }else{
                        touchconsumedbyMove = false;
                    }
                    break;
                default:
                    break;
            }
            return touchconsumedbyMove;
    }               
};
    private WindowManager.LayoutParams getRecbuttonLayout() {
    if (prms != null) {
        return prms;
    }
    prms = new WindowManager.LayoutParams();
    prms.format = PixelFormat.TRANSLUCENT;
    prms.flags = LayoutParams.FORMAT_CHANGED; // 8
    prms.type = LayoutParams.TYPE_SYSTEM_ALERT;
    prms.gravity = Gravity.TOP | Gravity.CENTER;
    prms.width = LayoutParams.WRAP_CONTENT;
    prms.height = LayoutParams.WRAP_CONTENT;
    // Tools.Log("getRecbuttonLayout", "return getRecbuttonLayout()");
    return prms;
}

private Button getManualRecButton() {
    if (recButton != null) {
        return recButton;
    }
    recButton = new Button(ACR.getContext());
    recButton.setText(R.string.start_recording);
    recButton.setTypeface(null, Typeface.BOLD);
    recButton.setGravity(Gravity.CENTER_VERTICAL);
    recButton.setBackgroundColor(Color.parseColor("#65FF0000"));
    recButton.setPadding(100, 30, 100, 30);
    recButton.setOnClickListener(recButtonOnClickListener);
    recButton.setOnTouchListener(recButtonOnTouchListener);
    return recButton;
}

Answer:

I am doing something similar, but using marginLeft and marginTop.
After changing the layout, you need to call

 windowManager.updateViewLayout(recButton, recButtonLayoutParams);

Answer:

I think you need to use mbtRec.postInvalidate() instead, AND remove gravity

Answer:

Since you change the layout params for the WindowManager, instead of calling:

mbtRec.setLayoutParams(recButtonLayoutParams);
mbtRec.invalidate();

You should call:

paramWindowManager.updateLayout(this.mbtRec, recButtonLayoutParams);

at the end of the ACTION_MOVE case.

Answer:

you set your button to fit the screen with

recButtonLayoutParams.width = WindowManager.LayoutParams.FILL_PARENT;

So you can’t move it in the x direction.

How ever you supposed to be able to move it in the Y direction unless its parent is set to wrap content then it will also block you.

That what I saw any way, hope this helps.