Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Set shape as ImageView background to get top corners rounded

Posted on November 14, 2022

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
    }
    

Result:
enter image description here

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.

image with four straight corners

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

READ  [ANSWERED] android - CameraX is not showing preview in Service
Powered by Inline Related Posts

image with four straight corners

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.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress