Android overriding onClick(View v) - is there a good solution to use for all buttons in an application -


as know can override few methods each type of view have created. not doing xml layout code because lot of stuff changes form @ runtime , there things created dynamically programmatic solution best route me here.

so gist of issue lets have 50 buttons in android app. these buttons potentially on 1 activity more span out across multiple screens(activites).

i have created button class called custombutton overrides onclick(view view) method. if of buttons supposed action(lets part of linearlayout) , part of relative layout , in each relative layout want information relative layout button resides in(perhaps information textviews in same relative layout, etc etc).

one solution of course id of each button , switch(case) or , depending on id of button returned can something. problem is have 50 buttons. if had 200? should have 200 case checks in switch statement figure out action need take?

so trying figure out information available me not aware of use when override onclick.

   @override    public void onclick(view v) {     // todo auto-generated method stub     if(!getclickinfo()){         ((custombutton)v).settext("i clicked button");         setclickinfo(true);     }     else{         isclicked = false;         ((custombutton)v).settext("this button want click");       }  } 

so here example. have passed in view represents button clicked , can change text of button click on over , on again. if button part of relative layout? can other information other views part of same relative layout? if buttons wanted change background color of each time clicked instead of changing text? if have x number of buttons trying stay away creating x number of ids -- pointed out potentially have hundreds of them.

was wondering if might have ideas on better route here having potentially unlimited of case checks inside switch statement. if find out view button came from(in case button part of outer view) switch on views instead of buttons - , making switch statement less cumbersome , easier maintain. thanks.

i'm not sure end goal is. view (button) can parent (e.g. relativelayout). once have view group can children , whatever need to.

for example:

@override public void onclick(view v) {     viewparent parent = v.getparent();     if (parent instanceof relativelayout) {         // button inside relativelayout         relativelayout rl = (relativelayout) parent;         int numchildren = rl.getchildcount();         // loop through children if need (use rl.getchildat(index) each child)         ...     } } 

if don't need know if it's relativelayout or linearlayout, or whatever use viewgroup.

hope helps.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -