Looking to make a small Android Compound View/Custom Component, where a FrameLayout is extended so that it has its own fully overlaying "Cover" view (that can intercept clicks and ripple) without repeating that cover in every XML instance.
I don't know whether it is best practice to re-layout the covering View in the Custom FrameLayout's onMeasure or onLayout methods, or if there's a simple XML trick that I'm missing (outside of just using a RelativeView since that would draw and re-draw the layout very often, but if I'm wrong here too correct me please)
CoveredFrameLayout.class
public class CoveredFrameLayout extends FrameLayout {
private View coverView;
public CoveredFrameLayout(@NonNull Context context) {
this(context, null);
}
public CoveredFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CoveredFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.widget_covered_framelayout, this);
//
setBackgroundColor(ContextCompat.getColor(context, R.color.light_red));
coverView = findViewById(R.id.widget_cover);
//
coverView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
coverView.animate()
.alpha(0)
.setDuration(1000)
.withEndAction(new Runnable() {
@Override
public void run() {
coverView.setVisibility(GONE);
}
})
.start();
view.setOnClickListener(null);
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
}
widget_covered_framelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:id="@+id/widget_cover"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@color/transparent_yellow400"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:visibility="visible" />
</merge>
The XML CoveredFrameLayout being used
<com.example.oliver.content.ui.widgets.CoveredFrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="hello" />
</com.example.oliver.content.ui.widgets.CoveredFrameLayout>
And here's a screenshot of what this is looking like currently. Just trying to learn the best practice to get that yellow square to dynamically cover the whole FrameLayout! Thanks y'all!
Here is the end goal result, however this result was only achieved by hardcoding the Cover View's height and width to the pre-calculated height and width in pixels of the view itself. A dynamic version of this could work fine, but I don't know what is the best practice for where in the onMeasure/onLayout cycle to put this code.

