simple
simple

Reputation: 159

Change font size at run time android

I have one textview and it has size as 32sp in xml.

android:textSize="32sp"

I wanted to change it as 28sp programmatically. So i used the below code.

txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.twenty_eight_sp));

But the above code is adding the 28sp to 32sp. So the font became too large. I don't want to add the font size, I want to set a new font size.

Can anyone suggest me.

Upvotes: 4

Views: 7636

Answers (4)

Android Geek
Android Geek

Reputation: 9225

Try this

txt_home.setTextSize(12);

Upvotes: 1

Harsh
Harsh

Reputation: 413

check this link https://developer.android.com/reference/android/widget/TextView#setTextSize(int,%20float)

consider both functions as per your requirement

enter image description here

Upvotes: 1

Rashpal Singh
Rashpal Singh

Reputation: 633

You have to change it to TypedValue.COMPLEX_UNIT_PX because getDimension(id) returns a dimen value from resources and implicitly converted to px.

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
           getResources().getDimension(R.dimen.result_font));

Upvotes: 8

Sari Masri
Sari Masri

Reputation: 226

txt.setTextSize(TypedValue.COMPLEX_UNIT_SP,getResources().getDimension(R.dimen.twenty_eight_sp));

Upvotes: 1

Related Questions