Show images using viewpager - android

I am using viewpager to show images from drawable folder
The images named:
a1
a2
a3...
To a700
So i use this code to get all images in viewpager
mImages=new int[700];
for (int i = 0; i < 700; i++) {
mImages[i] = getResources()
.getIdentifier("a"+i, "drawable", getPackageName()); }
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
It work fine and show all images
But when i use this code there is no first image in viewpager
The first image is appeared after first viewpager swip
Why?
How to start from a1 in viewpager
This is the adapter
private class ImagePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return mImages.length;
}
public int getImageId(int pos){return mImages[pos];}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.this;
final TouchImageView imageView = new TouchImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
And i use this cose in mainactivity
public class MainActivity extends Activity {
ViewPager viewPager;
public int[] mImages = new int[]{
R.drawable.startpic};

try
mImages=new int[700];
for (int i = 0; i < 700; i++) {
mImages[i] = getResources()
.getIdentifier("a"+ (i + 1), "drawable", getPackageName()); }
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
you do not have an image named a0, so make sure you are not starting at a0. With the logic you have currently, I think you will also be missing a700 at the end (your for loop only goes to when i is 699).

Related

Android ViewPager not showed on screen inside ScrollView

I create a ViewPager dynamically :
private View createCarousel(Context context)
{
ViewPager pager = new ViewPager(context);
ArrayList<Drawable> drawables = new ArrayList<>();
String[] carouselImages = mDisplayedGame.getCarouselImages();
if (carouselImages != null) {
for (String s : carouselImages) {
drawables.add(ImageLoader.getInstance().getDrawable(s));
}
pager.setAdapter(new ImageAdapter(context, drawables));
pager.setCurrentItem(0);
}
return pager;
}
Note: carouselImages != null always and ImageLoader works correctly.
Adapter :
public class ImageAdapter extends PagerAdapter
{
Context context;
private ArrayList<Drawable> mDrawables;
public ImageAdapter(Context context, ArrayList<Drawable> drawables, float width)
{
this.context = context;
mDrawables = drawables;
}
#Override
public int getCount()
{
return mDrawables.size();
}
#Override
public float getPageWidth(int position)
{
return 1f;
}
#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);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageDrawable(mDrawables.get(position));
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((ImageView) object);
}
}
And add to container :
mRootView.addView(createCarousel(this), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
And ViewPager not showed. What i'm doing wrong?
Confuse about this situation but magic happens in this line :
mRootView.addView(createCarousel(this), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
when i change height to 100 :
mRootView.addView(createCarousel(this), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100 ));
Content was appeared on screen.
So problem in setting layout params.
In my case i need to display fullscreen images so i just calculate dimens when app start and set manually them to constructor params.
Didn't you forget to set layout params for child ImageViews ? How about try something like:
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageDrawable(mDrawables.get(position));
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
((ViewPager) container).addView(imageView, 0);
return imageView;

How to set ViewPager to show images in a loop

I have a viewpager showing the images, at the moment i have 3 images. It is going like 1->2->3 it stops on 3 but i want it to go to 1. Same as it should work from 1->3.
Here is my code:
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);
}
}
How can i set the loop for it. I hope i explained well. Thank-you.
The best way to do this is to use Timer in combination with Runnable. Start by referencing your ViewPager:
imagePager = (ViewPager) view.findViewById (R.id.your_view_pager);
Then you need to set adapter to your imagePager like this (let's suppose you have already instantiated your ImageAdapter):
imagePager.setAdapter(imageAdapter);
Continue:
private Timer swipeTimer = new Timer();
private Handler handler = new Handler();
imagePager.setCurrentItem(0); // this is the beginning
final Runnable changeCurrentImage = new Runnable() {
public void run() {
currentImage++;
if (currentImage >= imageAdapter.getCount()) {
currentImage = 0; // go back to the first image
}
imagePager.setCurrentItem(currentPage, true);
}
};
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(changeCurrentImage);
}
}, 5000, 5000); // here you can set time interval

Media Player is player multiple sounds and text view not showing right text

I am creating an application in which I have multiple images,sounds and texts.I want to show single image on a screen and for this I am using view pager and implementing on Page change Listener interface for keeping track of pages.But in my case images are scrolling as I want but I want to play sound according means each sound is different for every image in my case first 2 sounds are being played simultaneously and the text which is being set is in the array and it is getting second index position.I am unable to get the problem.Any suggestion would be highly appreciated.
Here is my code,
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setOnPageChangeListener(adapter);
viewPager.setAdapter(adapter);
private class ImagePagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
Context context;
MediaPlayer mp;
private int[] mImages = new int[]{R.drawable.airplane,R.drawable.ambulance,R.drawable.apple,
R.drawable.ball_img,R.drawable.bear_img,R.drawable.bird_img,
R.drawable.cake_img,R.drawable.cat,R.drawable.car,R.drawable.clock,
R.drawable.drum_img,R.drawable.dinasour,R.drawable.dog,R.drawable.dolphine,R.drawable.duck,
R.drawable.eagle,R.drawable.elephant,
R.drawable.fish,R.drawable.frog,
R.drawable.horse,
R.drawable.insect,
R.drawable.jagular,
R.drawable.kangroo ,
R.drawable.lion,
R.drawable.monkey,
R.drawable.nose,
R.drawable.owl ,
R.drawable.peacock ,
R.drawable.quill,
R.drawable.rabit ,
R.drawable.star ,
R.drawable.tiger,
R.drawable.umbrella ,
R.drawable.vulture,
R.drawable.wolf ,
R.drawable.xylophone ,
R.drawable.yoyo ,
R.drawable.zebra
};
private int [] soundIds = {R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance};
String description [] = {"Airplane","Ambulance","Apple","Ball","Bear","Bird","Cake","Cat","Car","Clock"
,"Drum","Dinasour","Dog","Dolphine","Duck","Eagle","Elephant","Fish","Frog","Horse","Insect"
,"Jagular","Kangroo","Lion","Monkey","Nose","Owl","Peacock","Quill","Rabbit","Star","Tiger"
,"Umbrella","Vulture","Wolf","Xylophone","Yoyo","Zebra"};
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container,int position) {
context = ZoomedLetters.this;
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mp = MediaPlayer.create(context,soundIds[position]);
mp.start();
descriptions.setText(description[position]);
ImageView imageView = new ImageView(context);
int padding = context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
descriptions.setText(description[position]);
ImageView imageView = new ImageView(context);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;*/
}
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
}

Open activity or dialog box when i press image i have in my viewPAger

in My root Activity i have Slide show that displayed by the VIewPager .
in my case i want to open new activity or dialog box or something like this when i pressed image on Slide show .
Normally i used OnclickListener and assign picture id to it and don't get any Feedback .
RootActivity.java
public class RootActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
ImageAdapter.setTimer(viewPager, 3, 0);
// End Image slide show
ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
vp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.print("OK ...");
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends PagerAdapter {
final static Handler handler = new Handler();
public static Timer swipeTimer ;
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.ax1,
R.drawable.ax2,
};
ImageAdapter(Context countDownTimer){
this.context=countDownTimer;
}
#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);
}
public static void setTimer(final ViewPager myPager, int time, final int curPage){
final Runnable Update = new Runnable() {
int NUM_PAGES =5;
int currentPage = curPage ;
public void run() {
if (currentPage == NUM_PAGES ) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
}
Okay, first things first...
1. This line, in RootActivity.onCreate() does not make a lot of sense:
ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
You're trying to find a ViewPager object in the layout using a drawable resource id. So delete this line and everything in onCreate() that comes after it.
2. You can only attach an OnClickListener to an existing view and since there is no OnItemClickListener for PagerAdapters, you need to attach the OnClickListener to the appropriate Views from within the Adapter's instantiateItem() method.
I have attached a modified version of your code representing these points.
RootActivity.java
public class RootActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
ImageAdapter.setTimer(viewPager, 3, 0);
// End Image slide show
}
}
ImageAdapter.java
Look for the // Begin Added by SpaceLord comments
public class ImageAdapter extends PagerAdapter {
final static Handler handler = new Handler();
public static Timer swipeTimer;
Context context;
private int[] GalImages = new int[]{
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.ax1,
R.drawable.ax2,
};
ImageAdapter(Context countDownTimer) {
this.context = countDownTimer;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == 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]);
// Begin Added by SpaceLord
imageView.setOnClickListener(itemClickListener);
imageView.setTag(position);
// End Added by SpaceLord
container.addView(imageView, 0);
return imageView;
}
// Begin Added by SpaceLord
private final View.OnClickListener itemClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int tag = (Integer) v.getTag();
switch (tag) {
// Do whatever you need to for each slide click here
case 0:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on ONE");
break;
case 1:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on TWO");
break;
case 2:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on THREE");
break;
case 3:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX1");
break;
case 4:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX2");
break;
}
}
};
// End Added by SpaceLord
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
public static void setTimer(final ViewPager myPager, int time, final int curPage) {
final Runnable Update = new Runnable() {
int NUM_PAGES = 5;
int currentPage = curPage;
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time * 1000);
}
}
Directly set onClickListener(...) to your ImageView in your ImageAdapter like:
#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]);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your job
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}

android swipe left right content?

i have jSON array content image url and caption , the list content over 100 object
this is example code :
[{"url":"http:\/\/web.com\/image\/1.jpg","cap":"image 1"},{"url":"http:\/\/web.com\/image\/2.jpg","cap":"image 2"},{"url":"http:\/\/web.com\/image\/3.jpg","cap":"image 3"}]
what i am looking for is this :
sure its must content left right pagination swipe picture and caption
is there any asapter can do that ? can any one lead me what i have to use pager or fragment or any thing else ?
please guys any advice ?
you can use this AndroidViewPagerGallery
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);
}
}
and this adapter you can customize it to create layout and change the picture and captions
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);
}
}

Categories

Resources