How to change TextView font size on button click.
TextView font size can be set dynamically using .setTextSize() function. This feature is basically used where app builder need to increase or decrease given text size according to app user requirement. So here is the complete step by step tutorial for Change TextView font size in android programmatically.
Change TextView font size in android programmatically.
Code for MainActivity.java file.
package com.android_examples.com.changetextsize; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { TextView TxtfONT; Button BtnfONT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TxtfONT = (TextView)findViewById(R.id.textView1); BtnfONT = (Button)findViewById(R.id.button1); BtnfONT.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TxtfONT.setTextSize(40); } }); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.com.changetextsize.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="138dp" android:text="TEXT IS HERE" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="CLICK HERE TO CHANGE FONT SIZE" /> </RelativeLayout>
Screenshots: