Pedro Bonini
Pedro Bonini

Reputation: 1

how to get textSize value of a textview from XML and set it in code

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" />

Upvotes: -1

Views: 136

Answers (2)

Sandesh Khutal
Sandesh Khutal

Reputation: 1809

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));

Upvotes: 0

Sunday Ndu
Sunday Ndu

Reputation: 1

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

Upvotes: 0

Related Questions