21
package com.binod.customviewtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class CustomView extends RelativeLayout{

    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

//      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutInflater mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.custom_view  , this, true);
    }

}

Including as

<RelativeLayout 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=".MainActivity" >

    <com.binod.customviewtest.CustomView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></com.binod.customviewtest.CustomView>

</RelativeLayout>

Custom View as

<RelativeLayout 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"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

Just started adding a new custom view and got the error once If I clear this then can move forward

I am getting crash "Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class"

Binod Singh
  • 682
  • 4
  • 11
  • 22

2 Answers2

44

You need to have 2 more constructors. To know why

Do I need all three constructors for an Android custom view?

public class CustomView extends RelativeLayout{

    LayoutInflater mInflater;
    public CustomView(Context context) {
        super(context);
         mInflater = LayoutInflater.from(context);
         init(); 

    }
    public CustomView(Context context, AttributeSet attrs, int defStyle)
    {
    super(context, attrs, defStyle);
    mInflater = LayoutInflater.from(context);
    init(); 
    }
    public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mInflater = LayoutInflater.from(context);
    init(); 
    }
   public void init()
   {
       View v = mInflater.inflate(R.layout.custom_view, this, true);
       TextView tv = (TextView) v.findViewById(R.id.textView1);
   tv.setText(" Custom RelativeLayout");
   }
}

I am posting an example. My packagename is different

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<com.example.testall.CustomView
        android:id="@+id/timer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

custom_view.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="My Custom View" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }
}

Snap

enter image description here

As pskink suggested there in a RelativeLayout in activity_main.xml with a child CustomView. Then CustomView extends RealtiveLayout and then again you inflate a customview with RelativeLayout and a child TextView. No need for all these. Just a CustomView. Have a TextView created programatically and then add textview to RelativeLayout

Edit:

activity_main.xml

<com.example.testall.CustomView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/timer1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

CustomView

public class CustomView extends RelativeLayout{

    TextView tv;
    public CustomView(Context context) {
        super(context);
         tv = new TextView(context);
         init(); 

    }
    public CustomView(Context context, AttributeSet attrs, int defStyle)
    {
    super(context, attrs, defStyle);
    tv = new TextView(context);
    init(); 
    }
    public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    tv = new TextView(context);
    init(); 
    }
   public void init()
   {
       this.addView(tv);
       tv.setText(" Custom RelativeLayout");
   }
}
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @Raghunandan and instead of one RelativeLayout you will end up with *three* RelativeLayouts – pskink Apr 01 '14 at 08:32
  • @pskink edited but i wanted to point out about constructors. Any way you are right about relativelayouts and thats unnecessary. – Raghunandan Apr 01 '14 at 08:37
  • @binod do check the edit. No need to 3 RelativeLayouts you can even align the textview and set properties to the same – Raghunandan Apr 01 '14 at 08:44
  • First snippet of code could be updated. Instead of calling super, call this() until the the final constructor is called. Inflate the view there. "v" is never created in your snippet, though it is obvious it is to be returned by the inflator which you are unnecessarily holding a reference to. Removes the need for "init" and holding references that are not needed. – BajaBob Jul 21 '15 at 01:17
  • 1
    @BajaBob yup you could do that. i missed that in the first and yes the second customview can also be improved – Raghunandan Jul 21 '15 at 04:55
0

Try to get Activity and use this

 {
    LayoutInflater inflter = activity.getLayoutInflater();
    View v = inflter.inflate(R.layout.custom_view,null);
    this.addView(v); or addView(v);
    }
Yuvaraja
  • 715
  • 6
  • 22