I have a viewpager and it contains some information but it lets me swipe the pages from left to right how can I make it swipe from left to right which means change its direction?
-
Show what have you tried ? – GrIsHu Sep 13 '13 at 08:44
-
If its a viewpager, you can swipe to both direction. – Basim Sherif Sep 13 '13 at 08:45
-
1http://stackoverflow.com/questions/13489723/how-to-reverse-the-direction-of-viewpager-to-left-to-right-order – Anukool Sep 13 '13 at 08:45
-
@GrlsHu I tried adding android:layoutdirection="rtl" inside the viewpager and then android:textdirection="rtl" and now I am working with the starting position but don't know if it has to do with it.. – User Sep 13 '13 at 08:46
-
@Basim Sherif yes I know but I need it to start the swipe from right to left it starts swiping from left to right – User Sep 13 '13 at 08:47
-
@Anukool thanks will see that . – User Sep 13 '13 at 08:47
14 Answers
I think you can set current page to the last page using viewPager.setCurrentItem()
. Then it will swipe left to right .. :)

- 960
- 2
- 11
- 20
It is the very simple technique to change the swipe direction of the Viewpager.
There are two simple step which has to follow you,
1. Change the rotation of the View pager,
viewpager.setRotationY(180);
2. Then again change the direction of the fragment container which is the child of viewpager,
recyclerView.setRotationY(180);
Note: In my case, I have used recyclerview as the child of the view pager.
This is 100% worked for me.

- 2,971
- 2
- 19
- 39
-
it flipped my images also .. text is also flipped ... its not the proper solution – Muhammad Jamal Jul 04 '17 at 14:23
-
Because maybe you have rotated your layout only once. You have to rotate twice. Once parent layout than the child layout. Definitely, It will work properly. – Aman Gupta - ΔMΔN Jul 05 '17 at 06:18
-
1This is an extremely easy solution if all pagers use fragments and all fragments have the same base class, then you can just rotate all pagers and the view of every fragment that's part of a pager. Just two lines of code for me! :) – 0101100101 Aug 04 '17 at 02:54
-
this method has problem in HUAWEI Nova Plus and show nothing in page! – Fereshteh Naji Dec 04 '18 at 05:44
-
This is a good solution, but you should use this. mRecyclerView.setRotationY(180); for the internal recycler view – HusseinKamal Oct 01 '20 at 09:14
-
@user7168064 I think, I used the same, what you have written. – Aman Gupta - ΔMΔN Oct 07 '20 at 09:45
You can swipe in both directions already. By default it shows page zero, but you could change that:
protected void onCreate(Bundle savedInstanceState) {
// ...
viewPager.setCurrentItem(myFragmentCount-1);
// ...

- 2,821
- 2
- 13
- 19
-
thanks that's what I did when Anshish kumar answered me will mark it as usefull.hope that this will get you some points. – User Sep 13 '13 at 08:53
-
I prefer to set smooth scrolling to `false` to transit immediately by calling `viewPager.setCurrentItem(myFragmentCount-1, false)`. – Hafez Divandari Jan 31 '16 at 04:06
Don't use rotation with ViewPager if you're supporting API28 and above, because it makes ViewPager swiping too difficult on android pie, see this question. This Library helped me to implement RTL ViewPager https://github.com/duolingo/rtl-viewpager
Add it to dependencies and just make sure you add layoutDirection="locale" to the RtlViewPager
<com.duolingo.open.rtlviewpager.RtlViewPager
android:layoutDirection="locale"
android:keepScreenOn="true"
android:id="@+id/quranViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
/>
Update
ViewPager2 is locale friendly so it's automatically will change swipe direction according to the current device locale.
Some helpful links on the implementation of ViewPager2:

- 3,999
- 1
- 17
- 28
I'm sure it's not relevant for the op, but I found a solution that worked for me (as modifying the data was a problem for my case). I simply rotated the viewpager 180 degrees in the Y axis, and then rotated it's subviews again in 180.
android.view.View
public void setRotationY(float rotationY)
Hope it helped anyone, as I think this is the most close to how Android mirrors all the os when change to rtl language.

- 1,274
- 1
- 17
- 30
-
Set rotation will change the text inside view pager be displayed in rtl order too. {e.g. rightToLeft => tfeLoTthgir} – Kimmy Apr 13 '18 at 20:46
-
-
You can do something like this,
ViewPager.setCurrentItem(lastposition);

- 5,384
- 7
- 48
- 90
my solution. dynamically:
public class RTLViewPager extends ViewPager {
public RTLViewPager(Context context) {
super(context);
}
public RTLViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setAdapter(PagerAdapter adapter) {
super.setAdapter(adapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if(getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL){
setCurrentItem(adapter.getCount()-1);
}
}
}
}

- 12,743
- 6
- 56
- 63
I know this question is old, but here is my version of things:
In order to get your viewpager to swipe RTL , regardless if it has fragments or views inside it you need to do the following things
step 1:
define an integer: R.integer.angle_rotation_for_rtl
for values
it is 0
and for values-rtl
it is 180
step 2:
viewPager.setRotationY(getResources().getInteger(R.integer.angle_rotation_for_rtl));
step 3:
viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
@Override
public void transformPage(@NonNull View page, float position) {
page.setRotationY(getResources().getInteger(R.integer.angle_rotation_for_rtl));
}
});
And the result - a smoothly working viewpager , no extra ifs

- 13,521
- 11
- 61
- 126
First u need to implements OnPageChangeListener in your class and second set the listener to your viewpager like viewpager.setOnPageChangeListener(yourclass.this); Hope This will work.

- 614
- 4
- 10
in layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layoutDirection="rtl">
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="locale"/>
</LinearLayout>
in code:
//region swipe right to left
pager.setRotationY(180);
pager.setPageTransformer(false, new ViewPager.PageTransformer() {
@Override
public void transformPage(@NonNull View page, float position) {
page.setRotationY(180);
}
});
//endregion

- 374
- 2
- 13
For letting your horizontal ViewPager behave like RecyclerView when LayoutDirection change just add this method to your custom ViewPager;
public class LoopViewPager extends ViewPager {
....
...
public void setLayoutDirection(int layoutDirection) {
if(layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
setRotationY(0);
}
else if(layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) {
setRotationY(180);
}
}
and where you use your viewPager set it dynamically like this;
viewPager.setLayoutDirection(context.getResources().getConfiguration().getLayoutDirection());

- 306
- 1
- 3
- 9
I totally understand that the question is answered perfectly; but if you're using RTL language; for instance if you have a book that you want to show in ViewPager; then besides the answer, you might also consider to flip the pages over to get the right page; to do that modify your ViewPager adapter's getItem()
method
@Override
public Fragment getItem(int position) {
return SomeFragment.newInstance(N_PAGES - position - 1); // to filp the pages
}

- 37,492
- 7
- 60
- 84
You don't any third party library. Android recently added new UI component called ViewPager2 which supports RTL View Pager.
https://developer.android.com/jetpack/androidx/releases/viewpager2
Please find below link to an answer. The answer talks about Migration guide and sample github app to support RTL view pager.

- 1,806
- 22
- 21
If You want to move left to right -> viewPager.setCurrentItem(viewPager.currentItem+1,true)
//pass Boolean for smooth scrolling
If You want to move right to left ->
viewPager.setCurrentItem(viewPager.currentItem-1)
//without smooth scrolling

- 95
- 1
- 11