Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding : conditional formatting to set layout_weight

How to set layout_weight in xml using android data bindning? in dimens folder we provide values as in dp / sp.

like image 651
Mohanish Nerurkar Avatar asked Oct 30 '22 11:10

Mohanish Nerurkar


1 Answers

This is a binding adapter that works for me, your view does need to be inside a LinearLayout, which I think it will be if you are wanting to set the weight:

@BindingAdapter("android:layout_weight")
fun setLayoutWeight(view: View, weight: Float) {
    val layoutParams = view.layoutParams as? LinearLayout.LayoutParams
    layoutParams?.let {
        it.weight = weight
        view.layoutParams = it
    }
}
like image 124
tagy22 Avatar answered Nov 11 '22 10:11

tagy22