Setting the alpha value for a ConstraintLayout group

I created this extension function to help me achieve this functionality

fun Group.setAlphaForAll(alpha: Float) = referencedIds.forEach {
    rootView.findViewById<View>(it).alpha = alpha
}

And then in code I use it like this

my_group.setAlphaForAll(0.4f)

Group is only used for controlling the visibility of Referenced Ids in app:constraint_referenced_ids. According to the documentation.

The visibility of the group will be applied to the referenced widgets. It's a convenient way to easily hide/show a set of widgets without having to maintain this set programmatically.


We can not use Group to change the alpha value, but we can change alpha with DataBinding.

<layout>

    <data>
        <variable name="alpha" type="float" />
    </data>

    <!-- Unrelated attributes omitted for brevity. -->

    <android.support.constraint.ConstraintLayout>
        <Button android:alpha="@{alpha}"/>
        <TextView android:alpha="@{alpha}"/>
    </android.support.constraint.ConstraintLayout>
</layout>
binding.alpha = 1.0f  // 0.0 ~ 1.0

Tags:

Android