android - onClicks in a custom view are not responding: -
i have created custom view in want implement onclick
actions. created following:
custom view xml layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/leftmenu" android:layout_width="100dp" android:layout_height="fill_parent" android:background="@drawable/left_menu_background" android:orientation="vertical" > <imagebutton android:id="@+id/left_menu_close_button" android:layout_width="fill_parent" android:layout_height="49dp" android:background="@drawable/left_menu_close_button" /> <imagebutton android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/left_menu_separator" /> <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent" > <linearlayout android:id="@+id/left_menu_buttons_container" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <linearlayout android:id="@+id/left_menu_data_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/left_menu_button_transparent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingbottom="10dp" android:paddingtop="10dp" > <imagebutton android:id="@+id/left_menu_data_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/transparent_rectangle" android:scaletype="fitxy" android:src="@drawable/folder" /> <textview android:id="@+id/left_menu_data_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="data" android:textcolor="@color/my_white" /> </linearlayout> <imagebutton android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/left_menu_separator" /> <linearlayout android:id="@+id/left_menu_settings_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/transparent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingbottom="10dp" android:paddingtop="10dp" > <imagebutton android:id="@+id/left_menu_settings_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/transparent_rectangle" android:scaletype="fitxy" android:src="@drawable/settings" /> <textview android:id="@+id/left_menu_settings_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="settings" android:textcolor="@color/my_white" > </textview> </linearlayout> <imagebutton android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/left_menu_separator" /> </linearlayout> </scrollview> </linearlayout>
appviewlinearlayout:
package com.emildesign.sgreportmanager.ui; import android.content.context; import android.util.attributeset; import android.view.layoutinflater; import android.view.view; import android.widget.linearlayout; public class appviewlinearlayout extends linearlayout { public appviewlinearlayout(context context, attributeset attrs, int layoutresource) { super(context, attrs); if (isineditmode()) return; view view = layoutinflater.from(context).inflate(layoutresource, null); addview(view); } }
leftsidemenu:
public class leftsidemenu extends appviewlinearlayout { private leftsidemenuclicklistener mlistener; public leftsidemenu(context context, attributeset attrs) { super(context, attrs, r.layout.left_menu); findviewbyid(r.id.left_menu_data_layout).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); findviewbyid(r.id.left_menu_data_image).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); findviewbyid(r.id.left_menu_data_text).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); findviewbyid(r.id.left_menu_settings_layout).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); findviewbyid(r.id.left_menu_settings_image).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); findviewbyid(r.id.left_menu_settings_text).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); findviewbyid(r.id.left_menu_close_button).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.closemenuonclick(v); } }); } public void setlistener(leftsidemenuclicklistener listener) { mlistener = listener; } public interface leftsidemenuclicklistener { void dataonclick(view v); void settingsonclick(view v); void closemenuonclick(view v); } }
in activity this:
public class reportstableactivity extends activity { leftsidemenu leftsidemenu; static final string tag = reportstableactivity.class.getsimplename(); private sgraportmanagerappobj application; private list<report> reportsrepository; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); leftsidemenu = (leftsidemenu) findviewbyid(r.id.leftsidemenu); setcontentview(r.layout.reports_list_activity_layout); application = (sgraportmanagerappobj)getapplication(); reportsrepository = application.reportsrepository.getreportsrepository(); createreportslist(); if (leftsidemenu != null) { leftsidemenu.setlistener(new leftsidemenu.leftsidemenuclicklistener() { @override public void settingsonclick(view v) { } @override public void dataonclick(view v) { } @override public void closemenuonclick(view v) { log.d(tag, "close left menu button clicked"); togglebutton button = (togglebutton) findviewbyid(r.id.showleftmenubutton); button.setchecked(false); leftsidemenu.setvisibility(view.gone); } }); } //rest of code...
the problem: reason closemenuonclick never called , log: "close left menu button clicked"
never presented.
my guess problem happens because make check: if (leftsidemenu != null)
, after set listener. reason because custom view viability = gone
when activity presented.
any advice on how right appreciated.
thanks.
change:
leftsidemenu = (leftsidemenu) findviewbyid(r.id.leftsidemenu); setcontentview(r.layout.reports_list_activity_layout);
to:
setcontentview(r.layout.reports_list_activity_layout); leftsidemenu = (leftsidemenu) findviewbyid(r.id.leftsidemenu);
also don't create 7 onclicklistener, make one:
onclicklistener mclicklistener = new onclicklistener() { @override public void onclick(view v) { if (mlistener == null) { return; } int id = v.getid(); if (id == r.id.left_menu_data_layout || id == r.id.left_menu_data_image || id == r.id.left_menu_data_text) { // skipped 3 more ids mlistener.settingsonclick(v); } else { mlistener.closemenuonclick(v); } } }
and register:
// repeat 7 times findviewbyid(r.id.xxx).setonclicklistener(mclicklistener);
or better let leftsidemenu implements onclicklistener , register:
// repeat 7 times findviewbyid(r.id.xxx).setonclicklistener(this);