'How to enlarge images android studio
The gallery app. I made my adapter. I want to make it so that when you click on the image, it increases. I found a ready-made code, but it only shows a toast here. As I understand it, something needs to be added in the listener, but I don't know what. What do I need to add in the listener to make the picture bigger?
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<Cell> galleryList;
private Context context;
public ImageView img;
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Context context, ArrayList<Cell> galleryList) {
this.context = context;
this.galleryList = galleryList;
}
// Create new views (invoked by the layout manager)
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cell, parent, false);
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
setImageFromPath(galleryList.get(position).getPath(), viewHolder.img);
viewHolder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO something can happen if you click on the image
Toast.makeText(context, "" + galleryList.get(position), Toast.LENGTH_SHORT).show();
}
});
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public ImageView img;
public ViewHolder(View view) {
super(view);
img = view.findViewById(R.id.img);
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return galleryList.size();
}
private void setImageFromPath(String path, ImageView image) {
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = ImageHelper.decodeSampleBitmapFromPath(imgFile.getAbsolutePath(), 200, 200);
image.setImageBitmap(myBitmap);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
