Monday, August 12, 2019

Change TextView font size in android

Today I'm going to introduce you to an Android example which is How to change TextView font size in Android, you can be real. Now, these steps down here, if there's a problem with How to change TextView font size in Android, comment under the article, I'll do it. Answer to you as soon as you can.
Change TextView font size in android

Step 1. Create layout 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"
    >
    <TextView
        android:id="@+id/text_view1"
        android:text="Code Android example"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
     // use font size 25dp or 25px
    <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"
        />
   // use font size 0.20 in , if you don't in, please seach what in in android
    <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"
        />
    // fonsize mm what is millimeters
    <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>

can you set font size in file dimens.xml

<resources>
   
    <dimen name="activity_horizontal_margin">18dp</dimen>
    <dimen name="activity_vertical_margin">18dp</dimen>
    <dimen name="font_size_small">15sp</dimen>
    <dimen name="font_size_medium">25sp</dimen>
    <dimen name="font_size_large">35sp</dimen>
</resources>

also you can set font size textview in class mainActivity.class below:

        TextView tv1 = (TextView) findViewById(R.id.text_view1);
     
        tv1.setTextSize(50);
        tv1.setText("Font size 50");
        TextView tv2 = (TextView) findViewById(R.id.text_view2);
   
        tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,35);
        tv2.setText("Font size 35 DIP (Device Independent Pixels)");
        TextView tv3 = (TextView) findViewById(R.id.text_view3);
     
        tv3.setTextSize(TypedValue.COMPLEX_UNIT_IN,0.25f);
        tv3.setText("Font size 0.25 Inches");
        TextView tv4 = (TextView) findViewById(R.id.text_view4);
     
        tv4.setTextSize(TypedValue.COMPLEX_UNIT_MM,5);
        tv4.setText("Font size 5 Millimeters");
        TextView tv5 = (TextView) findViewById(R.id.text_view5);
     
        tv5.setTextSize(TypedValue.COMPLEX_UNIT_PT,25);
        tv5.setText("Font size 25 PT (Points)");
        TextView tv6 = (TextView) findViewById(R.id.text_view6);
   
        tv6.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);
        tv6.setText("Font size 50 Pixels");
        TextView tv7 = (TextView) findViewById(R.id.text_view7);
     
        tv7.setTextSize(TypedValue.COMPLEX_UNIT_SP,50);
        tv7.setText("Font size 50 SP (Scale Independent Pixels)");

No comments:

Post a Comment