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.
Related
here am trying to change the background resource for the image, am success in that, but the problem is, when i clicking on all the items in the gridview every item has changes in the background resource, my question is if i click on item which is 0 position it has to change the background resource image, and again if i click on the item which is in 1 position it has to change the background resource and as well as a image on 0 position has to set normal image like without background resource comes to normal state.
public void onItemClick(AdapterView<?> arg0, View vv, int arg2, long arg3) {
// TODO Auto-generated method stub
vv.setBackgroundResource(android.R.drawable.btn_default);
}
Provide me to set only one selected item has to change the image.
Thanks in advance.
You need modify your adapter to set background for checked items. For Example:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// creating view
if(item.isChecked()){
veiw.setBackgroundResource(android.R.drawable.btn_default);
}
return view;
}
You actually need to modify the Adapter and for that i will refer you to this question here
How to set an image as background image on a click?
Check the accepted answer and do it as described and you are good to go
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'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();.
I am an Android Developer.I have to add some more view in the list view on the the position on which user click.For example add two button on the item position on which user click. Is it possible to add views in list with onItemClicklistener I am using this code
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
LayoutInflater inflater=((Activity) getApplicationContext()).getLayoutInflater();
inflater.inflate(R.layout.row, null);
Button but=(Button)findViewById(R.id.button1);
but.setVisibility(0);
}
I have already added button in List view and set its visibility off.Can any body help me any help will be highly appreciable.
u can get the view on which the onItemClickListener was used, and then use view.post() to add new views to the item. I followed this to add extra views dynamically.
http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)
I have a listview. which is mase by custome control..
and custome control class extends baseadapter class.
one of my custome control is button..
so onclick of that particular button I have to find position of row..
pleas tell me howz it possible
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
});
in above code "onItemClick" method by using third parameter we can get the position of the row