I need to do a partially transparent layout with a completely tranparent rectangle in the middle. The best approach I found to do that was this suggestion, which worked pretty well. The problem is that I actualy needed this rectangle to be horizontal (as if the paddings were bigger vertically than horizontally). In the thread I mentioned they said it is possible to do that implementing the shapes programmatically, but I'm new to this language and I wasn't able to do that. So far I did this:
activity.xml has this layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/pattern"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/hole_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity.kt has this code
val frameLayout = findViewById<FrameLayout>(R.id.hole_frame)
val drawable = ShapeDrawable(RectShape())
drawable.paint.color = -0x34000000
drawable.paint.style = Paint.Style.STROKE
drawable.paint.strokeWidth = 200f
frameLayout.background = drawable
I already tried to set padding using drawable.setPadding but that didn't work. If you have a solution for that or any other way to do this rectangular "hole" in the middle of the layout I'd appreciate.
My approach - using only layout. No programming.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="@color/semi_transparent_grey"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="80"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:background="@color/semi_transparent_grey"/>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"/>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:background="@color/semi_transparent_grey"/>
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="@color/semi_transparent_grey"/>
</LinearLayout>
</FrameLayout>
You can see here nested ViewGroups and Views which widths and heights are regulated by their percantage weight.10%-80%-10% is width distribution in horizontal direction. 40%-20%-40% - in vertical direction inside inner stripe. Of course, you can set your own values.