Error in implementing a custom view in android: -
i'm trying implement custom view in application. went on page: http://developer.android.com/training/custom-views/index.html
and have created following:
xml:
<?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" android:visibility="gone" > <imagebutton android:layout_width="fill_parent" android:layout_height="52dp" android:background="@drawable/left_menu_close_button" android:onclick="showleftmenu" /> <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" android:onclick="showselecttab"> <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" android:onclick="showselecttab"/> <textview android:id="@+id/left_menu_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="data" android:textcolor="@color/my_white" android:onclick="showselecttab"/> </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" 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" android:onclick="showsettingspopup"> <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/transparent_rectangle" android:scaletype="fitxy" android:src="@drawable/settings" android:onclick="showsettingspopup"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="settings" android:textcolor="@color/my_white" android:onclick="showsettingspopup"> </textview> </linearlayout> <imagebutton android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/left_menu_separator" /> </linearlayout> </scrollview>
and classes:
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); }
}
and: leftsidemenu:
package com.emildesign.sgreportmanager.ui; import com.emildesign.sgreportmanager.r; import android.content.context; import android.util.attributeset; import android.view.view; public class leftsidemenu extends appviewlinearlayout { private leftsidemenuclicklistener mlistener; public leftsidemenu(context context, attributeset attrs, int layoutresource) { super(context, attrs, layoutresource); findviewbyid(r.id.left_menu_data).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.dataonclick(v); } }); findviewbyid(r.id.left_menu_settings).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) mlistener.settingsonclick(v); } }); } public interface leftsidemenuclicklistener { void dataonclick(view v); void settingsonclick(view v); }
}
now try add main layout this:
<framelayout 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" tools:context=".loginscractivity" > <com.emildesign.sgreportmanager.ui.leftsidemenu android:id="@+id/leftsidemenu" android:layout_width="wrap_content" android:layout_height="wrap_content"/> ....
and reason receive in logcat:
e/androidruntime( 1034): java.lang.runtimeexception: unable start activity co mponentinfo{com.emildesign.sgreportmanager/com.emildesign.sgreportmanager.activi ties.reportstableactivity}: android.view.inflateexception: binary xml file line #10: error inflating class com.emildesign.sgreportmanager.ui.leftsidemenu
update:
i made following change, in leftsidemenu
:
public leftsidemenu(context context, attributeset attrs) { super(context, attrs, r.layout.left_menu); .....
the error gone, still can see custom layout.
hirarchy viewer:
doing wrong? appreciated. thanks.
add constructor:
public leftsidemenu (context context, attributeset attrs) {...}