android swipe left right content? - android

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);
}
}

Related

download image from viewpager in which image is stored in raw file

I've made a image slider from viewPager. The image shown in viewPager is stored in raw folder inside the project of android studio.
I want to request for the permission of user to download image, display progress bar when image is downloading and finally a tost message when image is downloaded.
Things I've done:
-I have made a imageview button to download on the viewpager.
-Progressbar with android:visibility="invisible"
-My code in MainActivty where image and download option are shown.
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
ProgressBar progressBar;
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie,
R.drawable.androidparty
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
viewPager = findViewById(R.id.view_pager);
final ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
ImageView btnDownload = findViewById(R.id.btnDownload);
btnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//what should I do? please help :(
}
});
}
}
code of viewpager adapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
public View currentImageView;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView view = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.into(view);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentImageView = (View)object;
}
}

Show images using viewpager

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).

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) {
}
}

Categories

Resources