Why does my ViewPager crash? - android

I have 25 pictures in my project and I have a Button in my first Activity. When click this Button I want my app show the first pic of 25, then I want to swipe to change my pics.
This is my PagerAdapter:
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);
}
}
Then create the xml file and add the ViewPager in layout
<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>
Then create main Activity and create the instance of 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);
}
}
Everything is good, but when my app arrive to 20th pic of my 25 pics this error appears:
unfortunately, gallery 3 has stopped
please read here :
i find out some pics are very big so this error appears , when i delete them app is good now.......
is not anyway else ??
i do not want delete my pics

Related

PagerAdapter change TextView when swiping

I'm using a PagerAdapter to swipe through a couple of ImageViews which works perfectly fine. I have a bunch of people in the gallery and I wish to add an individual name/description (id: speaker_name) to them. All I got to work is the same description for all of them. Im absolutely new to this and I am struggling for a day now to get it to work. All the solutions I found used fragments or an OnPageChangeListener but i couldnt figure out how to implement it.
This is what i've got:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/basicBackground">
</android.support.v4.view.ViewPager>
<TextView
android:id="#+id/speaker_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50sp"
android:text="The Name"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="30sp" />
</FrameLayout>
</RelativeLayout>`
PagerAdapter:
public class ImageAdapter extends PagerAdapter {
SpeakerActivity sa;
Context context;
int i = 0;
public int[] GalImages = new int[] {
R.drawable.ben,
R.drawable.brett,
R.drawable.mark,
R.drawable.dusan,
R.drawable.michael,
R.drawable.mike,
R.drawable.ollie,
R.drawable.rebecca,
R.drawable.sebastian,
R.drawable.thomas,
R.drawable.tomasz,
R.drawable.toni,
};
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);
}
}
Activity:
public class SpeakerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speaker_activity);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}
}
To change textview text when swiping you can do this inside oncreate() method:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position){
case 0:
//Do stuff
textview.setText("Ben");
break;
case 1:
//Do stuff
textview.setText("Brett");
break;
//Add other cases for the pages
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
This is the implementation of viewpager OnPageChangeListener() for me the best way.
let me give you the basic idea
ViewPager that the Activity layout is binded to.
ImageAdapter that extends ViewPager/FragmentPager that basically adds views or fragments into the viewpager so that the activity can show it to you.
the activity connects the two objects by setting the viewpager's adapter to your ImageAdapter
Like I said in #2 there are many ways you u can implement the ViewPager there are many sites out there.
refer to https://www.bignerdranch.com/
it explain in great detail

Touch to image view not working

I've made a simple app in which I am using 3 tabs. Each tab is having separate classes and layouts. First tab shows map. Second tab will show the camera from which the user can take picture(s) and third tab is off Pictures in which the captured images are viewed in the GridView. All tabs are working good. Now I want is to show full ImageView when a user tap on any image.
For all of this I am using this tutorial. So I followed each and every step in it and the things are going good. But for full ImageView the code is not working.
Below is my picture Fragment
Pictures.java
public class Pictures extends Fragment implements MainActivity.Updateable {
private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pictures, container, false);
gridView = (GridView) rootView.findViewById(R.id.grid_view);
utils = new Utils(getContext());
// Initilizing Grid View
InitilizeGridLayout();
// loading all image paths from SD card
imagePaths = utils.getFilePaths();
// Gridview adapter
adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
/*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
columnWidth);*/
// setting grid view adapter
gridView.setAdapter(adapter);
//((BaseAdapter) adapter).notifyDataSetChanged();
//adapter.notifyDataSetChanged();
return rootView;
}
private void InitilizeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
#Override
public void update() {
if(adapter !=null)
{
adapter.notifyDataSetChanged();
}
else
{
Log.d(getTag(),"null");
}
}}
Now for full screen view, I have made a new Activity a two separate Layouts one for screen and one for image as below
fullscreen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></LinearLayout>
fullscreenImage.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="#drawable/button_background"
android:textColor="#ffffff"
android:text="Close" /></RelativeLayout>
Now i created a Adapter class
FullScreenImageAdapter.java
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
#Override
public int getCount() {
return this._imagePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
imgDisplay.setImageBitmap(bitmap);
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}}
Now the full screen Activity
FullScreenViewActivity.java
public class FullScreenViewActivity extends Activity {
private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
viewPager = (ViewPager)findViewById(R.id.pager);
utils = new Utils(getApplicationContext());
Intent i = getIntent();
int position = i.getIntExtra("position",0);
adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
utils.getFilePaths());
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}}
Now when i click on any image nothing happens, I got no errors on logcat, no any warnings nor any app crash.
I have debugged the app but it doesn't goes to that point.
I'm stuck to it, and don't know what to do
Any help would be highly appreciated.
This might not be the answer that you are looking for! But still if your code does not work this will help you towards your solution.If you know the whole process you might be able to plug and play this.
This answer is taken from the same tutorial guy that you followed which has kind of a same pattern with your code. Note image array is hard-coded.
Have a look, Take a special note at AndroidGridLayoutActivity !
Launcher Activity : HomeActivity Other activity you need in your Manifest :FullImageActivity
grid_layout.xml >
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center">
</GridView>
full_image.xml >
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:id="#+id/full_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout
HomeActivity >
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
AndroidGridLayoutActivity > (check the comments in this)
public class AndroidGridLayoutActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//----------------------Seems In your code you are missing this Part----------------------
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
ImageAdapter >
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array - I have hard coded this
public Integer[] mThumbIds = {
R.drawable.casper, R.drawable.casper,
R.drawable.monkey, R.drawable.monkey,
R.drawable.cub, R.drawable.cub,
R.drawable.mouse, R.drawable.mouse,
R.drawable.casper, R.drawable.casper,
R.drawable.monkey, R.drawable.monkey,
R.drawable.cub, R.drawable.cub,
R.drawable.mouse
};
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); /// IF YOU WANT TO CHANGE IMAGE SIZE CHANGE HERE
return imageView;
}
}
As a separate project this will work.In case if you cant fix your issue plug and play this.Just posting as a help!
Output >
You can try using OnItemClickListener on your grid view like below
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Get your item here
adapterView.getItemAtPosition(i);
//Do your code here
}
});
Please make sure that you removed your click listener from getView otherwise it will make conflict.
Please make sure you have to intent single imageview class
Send position id to imageview class I used below code for full screen image view
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(mContext, Singleimageview.class);
i.putExtra("pos", (Integer) itemView.getTag() + "");
mContext.startActivity(i);
overridePendingTransition(R.anim.pull_in_left, R.anim.pull_out_right);
}
});

Android viewPager image slide right to left

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>
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 :)
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
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>
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".
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" />

how to display the image with name in android

Hi In My application I am displaying images by using swipe.Now, i want to display the under the image i want to display the name.
I want to write the some name in that image.how to write the code
Can anyone please help me.
Photos class updated code
public class Photos extends Activity {
Button button1;
String mImages;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
String[] description_images = getIntent().getExtras().getStringArray("{image1,image2,image3,image4}"); //ARRAY OF ALL TEXT ASSOCIATED TO IMAGES
String[] mImages = getIntent().getExtras().getStringArray("{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d}");
// String[] mText={"Text1","Text2","Text3"};
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public View instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.image_view, null);
final ImageView imageView = (ImageView)view.findViewById(R.id.iv_image);
final TextView textView = (TextView )view.findViewById(R.id.iv_text);
textView.setText(description_images[position]);
// imageView.setImageResource(mImages[position]);
container.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000" >
</android.support.v4.view.ViewPager>
description_images = getIntent().getExtras().getStringArray("images_name"); //ARRAY OF ALL TEXT ASSOCIATED TO IMAGES
mImages = getIntent().getExtras().getStringArray("images_path"); //ARRAY OF ALL IMAGE PATHS
private class ImagePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.image_view, null);
final ImageView imageView = (ImageView)view.findViewById(R.id.iv_image);
final TextView textView = (TextView )view.findViewById(R.id.iv_text);
textView.setText(description_images[position]);
imageLoader.displayImage(mImages[position], imageView, options);
container.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
Corresponding XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/iv_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" />
</RelativeLayout>

How to do Gallery Images swap Left To right bydefault

I have Image Gallery . In start, I have to swap right to left to see more Images . I want to Images Gallery that start left to right swamping by default
public View getView(int index, View view, ViewGroup viewGroup)
{
ImageView img = new ImageView(mContext);
ShowImgsActivity.pagNumber.setText(index+"");
img.setImageResource(ShowImgsActivity.typedImgs.getResourceId((ShowImgsActivity.typedImgs.length()-1)-index, -1));
img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
You can use ViewPager as an Image Gallery.It lets you swipe left and right to see previos and next images.
Here's is Java class,layout file and Adapter.
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);
}
}
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>
Adapter:
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);
}
}
You can ask if you have any further queries.View flipper is another option.
Happy coding :)
Reference :here

Categories

Resources