Home > Mobile >  How to change rounded corner in viewpager2 Android
How to change rounded corner in viewpager2 Android

Time:02-05

Hey I am working in viewpager 2 in android. I want to give rounded corner in view pager 2 but it's not working. Can someone guide me

<androidx.viewpager2.widget.ViewPager2
     android:id="@ id/galleryPager"
     android:background="@drawable/round_drawable"
     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" />

round_drawable.xml

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

UPDATE

I used RecyclerView.Adapter inside my viewpager adapter and xml look like this

<?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/rounded_corner">

        <ImageView
            android:id="@ id/mainImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerInside"
            android:src="@drawable/gallery_placeholder"
            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>

viewpager.xml

<androidx.viewpager2.widget.ViewPager2
         android:id="@ id/galleryPager"
         android:background="@android:color/transparent"
         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" />

It look like this now

CodePudding user response:

Use round background for your elements inside viewpager not particulary viewpager

For example you have 2 fragment inside view pager and every fragment contain linear layout inside, Use rounded backgroud for those linear layout If you need example tell me:))

CodePudding user response:

The viewPager page fragment will obscure the background of the ViewPager as it is drawn on top of it; if you want to force that you have to use a transparent background on the root layout of them by setting android:background="@android:color/transparent"

  •  Tags:  
  • Related