android - How to set an onClickListener on an included layout? -
i have layout imagebutton included in several other layouts.
imagebutton layout:
call_cancelled.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:id="@+id/callendlayout" android:layout_width="70dp" android:layout_height="70dp" android:height="70dip" android:orientation="horizontal" android:paddingbottom="5dp" android:paddingtop="5dp" > <imagebutton android:id="@+id/phoneend" android:layout_width="63dp" android:layout_height="63dp" android:paddingbottom="7dp" android:background="@drawable/phone_cancelled" /> </linearlayout>
my include:
<include android:id="@+id/includecallend0" android:layout_width="70dp" android:layout_height="70dp" android:layout_alignbottom="@+id/include" android:layout_alignparentright="true" layout="@layout/call_cancelled" />
for includes need onclicklistener when it's pressed. i tried this:
view endcall0 = (view) findviewbyid(r.id.includecallend0); endcall0.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { intent callintent = new intent(intent.action_call_button); startactivity(callintent); int id = viewflipper.getdisplayedchild(); if (id == 0) { hidesoftkeyboard(); } } });
but doesn't work. know solution?
replace findviewbyid(r.id.includecallend0)
findviewbyid(r.id.includecallend0).findviewbyid(r.id.phoneend)
, should work
because want set click listener on imagebutton , not whole layout
use following function set onclicklistener once:
private static void applylistener(view child, onclicklistener listener) { if (child == null) return; if (child instanceof viewgroup) { applylistener((viewgroup) child, listener); } else if (child != null) { if(child.getid() == r.id.phoneend) { child.setonclicklistener(listener); } } } private static void applylistener(viewgroup parent, onclicklistener listener) { (int = 0; < parent.getchildcount(); i++) { view child = parent.getchildat(i); if (child instanceof viewgroup) { applylistener((viewgroup) child, listener); } else { applylistener(child, listener); } } }
use applylistener(rootview, youronclicklistener);
Comments
Post a Comment