'Setting the alpha value for a ConstraintLayout group
I want to ask is it possible to change the alpha for a constraint group?
<android.support.constraint.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="statusTv, statusDropDownIv"
android:id="@+id/buttonGroup"
android:visibility="visible"
android:alpha="0" />
Right now the visibility
tag has effect if I set it to visible/gone but the alpha
tag seems to not work.
Solution 1:[1]
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.
Solution 2:[2]
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)
Solution 3:[3]
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
Solution 4:[4]
Not working to me too, try to use a ViewGroup to set alpha propertie and put your views [statusTv, statusDropDownIv] as child. To me works with ScrollView, LinearLayout and others.
I know it's sucks because Constraint Layout is expected as a flat solution layout.
Solution 5:[5]
Currently androidx.constraintlayout.widget.Group class does not provide ability to change alpha of the grouped views. Mainly it is to be used to set Visibility (show/hide) of the grouped views with a single call.
Another solution is to inherit androidx.constraintlayout.widget.Group class and override setAlpha function.
The following code is almost exactly the same as calling Group.visibility with a difference that we are setting alpha of the views instead.
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
class MyGroup(context: Context, attrs: AttributeSet? = null) : androidx.constraintlayout.widget.Group(context, attrs)
{
override fun setAlpha(alpha: Float)
{
super.setAlpha(alpha)
val parent = this.parent ?: return
if(parent is ConstraintLayout)
{
for(i in 0..this.mCount)
{
val id = this.mIds[i]
val view: View = parent.getViewById(id) ?: continue
view.alpha = alpha
}
}
}
}
Then it can be used as you would expect:
myGroup.alpha = 0.5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ahmad Melegy |
Solution 3 | galcyurio |
Solution 4 | Lucas B. |
Solution 5 | draziw |