How to AutoFit Text inside the TextView according to given width and height and auto set the maximum and minimum used font size.
Autofit text is used to automatically scale the TextView text within the given specific height and width bounds. Means Developer can write text in android application without worrying of setting its font size. This type of Text can be auto adjust and well looked in all type of android devices like normal device and Tablets. So here is the complete step by step tutorial for Android AutoFit Text Resize-Scale Within Bounds Example Tutorial using Github library.
Main features of this library :
- Developer can set maximum font size, which can be used as default.
- Developer can set minimum font size. So no matter how much lines you will write it will apply the minimum font size.
- You can set Maximum numbers of lines.
- You can set Single Line , SO all of your text automatically show into Single line.
How to Android AutoFit Text Resize-Scale Within Bounds Example Tutorial using Github library.
1. Open your projects build.gradle(Module : app) file.
2. Add compile ‘me.grantland:autofittextview:0.2.+’ in dependencies block.
3. Open your project’s activity_main.xml file and paste the below Auto Scale Widget Code.
- android:singleLine=”true” : Is used to set all the given text into Single line only.
- android:maxLines=”5″ : Set the maximum number of lines.
- android:textSize=”30dp” : Set the default text font size.
- autofit:minTextSize=”5dp” : Set the minum font size
<me.grantland.widget.AutofitTextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLines="5" android:layout_marginTop="5dp" android:text="This is The Android Automatic Text Fit Example. This Text Will Automatically Resize - Adjust Itself According to Given Width And Height. " android:textSize="30dp" autofit:minTextSize="5dp" android:gravity="center" />
Complete Source Code:
Code for MainActivity.java file.
package com.android_examples.autosizetextview_android_examplescom; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Code for activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:autofit="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" autofit:context="com.android_examples.autosizetextview_android_examplescom.MainActivity" android:layout_margin="5dp"> <me.grantland.widget.AutofitTextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLines="5" android:layout_marginTop="5dp" android:text="This is The Android Automatic Text Fit Example. This Text Will Automatically Resize - Adjust Itself According to Given Width And Height. " android:textSize="30dp" autofit:minTextSize="5dp" android:gravity="center" /> </RelativeLayout>
Screenshots: