I've implemented a gridview of images that are picked from sdcard.Now i want every image to be shown in dialog on click.How to do it within Image Adapter?Any help will be appreciated.Thanks in advance.
You wouldn't do that in your adapter... Do it inside your click listener (from wherever you call and set the gridview adapter).
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageView iv = (ImageView) v.findViewById(R.id.imageviewid); // get the resource id
Drawable image = iv.getDrawable(); // get the image
CreateYourDialog(image); // pass the image to a method to create your dialog
}
});
If your GridView is only the imageview for each cell, then you can skip the findViewById line of code and simply use Drawable image = v.getDrawable();.
Related
I want to know how to get different item from selected item in gridview. I have gridview game of matching pictures. When i click on one image then that will flip and then when click on second image first will flip back. How to perform this animation? I want to get imageview of first selected image.
You can do that by saving the last view in a variable
View lastView = null;
// .....
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if(lastView != null) {
// do something with the last view
}
// the rest of your code
lastView = view;
}
});
Not tested but should work.
I am developing a game in which i used 4*3 grid size using gridview.i initially loaded images for each imageView in gridView.i want to change the images while clicking the previous image.
Set a click listener on the Grid view object. Suppose g is the grid view object, then
g.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}});
Now in the method onItemClick, you can handle the click on the images and change image accordigly.
for more follow the link onItemClick.
Hi all
I have a GridView of FrameLayout made of an image and textview. I want to get the corresponding image and the text when I click a cell of Grid. Mentioned that textview is top of image.
Does anybody have idea how to do it?
First register item click listener of grid view
e.g. gridview.setOnItemClickListener(gridItemClickListener);
Then
OnItemClickListener gridItemClickListener = new OnItemClickListner()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// here you will get your clicked cell of grid and you can find the image and text.
ImageView image = view.findViewById(R.id.grid_raw_image);
TextView textView = view.findViewById(R.id.grid_raw_text);
}
};
If you use the viewHolder technique as recommended by google, use:
gridView.setOnItemClickListener(new OnItemClickListner()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ViewHolder holder=(ViewHolder)view.getTag();
//use the views you hold in the viewHolder
...
}
});
Using the Hello, World Gridview tutorial example, I am attempting to make the image fullscreen on click instead of display the position of the image in the array. As I am unfamilar with Android and this is my first development attempt with it, I'm at a loss. I am familiar with Java though, and I've tried doing things like this (that don't work obviously):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
showImage(v, position);
Toast.makeText(HelloAndroid.this, "" + parent.getId(), Toast.LENGTH_SHORT).show();
}
});
}
private void showImage(View view, int position) {
ImageView imgView = (ImageView) view;
imgView.setImageResource(position);
}
But the app crashes (force close). Any ideas?
Here is the tutorial app I am using
imgView.setImageResource(position);
This gives you an error because you are not using it correctly. The parameter should be pointing to the resource ID of the image (which is an int) e.g. R.id.imageid. Even if you were to supply the right resource ID, what you want to do would not work.
To get the right resource ID, you would need to call the adapter of the view and use the getItemId(position) method to get the correct resource ID. in your ImageAdapter, if you change your getItemId() method to return mThumbsIds[position] rather than 0 (as in Google's example).
However, to achieve your result I would suggest for you to create a new Activity and just load the image like that.
For example:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
long imageId = (ImageAdapter)parent.getAdapter.getItemAt(position);
Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
fullScreenIntent.putExtra(thisClassName.class.getName(), imageId);
thisClassName.this.startActivity(fullScreenIntent);
}
});
Assuming you create a full_image.xml layout file, containing just an ImageView with an id of "fullImage", your FullScreenImage class should look like this:
public class FullScreenImage extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.full_image);
Intent intent = getIntent();
long imageId = intent.getExtras().get(thisClassName.class.getName());
ImageView imageView = (ImageView) v.findViewById(R.id.fullImage);
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
imageView.setImageResource(imageId);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
Something along these lines should work well for you, with some tweaking. Make sure you add FullScreenImage in your AndroidManifest.xml file as an <activity> as well.
Here, you're taking the ID of the view that was clicked (passed in the view argument) and using it to look up itself again:
ImageView imgView = (ImageView) findViewById(view.getId());
That makes no sense, especially since the clicked view apparently has no ID, causing findViewById to return null.
You probably want to pass the ID of your ImageView instead:
ImageView imgView = (ImageView) findViewById(R.id.whatever_the_id_of_the_image_view_is);
How to handle the positions in Gallery.I need to change the background of image at perticular position.Iam able to change the background image of selected image successfully within the onItemClick method.But i need to change the old selected background image also as non-selected one.
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
//previouse selected image
//here sometimes it is changing background of another image(which is not a old selected image)
if(mSelectedItemIndex!=-1 && mSelectedItemIndex!=position){
// Object v =mGallery.getItemAtPosition(mSelectedItemIndex);
// View v =imageAdapter.getDropDownView(mSelectedItemIndex,null,null);
// View v =mGallery.getChildAt(mSelectedItemIndex);
View v = (View)view.getChildAt(mSelectedItemIndex);
ImageView imgView = (ImageView) v.findViewById(R.id.adsimage);
// ImageView imgView = (ImageView)v;
// ImageView imgView = (ImageView)arg0.findViewById(R.id.adsimage);
imgView.setBackgroundResource(R.drawable.gallery_unselected_default);
//current selected image
//always this code is working fine.
ImageView imgView = (ImageView)view.findViewById(R.id.adsimage);
imgView.setBackgroundResource(R.drawable.gallery_selected_focused);
}
I used getItemBackground theme in base adapter but no use.Please give me how to persist image positions if gallery is scrolling form left to right and right to left.If i select one by one it is working fine.But when i select alternate image.It is not working.
Create a member variable eg View lastSelected in your activity to store the view when it has been selected/clicked. When a new item is selected/clicked use lastSelected to change the background and then update lastSelected with the newly selected view.
Hope that helps.