Android Studio layout editor cannot render custom views
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?
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.
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.