Android compound component and onclick -
i have 2 textviews , imageview in compound component (extending linearlayout).
i entire component clickable, not individual contained views.
the onclick listener set on compound component not called, though there visual feedback indicating component gets touch events.
any ideas ?
update: code compound component:
public class homebutton extends linearlayout { textview title; textview subtitle; imageview icon; public homebutton(context context, attributeset attrs) { super(context, attrs); typedarray = context.obtainstyledattributes(attrs, r.styleable.homebutton, 0, 0); string titletext = a.getstring(r.styleable.homebutton_title); string subtitletext = .getstring(r.styleable.homebutton_subtitle); int iconresid = a.getresourceid(r.styleable.homebutton_icon, 0); a.recycle(); layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.home_button, this, true); title = (textview) findviewbyid(r.id.home_button_title); title.settext(titletext); subtitle = (textview) findviewbyid(r.id.home_button_subtitle); subtitle.settext(subtitletext); icon = (imageview) findviewbyid(r.id.home_button_icon); icon.setimageresource(iconresid); } public void settitle(string title) { this.title.settext(title); } public void setsubtitle(string subtitle) { this.subtitle .setvisibility(subtitle == null ? view.gone : view.visible); this.subtitle.settext(subtitle); } } see own answer below (which post here don't have enough rep answer own questions ;))
solution, found myself
the issue due way xml layout compound component defined. root linearlayout.
when inflating layout in homebutton constructor, view hierarchy not start @ linearlayout defined in xml, has root node (from homebutton class guess), linearlayout defined in xml first child of root node.
setting onclick listener on homebutton fine not needed in case, onclick events consumed first child node...
possible solutions there on:
- remove root linearlayout , use merge tag
- override setonclicklistener forward first child
- call setclickable(false) on first child.
i've chosen go 3rd solution (but i've tested work) because using merge tag not allow set style way want.
public homebutton(context context, attributeset attrs) { super(context, attrs); typedarray = context.obtainstyledattributes(attrs, r.styleable.homebutton, 0, 0); string titletext = a.getstring(r.styleable.homebutton_title); string subtitletext = .getstring(r.styleable.homebutton_subtitle); int iconresid = a.getresourceid(r.styleable.homebutton_icon, 0); a.recycle(); layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.home_button, this, true); title = (textview) findviewbyid(r.id.home_button_title); title.settext(titletext); subtitle = (textview) findviewbyid(r.id.home_button_subtitle); subtitle.settext(subtitletext); icon = (imageview) findviewbyid(r.id.home_button_icon); icon.setimageresource(iconresid); // made work getchildat(0).setclickable(false); } one thing aware of solution 2: if onclick listener checks id of view generated event, not id defined in xml (because view generated event first child, not homebutton view), can cheat , assign id of homebutton view first child...
set layout clickable using attribute
android:clickable=true in xml, or
setclickable(true) to set programmatically.
you might want @ descendantfocusability determine how clicks on children work.
for example:
android:descendantfocusability="blocksdescendants" gives you: "the viewgroup block descendants receiving focus."
Comments
Post a Comment