Show a full screen slideshow image gallery Android

Home / Uncategorized / Show a full screen slideshow image gallery Android

Question:
I have implemented an Android image gallery to load all the images from the Android gallery into my Android application. All the images loads fine into the application. Now I want to show a full screen slideshow of all the images that I have loaded. I am able to show a full screen slideshow, but the only problem is it is not loading the image in the current position. For example, consider I have 3 folders in my Gallery, so for the first folder, it always loads the first image, for the second folder, it always loads the second image and so on. I figured out that I am passing the position of the gridView(Folder), and I am unable to figure out how to fix this.

Kindly help me regarding the same. Please find attached the code below of what I have tried.

GalleryFragment.java
gv_folder = (GridView)view.findViewById(R.id.gv_folder);
gv_folder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
Intent intent = new Intent(getContext(), PhotosActivity.class);
intent.putExtra("value",i);
startActivity(intent);
}
});

PhotosActivity.java
gridView = (GridView)findViewById(R.id.gv_folder);
int_position = getIntent().getIntExtra("value", 0);
adapter = new GridViewAdapter(this, GalleryFragment.al_images,int_position);
gridView.setAdapter(adapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView parent, View view, final int position, long id) {

CharSequence options[] = new CharSequence[]{"Add a Memory", "Listen Short Memory?", "Listen Long Memory?", "Fullscreen Slideshow"};

// if(position == 0) {
//options = new CharSequence[]{"Add a Memory", "Listen Short Memory?", "Listen Long Memory?", "Fullscreen Slideshow"};
// }else{
// options = new CharSequence[]{"Add a Memory", "Listen

Short Memory?", "Listen Long Memory?"};

// }

AlertDialog.Builder builder = new AlertDialog.Builder(PhotosActivity.this);
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
else if(which == 3){
Intent intent = new Intent(PhotosActivity.this, FullScreenViewActivity.class);
intent.putExtra("position", int_position);
startActivity(intent);
}
}
});
builder.show();
}
});

GridViewAdapter.java
package com.project.group.projectga.adapters;
public class GridViewAdapter extends ArrayAdapter {

Context context;
ViewHolder viewHolder;
ArrayList al_menu = new ArrayList<>();
int int_position;

public GridViewAdapter(Context context, ArrayList al_menu,int int_position) {
super(context, R.layout.adapter_photosfolder, al_menu);
this.al_menu = al_menu;
this.context = context;
this.int_position = int_position;

}

@Override
public int getCount() {

Log.e("ADAPTER LIST SIZE", al_menu.get(int_position).getAl_imagepath().size() + "");
return al_menu.get(int_position).getAl_imagepath().size();
}

@Override
public int getItemViewType(int position) {
return position;
}

@Override
public int getViewTypeCount() {
if (al_menu.get(int_position).getAl_imagepath().size() > 0) {
return al_menu.get(int_position).getAl_imagepath().size();
} else {
return 1;
}
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {

viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_photosfolder, parent, false);
viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);

convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.tv_foldern.setVisibility(View.GONE);
viewHolder.tv_foldersize.setVisibility(View.GONE);

Glide.with(context).load("file://" + al_menu.get(int_position).getAl_imagepath().get(position))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(viewHolder.iv_image);

return convertView;
}

private static class ViewHolder {
TextView tv_foldern, tv_foldersize;
ImageView iv_image;
}
}

FullScreenViewActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);

viewPager = (ViewPager) findViewById(R.id.pager);
countLabel = (TextView) findViewById(R.id.countLabel);

Intent i = getIntent();
selectedPostion = i.getIntExtra("position", 0);

adapter = new FullScreenImageAdapter(this, GalleryFragment.al_images.get(selectedPostion).getAl_imagepath());

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(pageChangeListener);

// displaying selected image first
//viewPager.
setCurrentItem(selectedPostion);
}

private void setCurrentItem(int position) {
viewPager.setCurrentItem(position, false);
displayExtras(position);
}

// page change listener
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
displayExtras(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
};

private void displayExtras(int position) {
countLabel.setText((position + 1) + " of " + GalleryFragment.al_images.get(selectedPostion).getAl_imagepath().size());

}

FullScreenImageAdapter.java
public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList 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);

}

Just posting the necessary code snippets. In case, you guys need anything else, just let me know so that I can update the question. As you can see, the position of the image for full screen is what I am missing. I am passing the wrong position value. And not able to figure out what i the position value to pass for FullScreenViewActivity. Kindly help.


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *