androidkotlinandroid-custom-view

Android customView(extends AppCompatImageView) app:srcCompat not showing image


I made custom view just extends AppCompatImageView

class BadgeImageView: AppCompatImageView {
    constructor(context: Context) : super(context) {

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

    }
    constructor(context: Context, attrs:AttributeSet?, defStyleAttr:Int):super(context, attrs,defStyleAttr){

    }
}

and when I used AppCompatImageView like..

<androidx.appcompat.widget.AppCompatImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_button_star" />

AppCompatImageView Result

and when I used BadgeImageView like..

  <com.my.proj.common.view.BadgeImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_button_star" />

BadgeImageView Result

As you can see, I just used a custom view instead of it, but it's not loading the image at all.

Why?


Solution

  • Try out using this. I hope work fine.

    class BadgeImageView @JvmOverloads constructor(
                context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
        ) : AppCompatImageView(context, attrs, defStyleAttr) { 
    
    
        }
    

    Xml code snippet here.

    <ui.components.BadgeImageView
                app:srcCompat="@drawable/ic_reported_image"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                     app:layout_constraintBottom_toTopOf="@id/reportedTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"/>
    

    here is screenshot

    
here is screenshot