• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ontouch event handling

 
Jeet Dholakia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I have a scrollview in which there are about 60 buttons arranged in a linear layout, all of which have a similar function(changing text font) , now, if i had , say , 10 buttons , i would create a referance for each and set a touchlistener for each button as below,






Obviously this is okay for a small number, but there must be a way in which the parent view will handle the event and get the ViewId of the respective buttons touched like below




Is this possible or do i need to copy paste the same code over for 60 times?

Any help is appreciated,

Thanks,

Jeet
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are ways around this:

1) If your layout is made in XML, and you want to handle the button clicks in an Activity, then you can set the android:onClick attribute to the method in the Activity to call.

2) If you are doing it in code, you can loop through all the children of the layout and check their type, and if they are Button, then set the listener:

Or something like that.

3) Get rid of the scroll view, and the 60+ buttons. Replace them with a ListView, an ArrayAdapter, a Layout which shows just one row, and use the ListView's setOnItemClickListener() or setOnItemSelectedListener(). See the ListView API guide for some info on how to use it.

Personally, I use #3, it is more maintainable and uses fewer resources.
 
Jeet Dholakia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

thanks for the help, I used the first solution you mentioned, also I've used the list adapter before, but my layout consists of buttons with different appearances, hence a tedious task of customizing them , so i guess your solution 1 solves it all I can have my custom buttons defined in layout.xml plus only one click method to process that data...!

thanks again

Jeet
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeet Dholakia wrote:hi,

thanks for the help, I used the first solution you mentioned, also I've used the list adapter before, but my layout consists of buttons with different appearances, hence a tedious task of customizing them , so i guess your solution 1 solves it all I can have my custom buttons defined in layout.xml plus only one click method to process that data...!

thanks again

Jeet


No problem Note the limitation with #1 is that it only works if the container is an Activity. Google is really pushing to get us to use Fragments more, and then the method would not be called on the Fragment that holds the layout, but the activity which holds the fragment - a little confusing and a possible source of errors. So just be aware of that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic