Home > Mobile >  How to use CustomView in Android
How to use CustomView in Android

Time:08-09

In my application I create one custom view for show TextView and FontAwesome!
I write below codes, but not set values and just show default values!
TextWithIcon class :

class TextWithIcon @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defStyleRes) {

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_text_with_awesome, this, true)
        orientation = VERTICAL

        attrs?.let {
            val typedArray = context.obtainStyledAttributes(R.styleable.TextWithIcon)
            val title = resources.getText(
                typedArray.getResourceId(
                    R.styleable.TextWithIcon_customText,
                    R.string.app_name
                )
            )
            val icon = resources.getText(
                typedArray.getResourceId(
                    R.styleable.TextWithIcon_customIcon,
                    R.string.app_name
                )
            )

            val titleTxt = getChildAt(0) as TextView
            titleTxt.text = title
            Log.e("titleTxt",title.toString())
            //binding.iconTxt.text = "&#x$icon"

            typedArray.recycle()
        }
    }
}

CustomViewLayout file :

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@ id/titleTxt"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/_20mdp"
        android:gravity="right"
        android:textColor="@color/darkJungleGreen"
        android:textSize="@dimen/_10font_mdp" />

    <com.myapp.utils.views.FontAwesome
        android:id="@ id/iconTxt"
        android:layout_width="@dimen/_20mdp"
        android:layout_height="@dimen/_20mdp"
        android:layout_gravity="right"
        android:gravity="center"
        android:textColor="@color/beauBlue"
        android:textSize="@dimen/_10font_mdp"
        app:fontPath="fonts/fontawesome_re.ttf" />

</merge>

And I used this class with code in XML :

        <com.myapp.TextWithIcon
            android:id="@ id/item1Title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/_10mdp"
            android:drawablePadding="@dimen/_5mdp"
            android:gravity="center"
            android:textColor="@color/ochre"
            android:textSize="@dimen/_10font_mdp"
            app:customIcon="f007"
            app:customText="wegwergewrg"
            app:fontPath="fonts/iransans_bold.ttf"
            app:layout_constraintEnd_toStartOf="@id/item1Line"
            app:layout_constraintTop_toTopOf="@id/item1Line" />

I set value with this code : app:customText="wegwergewrg", but not show this and just show default value from

resources.getText(
                typedArray.getResourceId(
                    R.styleable.TextWithIcon_customText,
                    R.string.app_name
                )
            )

How can I fix it?

CodePudding user response:

You are going to want your styleable to look something like this:

<resources>
    <declare-styleable name="TextWithIcon">
        <attr name="customText" format="reference|string" />
    </declare-styleable>
</resources>

This definition will permit you to use text as well as a string resource id for "app:customText".

You can get the value from the attribute as follows:

class TestCustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : androidx.appcompat.widget.AppCompatTextView(context, attrs) {

    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.TextWithIcon)
        for (attr in 0 until a.indexCount) {
            when (a.getIndex(attr)) {
                R.styleable.TextWithIcon_customText -> {
                    text = a.getText(attr) ?: "Unknown text."
                }
            }
        }
        a.recycle()
    }
}
  • Related