Home > front end >  how to get textSize value of a textview from XML and set it in code
how to get textSize value of a textview from XML and set it in code

Time:09-12

I want to set the textSize value of the snippet below to a textSize of an XML file, how to achieve this?

holder.tvValue.setTextSize();

in the xml it is like this:

<TextView
   android:id="@ id/tv_title"
   android:textSize="32sp"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent" />

CodePudding user response:

I am assuming you are using Java here. This method TextView.setTextSize(float size); requires you to enter a value as the parameter. Try entering holder.tvValue.setTextSize(32f);. Let me know if this helps you

CodePudding user response:

Find the [link]1 for more details.

Example:

holder.tvValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 65);

OR Cleaner and more reusable approach is

define text size in dimens.xml file inside res/values/ directory:

</resources>
   <dimen name="text_medium">14sp</dimen>
</resources>

and then apply it to the TextView:

holder.tvValue.setTextSize(TypedValue.COMPLEX_UNIT_SP,context.getResources().getDimension(R.dimen.text_medium));
  • Related