0

This is my source of testing Custom Layout (shows image and label).

XML CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/screen_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#3535ff"
    android:orientation="vertical"
    android:padding="1dp" >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:padding="20dp" >
        <ImageView
            android:id="@+id/screen_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:background="#000000"
            android:scaleType="centerInside"
            android:src="@drawable/cam_1_20130117_105601_118" />
        <TextView
            android:id="@+id/screen_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/screen_image"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="3dp"
            android:background="#95000000"
            android:gravity="center_vertical|center_horizontal"
            android:text="사출성형기 1호기"
            android:textColor="#ffffff" />
    </RelativeLayout>
</LinearLayout>

JAVA CODE

package com.example.testlayout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.testscreen.R;

public class CustomScreen extends LinearLayout {
    LinearLayout mLayout = null;
    TextView mLabel = null;
    ImageView mImage = null;

    public CustomScreen(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();     
    }

    public CustomScreen(Context context, AttributeSet attrs) {
        super(context, attrs);  
        init();     
    }

    public CustomScreen(Context context) {
        super(context);
        init();     
    }   

    void init() {
        LayoutInflater inflater = (LayoutInflater)getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.screen, this, true);
    //  mLabel = (TextView)findViewById(R.id.screen_label);
    //  mImage = (ImageView)findViewById(R.id.screen_image);

        if (isInEditMode()) {
            return;
        }

    }
}

With this code, I checked on Nexus One. It displays well. The problem is on editor mode. Exactly on xml editer with preview. When I add this view, error message appeared.

The following classes could not be instantiated : - com.example.testlayout.CustomScreen (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

I want to check it on edit mode. How can I check well like other views?

Temp
  • 109
  • 1
  • 11

1 Answers1

3

you have to add (!isInEditMode()) in each constructor. which tells if it not in edit Mode then initialized the stuff

 if(!isInEditMode())
      init(context);

and for initializing stuff follow this

also have a look at this

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300