I have generated dynamically ImageView's in LinearLayout - let's say 6, with backgrounds and images, so they are looking like an icons.
Now I would like to select one and based on this which icon is selected proceed with other things.
I know that I can set onClickListener for dynamically create ImageView's.
but question is, how to select one? with jQUery I would add some class after tapping on an icon, in Android ? I really do know, tried something with setTag() but, well, did not happened.
Assuming that somehow I know which icon was tapped, then how can I loop through all dynamic generated ImageView's to get selected one?
Lets say in a loop you are creating the ImageView's and adding that to the LinearLayout. Assign a onClickListener to all the ImageViews. Just like the following code.
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView selectedImageView = (ImageView) v;
// selectedImageView is the imageView which you have selected
}
});
So when you tap on a imageView, its onClick function will be called. Parameter passed to the onClick function will be the imageView which you have selected. Just typeCast the View 'v' to ImageView and use in your application.
Related
I'm using a ListView to show some images and names in my application. I can make the ListView tiles clickable and start new activities as well . But now i want to do different actions for the different items that are on one listview tile. If user clicks on image, image opens and the textView will show some toast or something. The ImageView and TextView are on the same ListView Tile. How can i achieve this? Any Help?
Define OnClickListener events in your adapter for the button and textview as required and you are done. They will precede the click event for the list item by default.
if the contained views in your custom row layout does not have a click event it will fall back to the click event of the list view item.
You can implement click event in adapter, but you also implement it in activity.
You can find solution in :http://www.migapro.com/click-events-listview-gridview/
hope it can help you.
Some note: should not use view holder in gridview like above tuturial, it can make some bug, can implement direct like this:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
Another way to implement this elegantly is to add secondary views called 'accessories' to the item views associated with your ListView. This is explaned quite well in the link below by Cyril Mottier: A Google Developer Expert on the Android platform:
http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
I am working with android app.I need to access the contents from sqlite to a single xml page which including text and image. How can I add different images for different activities in a single ImageView and TextView? I am new to android development. So please help me and thanks.
You can use following source of code to change image in single image view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageView.setImageResource(R.drawable.ic_launcher);
}
});
There may be different buttons event, but imageView will be same. So just change drawables inside click event.
I am trying to change UI of button in custom adapter click, I am able to do functional thing on particular button click event. But when I tried to change UI of any Button, it reflects to last added Button.
I tried with setTag() option too.
use V.setBackgroundResource(R.drawable.drawableName);
it worked for me, where the drawable name is the background resource in your drawable folder.
you can set the background of button inside onClick event like this :
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundDrawable(drawable);
}
});
I found answer by myself.
When any Inflator and Holder used in Custom adapter, you need to create new Object for every single row (data), so you can access any item belongs to their family.
Im working on a school project and it's my first time on android development. I've searched the whole web but couldn't find any answers to my problem. I have a list of images and i would like to put them on one screen and the user would be able to navigate through these images without changing the whole screen. Also Im trying to put an AutoComplete search box to call any image from the list on the same screen. I tried to use viewFlipper method but did not work out. Is there any other way to do it?
You could add an onClick listener to the ImageView. Something like this:
ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
imgFavorite.setClickable(true);
imgFavorite.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Change the image here.
}
});
Source: Android ImageView's onClickListener does not work
I have an image button which says Select city I want to call an android spinner if anyone clicks on that image. Is it possible??
I would recommend you check out the App/Alert Dialogs section of the API Demos application. I think what you are looking for is a dialog with a single choice list.
Or take a look at this:
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
This is entirely possible in several ways. One way is to have a spinner whose visibility is GONE. Clicking on the image makes the spinners visibility VISIBLE.
Another choice is to create a dialog that has the spinner and use the selection to do whatever it is you want to do.
There are other options, but that should get you started.
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.performClick();
}
});