I have a listview which has as image and a text.
Bases on conditions i have set two different images for the text views.
To set the images i have used
viewholder.btnFavItem.setImageResource(R.drawable.imagA);
Now on click i want toggle the images. if initially it was image a, i want it to becomes image b.
I have used the onclick listner and not the on item click becoz i need the position of image and the textview.I need the position becoz the textview can have either of the two images.the one that has image a,i want to change that to image b. i am clicking on the image and not the tetxview. i have used a custom adapter which extends base adapter.
in the onclick i am again using
viewholder.btnFavItem.setImageResource(R.drawable.imagB);
In the xml i have used clickable as true and focusable as false.
how do i toggle these images.
What you can actually do is use a tag to your image via setTag(), and compare it with your getTag():
String tag = viewholder.btnFavItem.getTag();if(tag.equals("imagA")){ viewholder.btnFavItem.setImageResource(R.drawable.imagB); viewholder.btnFavItem.setTag("imagB");}else if(tag.equals("imagB")){ viewholder.btnFavItem.setImageResource(R.drawable.imagA); viewholder.btnFavItem.setTag("imagA");}
Related
I am building a memory game with RecyclerView grid layout. When user clicks 2 time and didn't find the matched pictures, I want to change image view resource only in the clicked positions.
I built the logic sucessfully, but I don't know how to change imageview's resource in only certain positions.
You might have used the OnClickListener interface for card items click events.
So on click event, get the position of that clicked image tile using the getLayoutPosition() method and change its Drawable resource programmaticaLly through captured tile position.
I solved my problem with recyclerView.layoutManager?.findViewByPosition(position)
I have an app in which I have a listview with custom text and image. what I want based on data model I want to increase the number of image view in a listview.How do I do that with a listview.
Add a list view say "x"
the layout which defines each item of the listview (in your case text and image) , in place of image add another listview say "y" (can be horizontal/vertical)
now have multiple images mapped to y using baseAdaptor.
I am creating a application which contains a listView on lefthand side that display different images now i want to show that particular image in the center of the screen when user clicks on any image in listView.
You can add a custom adapter for your listview and add an onitemselectedlistener where inside it you can get the image you put in the view, and set the imageview.
You should keep the list of path of images and when user taps you will get the postion from OnItemClickListener. Using the position you can load the image from path.
The link has good content on creating custom adapters , You should use that then just map your path and positons. try that and post when you are stuck
The question might be duplicate ..sorry for that.
I have an activity (test activity) and array adapter (test array) for same.
The list contains image and text. but the image is not displayed always. It has some condition. So look is like
---------------------------------------
image1 text1
test2
test3
image4 test4
---------------------------------------
So test activity display the above list on screen.
Now i want to do different screen load on image (image activity) and text (text activity).
As the on-click handler will be in separate file (test array) i am not sure how to return to test activity and to specific task.
actually i have a list view with image and text.
Below are some scenario
Image will not be present on each row.
If image is present then Onclick of image shown other screen (for example image activity).
if user click other than image that is text show text activity screen).
What i have done is i have a list click handler in a separate file that is ArrayAdapter so i cant't do image click listener there.
So the issue was how to identify which row image is clicked?
Solution:
After lots of R&D and code changes i done below and its working:
I have added android:onClick="ImageClickHandler" for image click in xml.
and during row creation i have set row position as Id.
So when clicked on image you can get position by view.getId(), so u have the position when u click on image and hence you can get complete data on that row.
Might be there is another way....but this is what work for me.
In the native Gallery app on Android, when Select item via Option Menu action, you can select multiple items, each grid item which will have a colored-border overlay on top signify as selected when clicked.
Question:
How to do this dynamically using getView() in the ImageAdapter
class? (since there is setBackgroundColor() but not setBorder()
Is there a better way to accomplish this? (such as creating a padding
of some sort for the image within the gridview cell, then set the
background color which will look like a border)
I finally decided to use the 2nd method in the original post: creating a padding for ImageView within the gridview cell and set the background color.
I used ImageView.ScaleType.CENTER_CROP with setCropToPadding(true); for the ImageView.