Ontouch event handling need to copy paste the same code over for 60 times

Ontouch event handling need to copy paste the same code over for 60 times

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,

 
Button b1=(Button) findViewById(R.id.b1);  
b1.setOnTouchListener(this);  
.  
.  
.  
Button b10=(Button) findViewById(R.id.b10);  
b10.setOnTouchListener(this); 

Button b1=(Button) findViewById(R.id.b1);
b1.setOnTouchListener(this);
.
.
.
Button b10=(Button) findViewById(R.id.b10);
b10.setOnTouchListener(this);
 


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


LinearLayout parent=(LinearLayout) findViewById(R.id.parent);  
parent.setOnTouchListener(this);  
 
onTouch(View v){  
 
//handle child events here  
 
 

LinearLayout parent=(LinearLayout) findViewById(R.id.parent);
parent.setOnTouchListener(this);

onTouch(View v){

//handle child events here


}


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

Any help is appreciated,
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:

ViewGroup container = (ViewGroup) findViewById(R.id.ViewGroupHoldingButtons);  
int childCount = container.getChildCount();  
for (int child = 0; child < childCount; child++)  
{  
    View childView = container.getChildAt(child);  
    if (childView instanceof Button)  
    {  
        ((Button)childView).setOnTouchListener(this);  
    }  

ViewGroup container = (ViewGroup) findViewById(R.id.ViewGroupHoldingButtons);
int childCount = container.getChildCount();
for (int child = 0; child < childCount; child++)
{
    View childView = container.getChildAt(child);
    if (childView instanceof Button)
    {
        ((Button)childView).setOnTouchListener(this);
    }
}
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.
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.

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright