Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to implement a custom View in my application. I went over this page: http://developer.android.com/training/custom-views/index.html

and I have created the 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 those 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 I try to add it to my main layout like 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 for some reason I receive this in the logcat:

E/AndroidRuntime( 1034): java.lang.RuntimeException: Unable to 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 the following change, in LeftSideMenu:

public LeftSideMenu(Context context, AttributeSet attrs) 
{
    super(context, attrs, R.layout.left_menu);
            .....

The error was gone, but I still can see my custom layout.

Hirarchy Viewer:

enter image description here What am I doing wrong? Any help would be appreciated. Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Add this constructor:

public LeftSideMenu (Context context, AttributeSet attrs) {...}
share|improve this answer
    
Thanks, I already did that. please see update question. but for some reason I still can't see my custom view. –  Emil Adz Apr 25 '13 at 11:34
    
try to run hierarchyviewer, what do you see? –  pskink Apr 25 '13 at 11:40
    
in the hierarchyviewer I see my view. see updated question. –  Emil Adz Apr 25 '13 at 11:50
    
and for your root view, what are the properties Layout>getWidth()/getHeight() ? –  pskink Apr 25 '13 at 12:05
    
I posted my main layout as well in it the width and height properties are set to match_parent. this custom view is a part of my main view. –  Emil Adz Apr 25 '13 at 12:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.