Android, event listener? - android

I have a loop that runs through an array of image views, adding an event listener to each, how can I find out which image view was pressed inside the listener?
imageViewArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});

Doesn't the v parameter to the onClick method provide a reference to the ImageView?
EDIT
The thing is: You are not adding the same listener to all of the ImageViews in your code - every ImageView in your array gets its own listener.
In the listener's onClick method, the View that raised the event is passed in v, so when working with v you're working with the clicked ImageView.
To find the index of the ImageView in your array, you might as well set the ID as suggested by others and then use v.getId(), or you could loop over your array and check whether imageViewArray[i] == v, in which case i is the index of your ImageView within the array.

set id for each on the index value like eg: image_view.setId(i);

imageView.getId() will return you the ID you give this ImageView in findViewById()
method. Then you can check the ID's in a switch block. Hope this helps.

Related

I have 200 textviews and i want to know which one was pressed then how to change the text

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}

Detect which child is clicked on click of the parent

I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?
You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}
One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.
If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.

Getting index of element in ArrayList On Click in android programming?

I have an ArrayList of ImageButtons in my LinearLayout in android,
when it clicked my own "OnClick" event handler method run.
I want to get an index of an element(ImageButton) in ArrayList from its own OnClick event.
However, I cannot make a solution so far.
Otherwise I should create a custom ImageButton which has an index as integer,
which is more complicated.
Is there any good solution for it?
Thanks in advance.
Please take a look at this. It may help you.
//This is your arraylist of buttons
ArrayList<ImageButton> buttons ;
...
//This is your click listener
onClick(View view){
ImageButton clickedButton = (ImageButton)view;
int index = buttons.indexOfObject(clickedButton);
if(index!=-1){
//Your imagebutton clicked
}
}

How to set onclick function to dynamic textview in android?

I set the onClick() function, but when I click the text it works two times that mean I have two dynamic text view. How to resolve it?
My code:
TextView tView[] = new TextView [Array.length];
for(int i =1; i<Array.length; i++)
{
tview[i] = new TextView(this);
tview[i].setId(i);
tview[i].setText(Array[i]);
tview[i].setOnTouchListener(new OnTouchListener()
{
Public boolean onTouch(View v ,MotionEvent event)
{
Toast.makeText(getApplicationcontext,"MapVal",Toast.LengthShort).show();
}
});
}
Problem is in using OnTouchListener. Event onTouch() is calling not one time on every tap action, but minimum two: on touch down and on touch up. Use OnClickListener and setOnClickListener() instead.
if u want to set an onclick listener then use
urtextvw_name.setOnClicklistener()

Hightlight the ListView row when a contained UI element of row is touched

I want to enable the orange highlight that occurs when touching a listView row.
If the onclick event was generated from the row itself, I can adjust the hightlight by setting the style of the listview. However in my case, the clicking event is generated by the TextView underneath with onclick event attached to it.
When touch event occurs, Listview isn't aware of clicking is happening. It doesn't receive focused or pressed event.
Is there bubbling or capturing technique I could use like Flex did? Or any suggestion/solution?
Thank you very much.
View tempView = null; // Class Variable to temporary store Clicked Row's view
Method:
public void onItemClick(AdapterView parent, View v, int position,long id)
{
listView.setFocusable(true);
listView.setSelected(true);
v.setBackgroundColor(Color.LTGRAY); // CHANGE COLOR OF SELECTED ROW HERE>
selectedId = (int)id;
if(tempView != null){
//If row is already clicked then reset its color to default row color
tempView.setBackgroundColor(Color.TRANSPARENT);
}
tempView = v;
}
Hope this helps. Thanks :)

Categories

Resources