AutoFit AutoScale text view means a text view or button that automatically resizes the texts to fit within the specific bounds. How much text you write either five words or 20 words, all text is adjusted/fitted to the given area. In this tutorial, you will learn to make auto fit/ auto scale text in TextViews or Buttons or any android layouts/views/widgets.

We have to specify maximum text size and minimum text size and we have to use some other attributes like maxLines, singleLine etc.

How to Use Custom Fonts in Android Application
Android Auto Complete using AutoCompleteTextView and MultiAutoCompleteTextView
Create Scrolling Text (Marquee) in Android TextView

Android Example: How to AutoFit/AutoScale TextView Text to Fit within Bounds

First open build.gradle file and add compile 'me.grantland:autofittextview:0.2.+' as dependency. Your app build.gradle file will look like below.


Now we have to work in project XML layout file. Open XML layout file of your app and add me.grantland.widget.AutofitTextView… component inside any android layout. Following is the complete content of XML layout file with different attributes.

res/layout/ autoscale_fit_android_textview.xml

Default code of java activity file will look like below.

src/ AndroidAutoFitAutoScaleTextView.java

Android Example: How to AutoFit/AutoScale TextView Text to Fit within Bounds

Now, run your Android AutoFit AutoScale TextView Example application, you will see text of a text view being displayed in a line. If you place more text, the text size will automatically reduce and adjust in a line.

Post a Comment

Powered by Blogger.