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);


    }
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

1 Answers1

2

I'm quite lucky running into this answer:

https://stackoverflow.com/a/56045405/11110509

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.

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83