Solution 1 :
To make it working just set outline to your card container layout in code.
-
In layout xml add id to card container:
<androidx.constraintlayout.widget.ConstraintLayout android_id="@+id/card" android_layout_width="match_parent" android_layout_height="wrap_content" android_layout_marginLeft="16dp" android_layout_marginTop="16dp" android_layout_marginRight="16dp" android_background="@drawable/layout_bg" android_elevation="4dp">
-
In your MainActivity onCreate:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) card.clipToOutline = true card.outlineProvider = ViewOutlineProvider.BACKGROUND }
Problem :
I’m trying to build a layout with a ConstraintLayout with rounded corners inside a ScrollView.
At the top of the layout I want to place an image that should have the top corners rounded (to match the layout corners) and the bottom corners straight.
Following the suggestions I found in this StackOverflow question here’s my code:
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".MainActivity"
android_orientation="vertical">
<ScrollView
android_id="@+id/scroll_layout"
android_layout_width="match_parent"
android_layout_height="0dp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintLeft_toLeftOf="parent"
app_layout_constraintRight_toRightOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_weight="7.5"
android_background="@color/darkModePrimary">
<androidx.constraintlayout.widget.ConstraintLayout
android_layout_marginLeft="16dp"
android_layout_marginRight="16dp"
android_layout_marginTop="16dp"
android_layout_width="match_parent"
android_background="@drawable/layout_bg"
android_layout_height="wrap_content">
<ImageView
android_background="@drawable/layout_bg"
android_src="@drawable/house_medium"
android_id="@+id/house_image"
android_layout_width="match_parent"
android_layout_height="200dp"
android_scaleType="fitXY"
app_layout_constraintRight_toRightOf="parent"
app_layout_constraintLeft_toLeftOf="parent"
app_layout_constraintTop_toTopOf="parent"/>
<TextView
android_layout_marginTop="32dp"
android_gravity="center"
app_layout_constraintLeft_toLeftOf="parent"
app_layout_constraintRight_toRightOf="parent"
app_layout_constraintTop_toBottomOf="@id/house_image"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Some text"
android_textSize="24sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
This is the xml file where I define the shape for the background of the ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns_android="http://schemas.android.com/apk/res/android">
<solid android_color="@android:color/white"/>
<corners android_radius="10dp"/>
<padding
android_left="0dp"
android_top="0dp"
android_right="0dp"
android_bottom="0dp" />
</shape>
If I set this shape as the background of both the Constraint Layout and the ImageView, I get – as expected – an image with all four rounded corners, as shown here.
I’ve tried to create another shape to use as background of the image, like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns_android="http://schemas.android.com/apk/res/android">
<solid android_color="@android:color/white"/>
<corners android_bottomRightRadius="0dp"
android_bottomLeftRadius="0dp"
android_topLeftRadius="10dp"
android_topRightRadius="10dp"/>
<padding
android_left="0dp"
android_top="0dp"
android_right="0dp"
android_bottom="0dp"/>
</shape>
But when I set it as background of the ImageView I don’t get what I want, all four corners of the image are now straight
What am I doing wrong?
Comments
Comment posted by dmtr
Just put your card layout inside CardView, and change app:cardCornerRadius.
Comment posted by Mike
For a series of reasons I decided not to use CardViews but to build a layout myself so unfortunately I can’t do that
Comment posted by dmtr
ok, then try to add size attribute.