Search code examples
javaandroidxmlandroid-studioandroid-cardview

Android CardView Doesn't round corners when using app:cardCornerRadius="5dp"


I have an array of images that I want to display and I use smarteists image slider inside a cardview and now I want to round the corners of this cardview. I use app:cardCornerRadius="5dp" in the xml file and it doesn't work, when I try .setRadius(5) in the java file it also doesn't do anything. The strange thing is, before I used the image slider, I used a ViewPager and it rounded the corners just fine. I have no idea what I'm doing wrong so if anybody finds my mistake it would be greatly appreciated.

XML code:

    <androidx.cardview.widget.CardView
        android:id="@+id/NoLinkCardview"
        android:layout_width="392dp"
        android:layout_height="258dp"
        android:layout_marginTop="10dp"
        app:cardBackgroundColor="@color/colorPrimaryDark"
        app:cardCornerRadius="5dp"
        app:cardUseCompatPadding="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <com.smarteist.autoimageslider.SliderView
            android:id="@+id/NoLinkImageSlider"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:sliderIndicatorGravity="center_horizontal|bottom"
            app:sliderIndicatorMargin="15dp"
            app:sliderIndicatorOrientation="horizontal"
            app:sliderIndicatorPadding="3dp"
            app:sliderIndicatorRadius="1.8dp"
            />
    </androidx.cardview.widget.CardView>

Java Code:

        SliderView sliderView = findViewById(R.id.NoLinkImageSlider);
        SliderAdapter adapter = new SliderAdapter(this);
        List<SliderItem> currentSliderItems = new ArrayList<>();
        currentSliderItems.clear();
        assert currentPointOfInterest != null;
        for(int j = 0; j<currentPointOfInterest.getAmountOfPictures(); j++){
            SliderItem sliderItem = new SliderItem();
            sliderItem.setImageUrl(Objects.requireNonNull(picturesMap.get(currentPointOfInterest.getID()))[j]);
            currentSliderItems.add(sliderItem);
        }
        adapter.renewItems(currentSliderItems);
        sliderView.setSliderAdapter(adapter);
        sliderView.setInfiniteAdapterEnabled(true);
        sliderView.setIndicatorAnimation(IndicatorAnimationType.COLOR); 
        sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
        sliderView.setIndicatorSelectedColor(getColor(R.color.colorAccent));
        sliderView.setIndicatorUnselectedColor(getColor(R.color.colorAccentTransparent));

Solution

  • Call setClipToOutline(true) on your NoLinkCardview in Java code.

    CardView card = findViewById(R.id.NoLinkCardview)
    card.setClipToOutline(true)