Android viewPager image slide right to left
Asked Answered
P

5

9

I want to add an image slide. But cannot make it slide from right to left. (for languages like Arabic or Hebrew)

I checked nearly all replies in stackoverflow, but can not find a clear solution.

I write here the whole code.

Please write me clearly, I am not a professional

mainActivity;

package com.manishkpr.viewpagerimagegallery;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    viewPager.setAdapter(adapter);


  }


}

here is the ImageAdapter.java;

package com.manishkpr.viewpagerimagegallery;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);


      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }

and here is the layout file;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

          <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

</RelativeLayout>
Plough answered 11/6, 2016 at 8:39 Comment(3)
Simply remove visited page index from arrayMuscle
oh! i'm sorry!, you want change slide position right to left for arabic, i miss understand so ignore previous comments please, looking answer forward to you!Muscle
@Survivor , I am not a professional.. could you explain a bit?Muddy
L
11

Create a Layout file pager_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView" />
</LinearLayout>

Change your PagerAdapter like this:

public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
    R.drawable.one,
    R.drawable.two,
    R.drawable.three
};

LayoutInflater mLayoutInflater;

ImageAdapter(Context context){
    this.context=context;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
  return GalImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
  return view == ((LinearLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
    imageView.setImageResource(GalImages[position]);

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((LinearLayout)object);
}
}

EDIT 1:

A trick :

public class MainActivity extends Activity {


@Override
  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(adapter.getCount()-1);

}

I hope this helps :)

Lanellelanette answered 11/6, 2016 at 8:48 Comment(7)
thanks a lot. but I dont have PagerAdapter? must I add a new java file, called PagerAdapter.java ?Muddy
Error:(34, 25) error: cannot find symbol variable mLayoutInflaterMuddy
Please see my edited code i have edited the answer.. and defined mLayoutInflater. Use Above code of adapter.Lanellelanette
thanks. this time there s no error. but slider is the same.. cannot go from right to left. it shows the one.png firstly.. then, to see next picture, I must swipe from right to left. but i want it to swipe from left to right like reading Holy Qoran..Muddy
becuase of my reputaion is not enough, I cannot upvote.. but I selected it as right answer..Muddy
hi @janki gadhiya .. can I add this code "pinch Zoom" ? is it easy or hard?Muddy
Don't forget: @Override public void destroyItem (ViewGroup container, int position, Object object){ ((ViewPager) container).removeView((View) object); // more code if needed }Duyne
R
3

ViewPager is not support RTL.

You have to create custom ViewPager for swipe Right To Left for Arabic and other languages.

If you don't want to make code then there are lots of library available you can use one of them.

1. RtlViewPager

2. RtlViewPager

Recipient answered 11/6, 2016 at 9:17 Comment(1)
ViewPager2 does and it would be preferred today.Median
C
1

Instead of using ViewPager I suggest you to use SwipeDeck Because as Niranj Patel said ViewPager does not Support Right to left.

<?xml version="1.0" encoding="utf-8"?>
<com.daprlabs.cardstack.SwipeFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:swipedeck="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.daprlabs.cardstack.SwipeDeck
        android:id="@+id/swipe_deck"
        android:layout_width="match_parent"
        android:layout_height="480dp"
        android:padding="20dp"
        swipedeck:card_spacing="10dp"
        swipedeck:max_visible="3"
        swipedeck:render_above="true"
        swipedeck:rotation_degrees="15" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Button" />

</com.daprlabs.cardstack.SwipeFrameLayout>
Concepcionconcept answered 11/6, 2016 at 9:22 Comment(1)
ViewPager2 does and it would be preferred today.Median
N
0

Just smiple solution but it is not professional

Write this in xml for VeiwPager

 android:rotationY="180" 

and also in veiw_item

           android:rotationY="180".
Nonlegal answered 8/4, 2017 at 11:27 Comment(0)
S
0

ViewPager2 supports RTL paging. From docs:

RTL paging is enabled automatically where appropriate based on locale, but you can also manually enable RTL paging for a ViewPager2 element by setting its android:layoutDirection attribute:

<androidx.viewpager2.widget.ViewPager2
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layoutDirection="rtl" />     
Sartain answered 15/1, 2021 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.