Hi community stackoverflow,
Basically, I have a gallery displaying some images using gridView + imageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <GridView android:id="@+id/PhoneImageGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="12dp" android:horizontalSpacing="12dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:gravity="center" /> <ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:scaleType="centerCrop" />
I would like to use setOnLongClick for each image displayed by the adapter. This works well, however, when you click long on the imageView, I would like to display ContextMenu with some elements (i.e. you long click on imageView, the MenU context is displayed with some elements: Image information, send this image ...). Unfortunately, I cannot figure out how to inflate this menu in the adapter. (This is probably not the best way to do this)
I have the following lines in my main activity
_adapter = new ImageAdapter(activity,storedObjects.getAlbums()); imagegrid.setAdapter(_adapter);
My adapter (removed some unnecessary lines)
public class ImageAdapter extends BaseAdapter { private Albums albums; private Context context; private LayoutInflater inflater; public ImageAdapter(Context context, Albums albums) { this.albums = albums; this.context = context; inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); if(albums.getAlbumsListSize() == 0) { Toast.makeText(context, "There is no album to display", Toast.LENGTH_LONG).show(); } } public View getView(final int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.galleryitem, null); holder.imageview = (ImageView) view.findViewById(R.id.thumbImage); holder.checkbox = (CheckBox) view.findViewById(R.id.itemCheckBox); holder.textview = (TextView) view.findViewById(R.id.album_name); holder.checkbox.setChecked(true);
Questions:
- setOnLongClickListener works correctly when I click on an image, my log is displayed in logcat, however, how to create a menu for each image?
Apparently, I can override onCreateContextMenu in my main activity. I think I could pass each ImageView to onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo), but how?
I would be very grateful if you could help me with this.
Thank you very much
Floren Valdeliev