Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a requirement where I need to use a custom TextView across all modules in App .
When user enters XML attribute as "large" ,custom textview should pickup value from dimen.xml and set as 40sp .
I am facing issues while doing this:

What I have tried:

attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="textSize"/>
    </declare-styleable>

    <attr name="textSize" format="enum">
        <enum name="text_size_large" value="1"/>
        <enum name="text_size_small" value="2"/>
        <enum name="text_size_medium" value="3"/>
    </attr>
</resources>

dimens.xml:
<pre><resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="text_size_small">16sp</dimen>
    <dimen name="text_size_medium">25sp</dimen>
    <dimen name="text_size_large">40sp</dimen>
</resources>


CustomTextView.kt:
class CustomFontTextView   @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defStyleRes: Int = 0
) : AppCompatTextView(context, attrs, defStyle) {
    var customFont: String? = null

    init {
        attrs?.let {
            setTextSize(context, attrs);
        }
    }

    private fun setTextSize(context: Context,
                            attrs: AttributeSet){
        val a = context.obtainStyledAttributes(
            attrs,
            R.styleable.CustomFontTextView
        )
        val cf = a.getInteger(R.styleable.CustomFontTextView_textSize, 0)
        var fontSize = 1
        fontSize = when(cf){
            1 -> R.dimen.text_size_large
            2 -> R.dimen.text_size_small
            3 -> R.dimen.text_size_medium
            else -> R.dimen.text_size_small
        }
        println("fontSize $fontSize")
        val textSize  = a.getDimensionPixelSize(fontSize, 0);
        println("TextSize $textSize")
        this.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize.toFloat());
        a.recycle()
    }

 MyLayout:
<pre><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.android.dynamicfeaturemodulesample.UI.CustomFontTextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="57dp"
        android:text="TextView"
        app:fontName="Roboto_Italic"
        app:textSize="text_size_large"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>



The below exception is received:
2020-09-17 21:20:19.262 17486-17486/com.android.dynamicfeaturemodulesample 
E/AndroidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: 
length=875; index=2032337659 at 
android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:774)
Posted
Comments
[no name] 18-Sep-20 6:33am    
We wonder what those "issues" might be. Dog bothering you?
David Crow 18-Sep-20 8:25am    
The exception seems to be self-explanatory. getDimensionPixelSize() is being called with an index of 2032337659, which apparently is outside the array's size.
ChandraSaiMohan bhupati 18-Sep-20 9:25am    
Is there a way to resolve the exception ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900