Created
December 8, 2020 15:06
-
-
Save chinhnguyen/3255516447d906cf39c83892925bac26 to your computer and use it in GitHub Desktop.
Disable all buttons inside ViewGroup with data binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="@dimen/guideline" | |
app:disableButtons="@{vm.busy}"> | |
<com.google.android.material.button.MaterialButton | |
android:id="@+id/saveButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="@dimen/guideline" | |
android:enabled="@{vm.canSave}" | |
android:onClick="@{() -> vm.saveCurrentOrder()}" | |
android:text="@string/save" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toRightOf="@id/closeButton" /> | |
<com.google.android.material.button.MaterialButton | |
android:id="@+id/checkButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="@dimen/guideline" | |
android:enabled="@{vm.canCheck}" | |
android:onClick="@{() -> vm.checkCurrentOrder()}" | |
android:text="@string/check" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toRightOf="@id/saveButton" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.ViewGroup | |
import android.widget.Button | |
import androidx.core.view.children | |
import androidx.databinding.BindingAdapter | |
@BindingAdapter("disableButtons") | |
fun ViewGroup.setDisableButtons(disableButtons: Boolean) { | |
children.forEach { | |
(it as? Button)?.isEnabled = !disableButtons | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment