Set Text Size Base Of Operations On Typedvalue

Posted on

setTextSize (int unit, float size) set the default text size to a given unit of measurement in addition to value. See TypedValue for the possible dimension units.

Example to prepare text size of TextView base of operations on TypedValue:

MainActivity.java

package com.blogspot.android_er.androidfonts;  import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.TypedValue; import android.widget.TextView;  populace shape MainActivity extends AppCompatActivity {      TextView text1, text2, text3, text4, text5, text6;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         text1 = (TextView)findViewById(R.id.text1);         text2 = (TextView)findViewById(R.id.text2);         text3 = (TextView)findViewById(R.id.text3);         text4 = (TextView)findViewById(R.id.text4);         text5 = (TextView)findViewById(R.id.text5);         text6 = (TextView)findViewById(R.id.text6);          text1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);         text1.setText("20 DIP (Device Independent Pixels)");          text2.setTextSize(TypedValue.COMPLEX_UNIT_IN, 0.5f);         text2.setText("0.5 inch");          text3.setTextSize(TypedValue.COMPLEX_UNIT_MM, 10);         text3.setText("10 millimeter");          text4.setTextSize(TypedValue.COMPLEX_UNIT_PT, 30);         text4.setText("30 points");          text5.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);         text5.setText("30 raw pixels");          text6.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);         text6.setText("30 scaled pixels");     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidfonts.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <TextView         android:id="@+id/text1"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/text2"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/text3"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/text4"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/text5"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/text6"         android:layout_width="match_parent"         android:layout_height="wrap_content" /> </LinearLayout>