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