Jonathan Stellwag
Jonathan Stellwag

Reputation: 4277

Dialog custom state on windowBackground?

Is it possible to let my android:windowBackground drawable receive custom attributes I added?

For backgrounds I have custom Views on that I can override the onCreateDrawableState. For Dialogs that's not possible. Is there a way to work around?

What do I do?

I have two custom attributes which are used as a state in my drawables (like android:state_pressed="true")

<attr name="state_light" format="boolean"/>
<attr name="state_dark" format="boolean"/>

My MainActivity has variable val foo: Boolean. When the value is changed I call refreshDrawableState on the following ViewGroups and their children to trigger an onCreateDrawableState

In all of my Views I now override the onCreateDrawableState so I can add the custom state

override fun onCreateDrawableState(extraSpace: Int): IntArray {
    val state = super.onCreateDrawableState(extraSpace + 1)
    return when (mainActivity.foo) {
        true -> state.mergeDrawableState(attr.state_light)
        false -> state.mergeDrawableState(attr.state_dark)
    }
}

Now I create and set a drawable onto a specific View (somewhere in my tree) that itself has a Selector with my items that listen for my custom states.

<selector>
    <item android:color="#ffffff" app:state_light="true"/>
    <item android:color="#000000" app:state_dark="true"/>
    <item android:color="#323232"/>
</selector>

The top code works fine and I can switch colors in my app via the foo value. This just does not work for android:windowBackground. I need to use the windowBackground as I have to use the full API (like setPositiveButton).

My guess is that Android internal has an onCreateDrawableState that is not in my reach. I am just out of ideas and seek for some inspiration/help. Thanks in advance.

Upvotes: 0

Views: 33

Answers (1)

kpwn Apps
kpwn Apps

Reputation: 602

If you have custom views just use setView() of AlertDialog

Upvotes: 0

Related Questions