Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to call onClickListener in WindowManager

I am implementing windowManager in my Service Class

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mView, params);

I added a view in my windows manager but the click listener of that view is not working .

    mView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(),"onClick",     Toast.LENGTH_LONG).show();
        }
    });

Here are the layout params .

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(130,130);
    params.type=WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
    params.flags=WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
    params.format=PixelFormat.TRANSPARENT;

I want that click listener to work properly but its not working so please help me in this regard.

like image 537
Jan Avatar asked Feb 18 '23 10:02

Jan


1 Answers

Hi After a long research i found this code. this should work. have a try . Add this line in on-create of your service class. The below contents are the parameters we should pass FLAG_WATCH_OUTSIDE_TOUCH and all. Hope this helps you.

WindowManager.LayoutParams params = new WindowManager.LayoutParams(100, 100, 2007, 8, -3);
        Button bb=new Button(this);
        bb.setText("Button");
        bb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("Clicked----><<<<<<<");
            }
        });

        bb.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("Touched =----- > ");
                return false;
            }
        });

        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);        
        wm.addView(bb, params);
like image 170
itsrajesh4uguys Avatar answered Feb 27 '23 03:02

itsrajesh4uguys