4

I'm using this library : https://github.com/astuetz/PagerSlidingTabStrip and it works perfectly except that i want to add another language which is arabic so i want the tabs to swipe RTL and that every thing will be in reverse order.

I tried to set the Gravity of the tabs to right but it didn't work .. I also saw this library: https://github.com/dreamlearn/viewpager

but i don't know how to use it .. please help and thanks in advance

user3341448
  • 51
  • 1
  • 4

2 Answers2

0

What you need to do is to reverse the order of the fragments (when initialize them to the ViewPager) and do viewPager.setCurrentItem(ViewPagerSize). But it's has to happen only if the locale is RTL so you have to use a method to determine if it is.

This is the code:

 public static boolean isRTL() {
    return isRTL(Locale.getDefault());
 }

 public static boolean isRTL(Locale locale) {
    final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
    return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
            directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
 }

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    if (isRTL()) {
        // The view has RTL layout
        adapter.addFragment(new S5(), getString(R.string.stage5));
        adapter.addFragment(new S4(), getString(R.string.stage4));
        adapter.addFragment(new S3(), getString(R.string.stage3));
        adapter.addFragment(new S2(), getString(R.string.stage2));
        adapter.addFragment(new S1(), getString(R.string.stage1));
    } else {
        // The view has LTR layout
        adapter.addFragment(new S1(), getString(R.string.stage1));
        adapter.addFragment(new S2(), getString(R.string.stage2));
        adapter.addFragment(new S3(), getString(R.string.stage3));
        adapter.addFragment(new S4(), getString(R.string.stage4));
        adapter.addFragment(new S5(), getString(R.string.stage5));
    }
    viewPager.setAdapter(adapter);
}

And for the tabs you need to set the direction to LTR (It's looks messy when it's RTL).

So just used this code (It's only available in API 17+):

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        tabLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
    }

I could'nt find a way to do this on pre API 17 but you don't have to set the min SDK to 17.

Itiel Maimon
  • 834
  • 2
  • 9
  • 26
0

Hurray Android recently added new UI component called ViewPager2 which supports RTL Layout.

https://developer.android.com/jetpack/androidx/releases/viewpager2

  • RTL (right-to-left) layout support
  • Reliable Fragment support (including handling changes to the underlying Fragment collection)
  • Dataset change animations (including DiffUtil support)
  • Vertical orientation support

Below links talks about migration guide and sample app :

https://developer.android.com/training/animation/vp2-migration

https://github.com/android/views-widgets-samples/tree/master/ViewPager2

Alok Gupta
  • 1,806
  • 22
  • 21