Skip to content

Instantly share code, notes, and snippets.

@amirulzin
Last active May 29, 2019 02:07
Show Gist options
  • Save amirulzin/6230090fcd0e59801107f7ef5bc7d041 to your computer and use it in GitHub Desktop.
Save amirulzin/6230090fcd0e59801107f7ef5bc7d041 to your computer and use it in GitHub Desktop.
package android.support.constraint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
/**
* Custom Group for ConstraintLayout that also supports alpha
*/
public class CustomGroup extends Group {
public CustomGroup(Context context) {
super(context);
}
public CustomGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void updatePreLayout(ConstraintLayout container) {
int visibility = this.getVisibility();
float elevation = 0.0F;
if (Build.VERSION.SDK_INT >= 21) {
elevation = this.getElevation();
}
for(int i = 0; i < this.mCount; ++i) {
int id = this.mIds[i];
View view = container.getViewById(id);
if (view != null) {
view.setVisibility(visibility);
// getAlpha() came from the inherited View class, and can be defined in CustomGroup XML via android:alpha
// or programmatically via setAlpha()
view.setAlpha(getAlpha());
if (elevation > 0.0F && Build.VERSION.SDK_INT >= 21) {
view.setElevation(elevation);
}
}
}
}
}
@amirulzin
Copy link
Author

Usage:

<!-- Use whatever package name you use -->
<android.support.constraint.CustomGroup
            android:id="@+id/myGroup"
            android:alpha="0.2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="myview1,myview2"
            tools:ignore="MissingConstraints" />

or in code:

    myGroup.setAlpha(0.2f);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment