skip to Main Content

I have recently updated Android Studio to Android Studio Electric Eel | 2022.1.1.

For xml layout if we are using simple view like TextView/Button , it is displayed on preview.
But if we are using CustomView, then it is not showing in preview.(Preview is blank)

Also it is showing error: Missing classes
enter image description here

My Simple Test App , main activity xml file is as

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.example.webviewdeeplink.CustomTextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Test Text"
        android:textSize="24sp"
        app:font="RobotoCondensed-LightItalic.ttf"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Custom Component class –

class CustomTextView : androidx.appcompat.widget.AppCompatTextView {

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context!!,
        attrs,
        defStyle
    ) {
        init(attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {

        init(attrs)
    }

    constructor(context: Context?) : super(context!!) {

        init(null)
    }


    private fun init(attrs: AttributeSet?) {
        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView)
            val fontName = a.getString(R.styleable.CustomTextView_font)
            try {
                if (fontName != null) {
                    val myTypeface = Typeface.createFromAsset(
                        context.assets,
                        "fonts/$fontName"
                    )
                    setTypeface(myTypeface)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            a.recycle()
        }
    }
}

If any one faced this issue , could you please confirm issue.
OR this is known Android Studio Issue?

2

Answers


  1. try this code…

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context!!,
        attrs,
        0
    ) {
        init(attrs)
    }
    

    Replace defStyle with 0

    Login or Signup to reply.
    1. Change the build variant to debug
    2. Clean and Rebuild the project

    This will fix the issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search