'How to set custom view corner color transparent in android
Hey I am working in custom view in android. Inside my custom view, I am using view-pager and I want to view pager to be corner radius be 16dp. I did this successfully but the problem is there custom view corner showing some kind of translucent color. So how can i avoid this?. If I did in custom view to be rounded it's working fine, but I don't want to be whole view because inside custom view I have so many testView, images etc. I only want to do in view-pager. I am attaching image how it looks like. I marked in all corner in image. Can someone please guide me.
GalleryView.kt
class GalleryView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
private var binding: GalleryViewBinding = GalleryViewBinding.inflate(LayoutInflater.from(context), this, true)
init{
//.. Initialise code logic.
}
}
gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/galleryContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/galleryPager"
android:layout_width="match_parent"
android:layout_height="224dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
// Textview, images etc
</androidx.constraintlayout.widget.ConstraintLayout>
I am not adding code for viewpager adapter, instead adding layout and how can I achieved rounded corner
viewpager_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gallery_pager_item_background">
<ImageView
android:id="@+id/main_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
gallery_pager_item_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle">
<solid android:color="@color/black" />
<corners android:radius="16dp" />
</shape>
Solution 1:[1]
The corners that are showing are probably from a ViewGroup that frames the ViewPager. It is hard to know since you haven't posted all of your code.
The culprit could the the Decor View. Try the following in the main activity to see if the color of the edges change.
getWindow().getDecorView().setBackground(new ColorDrawable(getResources()
.getColor(android.R.color.holo_green_light)));
If that doesn't work, look at other ViewGroup parents to see where the color is coming in. You can use the Layout Inspector to look into the view hierarchy.
The problem could also be in how you are constructing the custom view.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cheticamp |

