swappy
swappy

Reputation: 21

How to remove multiple views from window manager in Android?

I am working on Caller ID application where I am showing information of the dialed/receiving number in a window manager view. User can manually close the view. All went good until I tested it on call waiting service. On call waiting view stays on screen even after clicking close button and showing error "View Not Attached To Window Manager". I am doing all these stuff from service.

My question is if there any way to remove multiple views on a single click. or Can I keep a track of views attached to window manager.

Here's my code

linearLayout = new LinearLayout(this);
    linearLayout.addView(textHeaderName);
    linearLayout.addView(textheader);
    linearLayout.addView(chatHead);
    linearLayout.addView(textfooter);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    btnClose.setImageResource(R.drawable.button_close);
    btnClose.setLeft(0);
    chatHead.setBackgroundResource(R.drawable.img);

android.view.WindowManager.LayoutParams layoutparams = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    android.view.WindowManager.LayoutParams layoutparams1 = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    android.view.WindowManager.LayoutParams layoutparams2 = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    final android.view.WindowManager.LayoutParams paramsChatHead = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    android.view.WindowManager.LayoutParams layoutparams3 = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);

    new android.widget.LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);

    layoutparams.gravity = 53;
    layoutparams.y = 0;
    layoutparams1.gravity = 49;
    layoutparams1.x = 0;
    layoutparams1.y = 0;
    layoutparams2.gravity = 49;
    layoutparams2.x = 0;
    layoutparams2.y = 25;
    paramsChatHead.gravity = 49;
    paramsChatHead.x = 0;
    paramsChatHead.y = 50;
    layoutparams3.gravity = 49;
    layoutparams3.x = 0;
    layoutparams3.y = imgheight + 50;

    try 
    {
        windowManager.addView(linearLayout, layoutparams1);
        windowManager.addView(btnClose, layoutparams);

    } catch (Exception e) 
    {}

Close event :

btnClose.setOnClickListener(new android.view.View.OnClickListener() 
    {
        public void onClick(View view)
        {
            try
            {
                if (linearLayout != null)
                {
                    windowManager.removeView(linearLayout);
                }
                if (btnClose != null)
                {
                    windowManager.removeView(btnClose);
                }
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
        }     
    });

Please help

Upvotes: 1

Views: 3534

Answers (1)

Mehdi Azadi
Mehdi Azadi

Reputation: 77

use this condition :

 if (linearLayout.getWindowToken() != null) {
                                    windowManager.removeView(linearLayout);
                                }

Upvotes: 2

Related Questions