Android: making gallery infinite loop of images - android

I'm using a gallery in my project in which I have added four images and I want it to be infinite from both right side and left side. How do I accomplish this?

The main idea is that in your getView method, you have to use
position = position % imagesArray.length;
if (position < 0)
position = position + imagesArray.length;
imagesArray is the array that holds the images in your res folder. For example:
public class CircularGallery extends Activity {
/** Called when the activity is first created. */
private Integer[] imagesArray = { R.drawable.picture1, R.drawable.picture2, R.drawable.picture3, R.drawable.picture4, R.drawable.picture5, R.drawable.picture6 , R.drawable.picture7 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
Toast.makeText(CircularGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
if (position >= imagesArraylength) {
position = position % mImageIds.length;
}
return position;
}
public long getItemId(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
i.setImageResource(imagesArray[position]);
i.setLayoutParams(new Gallery.LayoutParams(80, 80));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int checkPosition(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
}}
Also, some developers have done such a functionality and you can find sources on their blogs
http://abhinavasblog.blogspot.com/2011/09/android-infinite-looping-gallery.html
http://blog.blundellapps.com/infinite-scrolling-gallery/

if you want to set image showing on right side, just set g.setSelection(image)

My first guess is to change adapter data, i.e. if you detect that you are on the "right edge", then get your first image and add it to the end, then take second image and so on...

Related

Android GridView getItem Color

I have a custom GridView populated by array of colors.
Now when I click the item I want to get the color of cell.
I have this code, but when I click the item, get the java.lang.NullPointerException.
public class Colori_picker extends Activity {
private GridView grColori;
private ColorPickerAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_picker);
grColori= (GridView) findViewById(R.id.gridViewColors);
grColori.setAdapter(new ColorPickerAdapter(this));
grColori.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object color = mAdapter.getItem(position);
finish();
}
});
}
the adapter
public class ColorPickerAdapter extends BaseAdapter {
private Context context;
// list which holds the colors to be displayed
private List<Integer> colorList = new ArrayList<Integer>();
// width of grid column
int colorGridColumnWidth;
public ColorPickerAdapter(Context context) {
this.context = context;
String colors[][] = {
{ "83334C", "B65775", "E07798", "F7A7C0", "FBC8D9", "FCDEE8" },
{ "000000", "434343", "666666", "999999", "CCCCCC", "EFEFEF" } };
colorList = new ArrayList<Integer>();
// add the color array to the list
for (int i = 0; i < colors.length; i++) {
for (int j = 0; j < colors[i].length; j++) {
colorList.add(Color.parseColor("#" + colors[i][j]));
}
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
// set the width of each color square
imageView.setLayoutParams(new GridView.LayoutParams(colorGridColumnWidth, colorGridColumnWidth));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundColor(colorList.get(position));
imageView.setId(position);
return imageView;
}
public int getCount() {
return colorList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
What is happening is that you are declaring the instance variable mAdapter but never instantiating it. All you need to do is change this
grColori.setAdapter(new ColorPickerAdapter(this));
To this
mAdapter = new ColorPickerAdapter(this);
grColori.setAdapter(mAdapter);

Need Help Using a 2-Dimensional Array

So I made this custom 2d array adapter(basically copied it from someone else, so I don't completely know what I am doing with it), because I am making a checkers board game, and I wanted the board to be a 2d array of imageviews. I'm just wondering: How do I actually access each individual item in this 2d array in my main activity. I know in the OnClick it gives me both the view that is selected and the position, but I was just wondering if there is any way that I can do something like array[row][column].setBackgroundResource(R.drawable.piecered2) in my main activity.
public class GridViewAdapter extends BaseAdapter {
private Context context;
ImageView[][] gridContent;
int rowPosition, columnPosition, count;
public GridViewAdapter(Context c, ImageView[][] content){
context = c;
count = 0;
gridContent = new ImageView[content.length][content[0].length];
for(int i = 0; i<gridContent.length; i++){
for(int j = 0; j<gridContent[i].length; j++){
gridContent[i][j] = content[i][j];
count++;
}
}
rowPosition = 0;
columnPosition = 0;
}
public int getCount() {
return count;
}
public int getRowCount(){
return gridContent.length;
}
public int getColumnCount(){
return gridContent[0].length;
}
public Object getItem(int rowNum, int columnNum) {
return gridContent[rowNum][columnNum];
}
public View getView(int position, View view, ViewGroup group) {
ImageView imageView;
if(view == null){
imageView = new ImageView(context);
}
else{
imageView = (ImageView) view;
}
columnPosition = position % gridContent[0].length;
rowPosition = (position - columnPosition)/gridContent[0].length;
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//Just Setting Up the Initial State of the Board Here
if(columnPosition%2 != 0 && rowPosition == 6){
setRed(imageView);
} else if(columnPosition%2 == 0 && (rowPosition ==5 || rowPosition==7)){
setRed(imageView);
}
else if(columnPosition%2 != 0 && (rowPosition == 0||rowPosition==2)){
setBlack(imageView);
} else if(columnPosition%2 == 0 && rowPosition == 1){
setBlack(imageView);
}
return imageView;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position) {
return 0;
}
}
Didn't you just post the solution yourself:
ImageView[][] gridContent;
// fill the array
// ...
gridContent[row][column].setImageResource(yourresource);
will set the image-resource for the ImageView, assuming "gridContent" is a 2D array of ImageViews.
Just make sure that the array is actually filled with ImageViews that are already initialized with "= new ImageView(this)". Otherwise you will run into a NullPointerException.
Furthermore, I would suggest that you change your getItem() method to the following:
/** use ImageView as the return type instead of Object */
public ImageView getItem(int rowNum, int columnNum) {
return gridContent[rowNum][columnNum];
}
This will enable you to do the following in your Activity:
GridViewAdapter adapter = new GridViewAdapter(this); // make sure the adapter contains a filled ImageView array
adapter.getItem(row, column).setImageResource(yourresource);

android - get resource id from gallery

So i have a program with a Gallery (made with pictures from the resources folder), a Button and an ImageView
I want to choose one of the images from the gallery, press the button, and change the ImageView bitmap to display the one selected from the gallery...
I have the changing methods from activity to activity all fixed... but i'm just having trouble getting the id of the image picked, the integer, drawable, long, string... or whatever it helps...
Here's the code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//CODE TO KNOW IF PICKED THE 1ST OR 2ND IMAGE
//Toast.makeText(Galeria.this, v.getId() + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, parent.getItemIdAtPosition(position) + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, id + "", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageId = {
R.drawable.image1, //first image
R.drawable.image2, //second image
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount()
return mImageId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageId[position]);
i.setLayoutParams(new Gallery.LayoutParams(400, 225));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
The three Toast there on the OnItemClick void are my attempts of showing the id... but only showing the position of the image from the gallery.
Is there any other solution? I've read something about the dynamics ids... but could understand well...
Where should I put the code to get my desired id??
Any Help? Thanks.
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//CODE TO KNOW IF PICKED THE 1ST OR 2ND IMAGE
int imageId = (Integer) (parent.getItemAtPosition(position));
myImageView.setImageResource(imageId);
//Toast.makeText(Galeria.this, v.getId() + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, parent.getItemIdAtPosition(position) + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, id + "", Toast.LENGTH_SHORT).show();
}
});
In your adapter getItem should return the object of the dataset
public Object getItem(int position) {
return (mImageId[position]);
}
You can do something like this to get the image view resource as drawable on your itemClickListener :
(ImageView)view.getBackground()
This will return image as drawable of selected image view's resource.

Creating Image Gallery using Drawable images in res folder

I am new to android. I have a requirement now. I need to add around 10 images in the res/drawable folder and on running the app i should display this images on a listView.and on selecting any of the image i should display this image in the new activity should be able to zoom in and zoom out. Please help me out to figure out this with the sample code.
Thanks in advance.
this example for create Gallery, select one and set to selectedImageView.
so after that you can do everything with selectedImageView.
public class MyActivity extends Activity{
private int selectedImagePosition = 0;
private ImageView selectedImageView;
private List<Drawable> drawables;
private Gallery gallery;
#Override
public void onCreate(Bundle savedInstanceState) {
selectedImageView = (ImageView) view.findViewById(R.id.selected_imageview);
getDrawablesList();
gallery = (Gallery) view.findViewById(R.id.Gallery);
gallery.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
setSelectedImage(selectedImagePosition);
}
});
private void getDrawablesList() {
drawables = new ArrayList<Drawable>();
drawables.add(getResources().getDrawable(R.drawable.res1));
drawables.add(getResources().getDrawable(R.drawable.res2));
drawables.add(getResources().getDrawable(R.drawable.res3));
drawables.add(getResources().getDrawable(R.drawable.res4));
}
private void setSelectedImage(int selectedImagePosition) {
BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
selectedImageView.setImageBitmap(b);
selectedImageView.setScaleType(ScaleType.FIT_XY);
}
You can use a GalleryView
Refer to the link below for more help:
http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views
private ImageView selectedImageView;
private TextView _nameTextView;
private Gallery gallery;
Integer[] imageIDs = { R.drawable.hbath, R.drawable.hfood,
R.drawable.hmedicine, R.drawable.htherapy, R.drawable.htoilet,
R.drawable.hother };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedImageView = (ImageView) findViewById(R.id.imageSwitcher1);
_nameTextView = (TextView) findViewById(R.id.NameTextView);
gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
if (position == 0) {
selectedImageView
.setImageResource(R.drawable.hbathbackground);
_nameTextView.setText("Toilet");
} else if (position == 1) {
selectedImageView
.setImageResource(R.drawable.hfoodbackground);
_nameTextView.setText("Food");
} else if (position == 2) {
selectedImageView
.setImageResource(R.drawable.hmedicinebackground);
_nameTextView.setText("Medicine");
} else if (position == 3) {
selectedImageView
.setImageResource(R.drawable.htherapybackground);
_nameTextView.setText("Therapy");
} else if (position == 4) {
selectedImageView
.setImageResource(R.drawable.htoiletbackground);
_nameTextView.setText("Bath");
} else if (position == 5) {
selectedImageView
.setImageResource(R.drawable.hotherbackground);
_nameTextView.setText("Other");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
and ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
// ---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
// ---returns the number of images---
public int getCount() {
return imageIDs.length;
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setBackgroundColor(0xFF000000);
// imgView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(155, 235));
imageView.setBackgroundResource(R.drawable.customborder2);
return imageView;
}
}

Android Gallery View Update Images

i have a question about using GalleryView.
At first, i set five "default images" to show from drawable directory.
But after, i want to run an Async Task in which i download the images, save them and show them in the gallery.
For that i created the following Adapter:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private ArrayList<Integer> mImageIds = new ArrayList<Integer>();
private ArrayList<Drawable> mImageDrawables = new ArrayList<Drawable>();
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public void setPlaces(int count) {
for (int i = 0; i < count; i++) {
mImageIds.add(R.drawable.tournoimg);
mImageDrawables.add(null);
}
}
public void setDrawable(String resource, int position) {
Drawable image = Drawable.createFromPath(resource);
mImageDrawables.add(position, image);
}
public int getCount() {
return mImageIds.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (mImageDrawables.get(position) == null)
i.setImageResource(mImageIds.get(position));
else
i.setImageDrawable(mImageDrawables.get(position));
i.setLayoutParams(new Gallery.LayoutParams(60, 78));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
and the following Async Task
private class FillImages extends AsyncTask<ArrayList<Place>, Void, Void> {
protected Void doInBackground(ArrayList<Place>... listplaces) {
ArrayList<Place> places = listplaces[0];
Iterator<Place> it = places.iterator();
int position = 0;
while (it.hasNext()) {
Place p = it.next();
saveImage(p.getImage(), p.getURLImage());
// Gallery g = (Gallery) findViewById(R.id.gallery);
mImageAdapter.setDrawable(p.getImage(), position);
position++;
mImageAdapter.notifyDataSetChanged();
}
return (null);
}
But when i run it i have this error:
Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Any idea?
Thanks
You should implement onPostExecute() and move any code that interacts with the UI (perhaps notifyDataSetChange()) to that method. Android requires that interaction with the GUI happen from the UI thread.
For that you have to put this code into onPostExecute() method of Async task :
ArrayList<Place> places = listplaces[0];
Iterator<Place> it = places.iterator();
int position = 0;
while (it.hasNext()) {
Place p = it.next();
saveImage(p.getImage(), p.getURLImage());
// Gallery g = (Gallery) findViewById(R.id.gallery);
mImageAdapter.setDrawable(p.getImage(), position);
position++;
mImageAdapter.notifyDataSetChanged();
}

Categories

Resources