kavuru
kavuru

Reputation: 379

how to create popup in oncreate method of service class

I am trying to create popup window on home screen when an floating image is clicked (using windowmanager).

so while using layoutinflater , I am not able to set a viewgroup in the second argument as findviewbyId is not recognised. so I kept null. like below.

LayoutInflater inflater = (LayoutInflater) Floater.this
                             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     View layout = inflater.inflate(R.layout.popup_for_floating_img,
                             null);

                     pwindo = new PopupWindow(layout, 300, 370, true);
                     pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

But when i click the floating image on home screen , it is giving below error message.

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

so please suggest me the way forward to get the popup on click.

I am trying for popup like if we enable floating widget in CLEAN MASTER app and click on floating broom image will give popup.

Upvotes: 1

Views: 613

Answers (1)

Sándor Megyeri
Sándor Megyeri

Reputation: 43

Try this in your showPopup(...) function:

layout.post(new Runnable()
{
    public void run()
    {
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, OFFSET_X, OFFSET_Y);
    }
});

where the layout is your popup. This is because you trying to show the popup before you even created the activity.

Upvotes: 1

Related Questions