I have a view that uses DataBinding
to display data from a ViewModel
. The view has a button that is hidden when the view is in landscape mode. At the moment this works by having two layoutfiles, but I would like to only have one as it's a nightmare to refactor. But how to go about this using DataBinding
and BindingAdapters
?
How do you enable the following bindingexpression for a given view?
android:visibility="@{isLandscapeMode ? View.INVISIBLE : View.VISIBLE}"
EDIT:
My extension property definition (ViewUtils.kt
):
val View.isLandscapeMode: Boolean
get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
And my layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<import type="com.example.base.views.ViewUtilsKt" />
<variable
name="isLandscapeMode "
type="java.lang.Boolean"/>
<variable
name="viewmodel"
type="com.example.ui.task.TaskViewModel" />
</data>
...
<ImageButton
...
android:visibility="@{isLandscapeMode ? View.INVISIBLE : View.VISIBLE}"
...
/>
This causes a compile error: Cause: not a valid name: isLandscapeMode