Android Studio layout editor cannot render custom views Android Studio layout editor cannot render custom views android android

Android Studio layout editor cannot render custom views


Custom view components are also supported and shown correctly in IDEA, But since IntelliJ IDEA uses class files from your output directory to render such components, you have to do build->make project on your project first.

enter image description here

reference


Facing the same issue, I had to override the three and four argument constructors:

public View(Context context, AttributeSet attrs, int defStyle) public View(Context context, AttributeSet attrs, int defStyle, int defStyleRes)

Then rebuild the project.


As I found out today, at last, the "Classdef not found" etc. errors during layout rendering are actually misleading. What they really mean is that there is some error during execution of your widget.

The simplest way to find out, where exactly the problem lays, is this:

  1. In your XML layout file replace you custom view class (let's call it "MyFrameLayout" for clarity) with Android stock class ( e.g. with FrameLayout) and make sure that Layout Editor works. Add "tools:..." attributes to allow you to see content, not an empty layout. E.g. if you have EditText widget in your custom view, add this attribute to it, which will be used in Design mode only:

    tools:text="Sample content"

("tools: namespace is added by Android Studio automatically)

  1. Return your original class name (e.g. "MyFrameLayout") to the XML layout. Does it work now?

If not:

  1. Copy definition of your custom view to a temporary new class (e.g. "MyFrameLayoutBeforeFix"), for convenience. You will use it for comparison with "MyFrameLayout" class, which you will start modifying now.

  2. Recreate your "MyFrameLayout" class from scratch, using Android Studio, starting with absolute minimum: it should compile. As a result the Java class will contain "extends "FrameLayout" and required constructors/methods e.g. in this case:

    package com.myprojectxxx.view;import android.content.Context;import android.util.AttributeSet;import android.widget.FrameLayout;public class MyFrameLayout  extends FrameLayout {    public MyFrameLayout(Context context) {        super(context);    }    public MyFrameLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }}
  3. Make sure that this custom view renders normally. It should, at least in year 2016!

  4. Move code piece by piece from a "MyFrameLayoutBeforeFix" copy to this class, checking that there are no errors at each step...

Above sequence seems obvious but it worked for me. The trick is that Layout Editor starts your class in its own context, and this can cause some unexpected errors in your code, which "works" when started from inside your application...

Another trick is to use isInEditMode() check in your widget's code to skip parts, which may not work in Design view. E.g.:

MyClass myClass = isInEditMode() ? null : MyClass.getInstance();