Android Studio layout editor cannot render custom views

By : Naetmul
Source: Stackoverflow.com
Question!

In Android Studio, the layout editor cannot preview custom views in xml.

Very simple example:

public class MyCustomView extends FrameLayout {
    public MyCustomView(Context context) {
        super(context);
    }

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

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

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.myprojectxxx.view.MyCustomView
        android:layout_width="48dp"
        android:layout_height="48dp" />

</LinearLayout>

Android Studio always says,

Rendering Problems

The following classes could not be found:

  • com.myprojectxxx.view.MyCustomView (Fix Build Path, Create Class)

Tip: Try to build the project

Of course, I HAVE that class. If I click "Create Class", it complains that the same class already exists. If I rebuild that project, nothing changes.

And, yes, the project works very well on my Android device. Also, it is rendered very well in Eclipse ADT. However, in Android Studio, it always says that "CLASSES COULD NOT BE FOUND."

Android Studio does not have the ability to preview a xml file with custom views? What's wrong with this?

By : Naetmul


Answers

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 with Android stock class and make sure that Layout Editor works. Add "tools:..." attributes to allow you to see content, not an empty layout.

  2. Return your original class name to the XML layout. Does it it work now?

If not:

  1. Copy definition of your custom view to the new class, for convenience. It will be used for comparison with your original class.

  2. Recreate your original Java class from scratch, using Android Studio, starting with absolute minimum: it should compile. As a result the Java class will contain "extends SomeAndroidViewClass" 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 MyCustomView  extends FrameLayout {
        public MyCustomView(Context context) {
            super(context);
        }
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyCustomView(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 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...

By : yvolk


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.



This video can help you solving your question :)
By: admin