How to make a corner radius in android

I am trying to make a gridview that will have an image and text underneath with a corner radius effect. While this seems impossible. Here is my xml activity that is holding the gridview:

<ScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="match_parent">
    <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="140dp"
        android:padding="5dp"
        android:layout_marginTop="10dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="5dp"
        android:horizontalSpacing="5dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>
</ScrollView>

      

Here is the xml adapter:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_height="wrap_content"
              android:padding="2dp"
              android:layout_margin="20dp"
              android:background="@drawable/grid_corner_radius"
              android:layout_width="wrap_content">
    <ImageView
        android:layout_width="200dp"
        android:background="@drawable/cat_life"
        android:layout_height="160dp"/>

    <TextView
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:id="@+id/credit_textView"
        android:gravity="center"
        android:text="Life"
        android:textColor="#FFFFFF"
        android:background="@color/colorPrimary"
        android:textSize="15dp"/>
</LinearLayout>    

      

And finally, here is the magic code for the radius corner:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <solid android:color="#C0C0C0"></solid>
            <corners android:radius="15dp"></corners>
        </shape>
    </item>
</selector>

      

And the result is the same:

enter image description here

See image. The clear edge of the rectangle is visible. What is the possible solution?

+3


source to share


2 answers


You can encapsulate your xml adapter with CardView and set cornerRadius for it. It should work.

Add compile 'com.android.support:cardview-v7:24.2.0'

gradle to your app.



<android.support.v7.widget.CardView xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:card_view="https://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_margin="20dp"
card_view:cardCornerRadius="4dp">
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_height="match_parent"
          android:layout_width="match_parent">
<ImageView
    android:layout_width="200dp"
    android:background="@drawable/cat_life"
    android:layout_height="160dp"/>

<TextView
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:id="@+id/credit_textView"
    android:gravity="center"
    android:text="Life"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimary"
    android:textSize="15dp"/>
      </LinearLayout>    
</android.support.v7.widget.CardView>

      

+4


source


try this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:layout_margin="20dp"
    android:background="@drawable/corners_layout"
    android:layout_width="wrap_content">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="160dp"
        android:background="@drawable/corners_image"/>

    <TextView
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:id="@+id/credit_textView"
        android:gravity="center"
        android:text="Life"
        android:textColor="#FFFFFF"
        android:background="@drawable/corners_text"
        android:textSize="15dp"/>
</LinearLayout>

      

Use this style for your layout:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimaryJob"/>
    <corners android:radius="20dp"/>
</shape>

      



Use this style for text view:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorBlue"/>
    <corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
</shape>

      

Use this style to view images:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimaryJob"/>
    <corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/>
</shape>

      

+2


source







All Articles