android - How do I get the OnClickListener working on a custom view with a RelativeLayout? -
question
hello,
i have built custom view relativelayout use custom image button. far, selecting works (pic2) , when click (using googletv remote), view changes it's state pic3 (thanks android:duplicateparentstate="true")
but unfortunately onclicklistener not fire (doesn't matter if click view remote "ok" button or use touchpad..)
i need same behavior normal button. how accomplish that? spent few hours on searching google , stackoverflow... (btw. when setting android:clickable="false" relativelayout, onclicklistener working, when use mouse pointer (touchpad) , afterwards focus lost , state (pic 3) not displayed)
pictures
pic1
pic2
pic3
code
rounded_button.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:focusable="true" android:focusableintouchmode="false"> <textview android:id="@+id/caption" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="30dp" android:background="@drawable/btn_rounded_corners" android:paddingleft="25dp" android:textsize="15sp" android:duplicateparentstate="true"/> <imageview android:id="@+id/icon" style="@style/menu_button" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginright="-50dp" android:layout_toleftof="@id/caption" android:background="@drawable/btn_main_menu_back_shape" tools:ignore="contentdescription" android:duplicateparentstate="true" />
roundedbutton.java
public class roundedbutton extends relativelayout { private string label; private int icon; /** * @param context */ public roundedbutton(context context) { super(context); initattributes(context, null); } /** * @param context * @param attrs */ public roundedbutton(context context, attributeset attrs) { super(context, attrs); initattributes(context, attrs); } /** * @param context * @param attrs * @param defstyle */ public roundedbutton(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); initattributes(context, attrs); } private void initattributes(context context, attributeset attrs) { layoutinflater.from(context).inflate(r.layout.rounded_button, this, true); typedarray = context.obtainstyledattributes(attrs, r.styleable.roundedbutton); final int n = a.getindexcount(); (int = 0; < n; ++i) { int attr = a.getindex(i); switch (attr) { case r.styleable.roundedbutton_text: setlabel(a.getstring(attr)); break; case r.styleable.roundedbutton_icon: seticon(a.getresourceid(attr, 0)); break; } } a.recycle(); } public string getlabel() { return this.label; } public void setlabel(final string label) { this.label = label; ((textview)findviewbyid(r.id.caption)).settext(this.label); } /** * @return icon */ public int geticon() { return icon; } /** * @param icon icon set */ public void seticon(int icon) { this.icon = icon; ((imageview)findviewbyid(r.id.icon)).setimageresource(this.icon); } }
relevant part of activity_main.xml
<eu.test.custom_views.roundedbutton android:id="@+id/custombutton" android:layout_width="wrap_content" android:layout_height="wrap_content" custom:icon="@drawable/hand_icon_green_left" custom:text="normal state" />
main activity
public class mainactivity extends activity implements onclicklistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ((roundedbutton) findviewbyid(r.id.custombutton)).setonclicklistener(this); } @override public void onclick(view arg0) { if(arg0.getid() == r.id.custombutton) { toast.maketext(this, "clicked", toast.length_short).show(); } } }
i got now.. solution simple, took time ;-)
solution
override dispatchkeyevent(keyevent event)
in roundedbutton.java , implement own onclicklistener. write public setonclicklistener
function...
private onclicklistener listener; @override public boolean dispatchtouchevent(motionevent event) { if(event.getaction() == motionevent.action_up) { if(listener != null) listener.onclick(this); } return super.dispatchtouchevent(event); } @override public boolean dispatchkeyevent(keyevent event) { if(event.getaction() == keyevent.action_up && (event.getkeycode() == keyevent.keycode_dpad_center || event.getkeycode() == keyevent.keycode_enter)) { if(listener != null) listener.onclick(this); } return super.dispatchkeyevent(event); } public void setonclicklistener(onclicklistener listener) { this.listener = listener; }
working roundedbutton.java
public class roundedbutton extends relativelayout { private onclicklistener listener; private string label; private int icon; @override public boolean dispatchtouchevent(motionevent event) { if(event.getaction() == motionevent.action_up) { if(listener != null) listener.onclick(this); } return super.dispatchtouchevent(event); } @override public boolean dispatchkeyevent(keyevent event) { if(event.getaction() == keyevent.action_up && (event.getkeycode() == keyevent.keycode_dpad_center || event.getkeycode() == keyevent.keycode_enter)) { if(listener != null) listener.onclick(this); } return super.dispatchkeyevent(event); } public void setonclicklistener(onclicklistener listener) { this.listener = listener; } /** * @param context */ public roundedbutton(context context) { super(context); initattributes(context, null); } /** * @param context * @param attrs */ public roundedbutton(context context, attributeset attrs) { super(context, attrs); initattributes(context, attrs); } /** * @param context * @param attrs * @param defstyle */ public roundedbutton(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); this.setclickable(true); this.setenabled(true); this.setfocusable(true); this.setfocusableintouchmode(true); initattributes(context, attrs); } private void initattributes(context context, attributeset attrs) { layoutinflater.from(context).inflate(r.layout.rounded_button, this, true); typedarray = context.obtainstyledattributes(attrs, r.styleable.roundedbutton); final int n = a.getindexcount(); (int = 0; < n; ++i) { int attr = a.getindex(i); switch (attr) { case r.styleable.roundedbutton_text: setlabel(a.getstring(attr)); break; case r.styleable.roundedbutton_icon: seticon(a.getresourceid(attr, 0)); break; } } a.recycle(); } public string getlabel() { return this.label; } public void setlabel(final string label) { this.label = label; ((textview)findviewbyid(r.id.caption)).settext(this.label); } /** * @return icon */ public int geticon() { return icon; } /** * @param icon icon set */ public void seticon(int icon) { this.icon = icon; ((imageview)findviewbyid(r.id.icon)).setimageresource(this.icon); }
}
Comments
Post a Comment