I´m trying to add a floating view with WindowManager, it´s a simple button but it looks like the Theme is not being applied and the app icon is not shown, found this question and this question but nothing of what´s recommended worked. On the preview, the theme is applied and I can see the icon, but not at runtime.
The app default theme:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PartialScreenshot" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
I´m calling the view from a service:
class OptionsWindowService(): Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
setUpFloatingWidget()
return START_NOT_STICKY
}
private fun setUpFloatingWidget() {
val manager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val inflater= LayoutInflater.from(baseContext)
.cloneInContext(ContextThemeWrapper(baseContext,
R.style.Theme_PartialScreenshot))
val optionsFloatingView = inflater.inflate(R.layout.options_view, null) as ConstraintLayout
val layoutFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
val flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
val params = WindowManager.LayoutParams(
WRAP_CONTENT,
WRAP_CONTENT,
layoutFlag,
flags,
PixelFormat.TRANSLUCENT
)
//Specify the view position
params.gravity = Gravity.TOP or Gravity.START //Initially view will be added to top-left corner
params.x = 400
params.y = 200
manager.addView(optionsFloatingView,params)
}
And this is the XML file:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/imageView"
android:text="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:icon="@drawable/ic_baseline_delete_forever_24" />
</androidx.constraintlayout.widget.ConstraintLayout>