4

Is there any way to force ViewPager2 RTL direction? I have set android:layoutDirection="rtl" to viewpager2 but because of android:supportsRtl="false" in manifest it is still LTR. How can i solve this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44

2 Answers2

3

I didn't find an official solution so I firt rotate viewpager with :

viewPager2.rotationY = (180).toFloat()

and overide onBindViewHolder() in FragmentStateAdapter like below :

   override fun onBindViewHolder(
        holder: FragmentViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        holder.itemView.rotationY = 180.toFloat()
    }

That's work for me

Keyvan Norouzi
  • 149
  • 1
  • 7
0

set supportsRtl=true and add

<item name="android:layoutDirection">ltr</item>

in your app theme for set LTR direction as globally (It is same that android:supportsRtl="false") and define style like below:

<style name="AppTheme.RTL" parent="AppTheme">
    <item name="android:layoutDirection">rtl</item>
</style>

and set this style as theme for viewpager or other view that you want be RTL . Make sure to use android:theme instead of style

<androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.Dark.RTL"
            android:layout_height="match_parent"
            android:background="#1F1F1F"
            android:saveEnabled="false" />