Inflated View in Service is displaying the View, but not an ImageView inside the layout
Asked Answered
A

1

3

I have successfully inflated my layout in a service, however it's not displaying the ImageView inside the Inflated Layout and I have no idea why. I set an onClickListener on the imageview and it works perfectly fine (Logs and stops the service when I click on the green square). The only problem is that it's not displaying the X mark and I can't figure out why.

enter image description here

enter image description here

public class OverlayService extends Service {

    private ViewGroup view;

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

    @Override
    public void onCreate() {
        super.onCreate();

        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.TOP | Gravity.END;
        }

        view = (ViewGroup) layoutInflater.inflate(R.layout.test_view, null);

        ImageView btnClear = view.findViewById(R.id.btn_clear);
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                // THIS IS SUCCESSFULLY CALLED WHEN CLICKING ON THE GREEN BUTTON.
                Log.d(TAG, "onClick: Clicked");
                stopSelf();
            }
        });

        wm.addView(view, params);


    }
Agave answered 25/6, 2021 at 8:39 Comment(0)
A
2

I'm quite lucky running into this answer:

https://mcmap.net/q/234358/-android-vector-drawable-app-srccompat-not-showing-images

I was about to give up until it told me to try

<androidx.appcompat.widget.AppCompatImageView in the XML instead of ImageView. I have no idea why this works, but I can now see the drawable in the Inflated Layout.

Agave answered 25/6, 2021 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.