#android #binding #themes
Вопрос:
У меня есть две кнопки, которые выглядят так:
<co.proxy.uicomponents.buttons.LargeLoadingButtonWidget
android:id="@ id/accept_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/grid_1"
android:paddingHorizontal="@dimen/dimen_page_margin"
android:text="@string/accept_action"
app:base_button_style="black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/half_buttons_guideline"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<co.proxy.uicomponents.buttons.LargeLoadingButtonWidget
android:id="@ id/not_now_button"
android:layout_width="match_parent"
android:paddingHorizontal="@dimen/dimen_page_margin"
android:layout_height="wrap_content"
android:text="@string/share_health_information_not_now_action"
app:base_button_style="transparent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
Корневой макет является макетом ограничений, а параметр accept_button находится внутри вложенного макета ограничений. Это пользовательские кнопки, которые при нажатии имеют ресурсы анимации. Они оба работают безупречно, когда их прослушивают. Они прекрасно переходят к анимации.
Кнопки имеют разный внешний вид, определяемый base_button_style
.
Мои ресурсы для этой кнопки выглядят так
<attr name="base_button_style" format="enum">
<enum name="black" value="0" />
<enum name="grey" value="1" />
<enum name="transparent" value="2" />
<enum name="red" value="3" />
<enum name="white" value="4" />
<enum name="dark_grey" value="5" />
<enum name="black_outline" value="6" />
</attr>
<declare-styleable name="BaseLoadingButtonWidget">
<attr name="android:enabled" />
<attr name="android:text" />
<attr name="base_button_style" />
</declare-styleable>
И в моем виджете кнопок у меня есть код селектора, который знает, какую кнопку выбрать на основе этого значения перечисления.
sealed class ButtonState(val buttonText: String) : Widget.State {
class Black(buttonText: String) : ButtonState(buttonText)
class Grey(buttonText: String) : ButtonState(buttonText)
class Transparent(buttonText: String) : ButtonState(buttonText)
class Red(buttonText: String) : ButtonState(buttonText)
class White(buttonText: String) : ButtonState(buttonText)
class MediumGrey(buttonText: String) : ButtonState(buttonText)
class BlackOutline(buttonText: String) : ButtonState(buttonText)
companion object {
fun getStateFromValue(value: Int, actionText: String): ButtonState {
return when (value) {
0 -> Black(actionText)
1 -> Grey(actionText)
2 -> Transparent(actionText)
3 -> Red(actionText)
4 -> White(actionText)
5 -> MediumGrey(actionText)
6 -> BlackOutline(actionText)
else -> Black(actionText)
}
}
}
}
Each button has its own animation. For this example, black button has a white animation, and for the transparent button, the animation is black.
When I tap the buttons it uses the correct animation color.
I do not support dark theme in my application, in my App.kt I use
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
which works.
If I kill the app and toggle on dark theme, and start the app back up, everything works as expected.
The same is true if I kill the app and toggle on light theme, and start the app back up.
No issues there.
The issue is if I have the app open, switch to settings, switch to dark (or light) theme, and then switch back to the still-open app, the accept_button is now using the transparent buttons resources for animations. So the animation turns black instead of white, causing a black button with a black animation to occur, so the button looks like it is doing nothing while loading.
I the layout I have listed above is an Activity, not fragment. I have attempted to update the AppCompat library without success. Whenever I check the ID’s amp; matching in my custom LargeLoadingButtonWidget, everything matches up and it is doing what it is meant to do. It selects the appropriate resources based on the IDs provided. But in my case, it is seeming to mix up the base_button_style
for the button when I switch the system theme while the app is open.
I will reiterate that my app has no dark theming at all.
The init of the activity looks like
@FlowPreview
@ExperimentalCoroutinesApi
@AndroidEntryPoint
class HealthShareRequestActivity: AppCompatActivity() {
private val binding by viewBinding(ActivityHealthShareRequestBinding::inflate)
private val viewModel by viewModels<HealthShareRequestViewModel>()
private val orgId: String by extra(ORG_ID)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
initUI()
registerListeners()
registerObservers()
}
So we are using viewbinding here. And the animations are both lottie animations set via setAnimation.