Implement a custom TextView with custom textSize
Asked Answered
L

1

9

I have implemented a custom TextView, then I need to change its text size for future usages. At that step I decided to make its textSize custom with using style.xml and attr.xml

However I am getting a runtime error while my custom layout is inflating. The error related with the parts that Im changed for making it custom.

Here is the code pieces that I was implemented.

attr.xml

<declare-styleable name="FieldLayout">
    <attr name="right_detail_text_size" format="dimension"/>
</declare-styleable>

styles.xml

 <style name="TextViewRightDetail">
    <item name="android:textSize">?attr/right_detail_text_size</item>
 </style>

layout_field_detailed.xml

    <com.my.view.TextViewFont
        android:id="@+id/layout_fielddetailed_textview_rightdetail"
        style="@style/TextViewRightDetail" />

layout_implementation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:iattr="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.my.view.FieldLayoutDetailed
        android:id="@+id/fieldlayoutdetailed_example"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        iattr:right_detail_text_size="16sp"/>
</LinearLayout>

FieldLayoutDetailed.java

public FieldLayoutDetailed(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.FieldLayout, 0, 0);
    LayoutInflater.from(context).inflate(R.layout.layout_field_deailed, this, true);

Error that Im getting at Runtime

...
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at android.widget.TextView.<init>(TextView.java:1003)
at android.widget.TextView.<init>(TextView.java:632)
at android.widget.TextView.<init>(TextView.java:628)
at com.my.view.TextViewFont.<init>(TextViewFont.java:29)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.my.view.FieldLayoutDetailed.<init>(FieldLayoutDetailed.java:50)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.my.ActivityWithMenu.setContentView(ActivityWithMenu.java:77)
at com.my.FastCreditApplyMain.onCreate(FastCreditApplyMain.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Now I am solved the problem by dynamically change text size, but I am curious about the reason of the problem.

Thank you.

Latinist answered 2/7, 2015 at 15:10 Comment(2)
Please add TextViewFont.java implementationEurhythmics
See also Accessing attrs in AttributeSet for custom componentsNorvol
E
15

This is your problem:

Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
    at com.my.view.TextViewFont.<init>(TextViewFont.java:29)

Possible solution: place this into your contructor of TextViewFont.java

TypedArray a = context.obtainStyledAttributes(
        attrs,
        R.styleable.FieldLayout,
        0, 0);

try {
    final int fontSize = a.getDimensionPixelSize(R.styleable.FieldLayout_right_detail_text_size, 0);
    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
}

finally {
    a.recycle();
}

or

TypedArray a = context.obtainStyledAttributes(
        attrs,
        R.styleable.FieldLayout,
        0, 0);

try {
        final int fontSize = a.getDimension(R.styleable.FieldLayout_right_detail_text_size, 0);
        this.setTextSize(fontSize);
}

finally {
    a.recycle();
}
Eurhythmics answered 4/7, 2015 at 11:19 Comment(4)
Thank you so much, before your answer I understand that I do not have textsize definition in default theme so it will crash because of undefined.Latinist
Yes, this can be a problem too, if you didn't set ex. parent="Theme.AppCompat" for theme you are using.Eurhythmics
Since you transformed the dimension to pixels when calling getDimensionInPixelSize(), you should use setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize) instead of setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize) or you'll get a wrong size. Don't you agree?Quest
the combination of using format="dimension" in my styleable in xml and getDimensionPixelSize was what worked for meKarolyn

© 2022 - 2024 — McMap. All rights reserved.