Saturday, January 3, 2015

How to change TextView font size in android

TextView font size
TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file.
activity_main.xml

<LinearLayout
    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:orientation="vertical"
    android:layout_margin="25dp"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/text_view1"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/text_view2"
        android:text="Font size 25dp or dip (Density-independent-Pixels)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:textSize="25dp"
        />

    <TextView
        android:id="@+id/text_view3"
        android:text="Font size 0.20in (inches)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FF5500"
        android:textSize="0.20in"
        />

    <TextView
        android:id="@+id/text_view4"
        android:text="Font size 4mm (millimeters)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#DC143C"
        android:textSize="4mm"
        />

    <TextView
        android:id="@+id/text_view5"
        android:text="Font size 15pt (points)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#8A2BE2"
        android:textSize="15pt"
        />

    <TextView
        android:id="@+id/text_view6"
        android:text="Font size 50px (pixels)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#A52A2A"
        android:textSize="50px"
        />

    <TextView
        android:id="@+id/text_view7"
        android:text="Font size 25sp (Scale-independent-Pixels)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#1E90FF"
        android:textSize="25sp"
        />

    <TextView
        android:id="@+id/text_view8"
        android:text="Font size small (from dimens.xml)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#4B0082"
        android:textSize="@dimen/font_size_small"
        />

    <TextView
        android:id="@+id/text_view9"
        android:text="Font size medium (from dimens.xml)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#4B0082"
        android:textSize="@dimen/font_size_medium"
        />

    <TextView
        android:id="@+id/text_view10"
        android:text="Font size large (from dimens.xml)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#4B0082"
        android:textSize="@dimen/font_size_large"
        />

</LinearLayout>
dimens.xml file allow us to declare dimension variables. dimension type resources are as like string resources. we can declare different dimens.xml files for different types of screens. in this example, we uses dimens.xml file to decalre three diffrent font size variables small, medium and large.
res/values/dimens.xml

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="font_size_small">10sp</dimen>
    <dimen name="font_size_medium">20sp</dimen>
    <dimen name="font_size_large">30sp</dimen>
</resources>

the following image shows the initial state of this application. this app describe how can we define different font size of TextView widgets in xml layout file and dimens.xml file.


the following image displays the result of this android app. the image shows different font size of different TextView widgets.

Dimension
we defined dimension value in xml layout file as example 50sp, 20pt, 30dp. dimension is specified with a number followed by a unit of measure. android supported the following units of measure, those are dp, sp, pt, px, mm, in.

dp means Density-independent-Pixels. this is an abstract unit that is based on the physical density of the screen.

sp means Scale-independent-Pixels. SP as like dp but it is also scaled by the user's font size preference. Sp is recommended unit for measure.

pt means Points - 1/72 of an inch based on the physical size of screen. px describe Pixels which corresponds to actual pixels on the screen. mm is Millimeters which is based on the physical size of the screen. in describe inches which also based on the physical size of the screen.

Programmatically define the font size

android app developers can also define the TextView font size programmatically at run time. to define text size programmatically we can use setTextSize() method. we also can define the complex unit (sp, dp, pt, px, mm, in) by importing the android.util.TypedValue class.

this class works with setTextSize() method as example setTextSize(TypedValue.COMPLEX_UNIT_SP, 50). setTextSize() method has two parameters unit and size. unit is the desired dimension unit and size is the desired size in the given units.
activity_main.xml

<LinearLayout
    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:orientation="vertical"
    android:layout_margin="25dp"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/text_view1"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/text_view2"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:textColor="#00008B"
        />

    <TextView
        android:id="@+id/text_view3"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#9932CC"
        />

    <TextView
        android:id="@+id/text_view4"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#00008B"
        />

    <TextView
        android:id="@+id/text_view5"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#9932CC"
        />

    <TextView
        android:id="@+id/text_view6"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#00008B"
        />

    <TextView
        android:id="@+id/text_view7"
        android:text="A default font size text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#9932CC"
        />

    <Button
        android:id="@+id/push_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:text="Apply Font Size"
        android:onClick="perform_action"
        />

</LinearLayout>
MainActivity.java

package com.cfsuman.me.myapplication5;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.util.TypedValue;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //button click handler
    public void perform_action(View v){

        TextView tv1 = (TextView) findViewById(R.id.text_view1);
        //set the font size 50
        tv1.setTextSize(50);
        tv1.setText("Font size 50");

        TextView tv2 = (TextView) findViewById(R.id.text_view2);
        //define font size 35 dp or dip (Device Independent Pixels)
        tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,35);
        tv2.setText("Font size 35 DIP (Device Independent Pixels)");

        TextView tv3 = (TextView) findViewById(R.id.text_view3);
        //define font size 0.25 Inches
        tv3.setTextSize(TypedValue.COMPLEX_UNIT_IN,0.25f);
        tv3.setText("Font size 0.25 Inches");

        TextView tv4 = (TextView) findViewById(R.id.text_view4);
        //define text size 5 Millimeters
        tv4.setTextSize(TypedValue.COMPLEX_UNIT_MM,5);
        tv4.setText("Font size 5 Millimeters");

        TextView tv5 = (TextView) findViewById(R.id.text_view5);
        //define text size 25 Points
        tv5.setTextSize(TypedValue.COMPLEX_UNIT_PT,25);
        tv5.setText("Font size 25 PT (Points)");

        TextView tv6 = (TextView) findViewById(R.id.text_view6);
        //set the text size 50 Pixels
        tv6.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);
        tv6.setText("Font size 50 Pixels");

        TextView tv7 = (TextView) findViewById(R.id.text_view7);
        //set the text view text size 50 SP (Scale Independent Pixels)
        tv7.setTextSize(TypedValue.COMPLEX_UNIT_SP,50);
        tv7.setText("Font size 50 SP (Scale Independent Pixels)");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


the following image display the initial states of this application. this example demonstrate how can we define text size programmatically at run time in java file.



when someone click the push button, the different TextView changes its text size as defined in java file. in the java file we uses the settextSize() method to set different text size for different TextView widget. following image shows the result.

More android examples