Jul 11, 2017

Android O – Autosizing TextViews

Introduction

Android O is coming. Right now in June, we’re in Release Preview 3 with an expected final release date of Q3. My guess for the dessert name is Oreo, but I haven’t been right on a single one so far. Android O feels more like a minor change with incremental improvements, similar to the change with Marshmallow to Nougat, as compared to the complete overhaul that KitKat to Lollipop felt like. That doesn’t mean some neat things aren’t being released in Android O, though! Here is the feature (with examples) of the new API change that I am most excited about.

Autosizing Textviews

I know what you’re thinking. Finally?! Right? No longer do you have to include a third party library in your project to achieve this functionality. This layout gist and accompanying java gist provides the following screenshot:

    There are three ways to setup an Autosizing TextView in your project: Default, Granularity, and Preset sizes. When using the Default  setting, this allows the TextView to uniformly scale along both the X and Y axis. This can be accomplished by adding the field

android:autoSizeTextType="uniform"

 

to your TextView.

When setting the Granularity property of your TextView, you set a range of minimum and maximum values, along with an incremental value, to determine how your TextView scales. An example of how to set this up in your TextView might look like this:

  android:autoSizeTextType="uniform"
  android:autoSizeMinTextSize="12sp"
  android:autoSizeMaxTextSize="100sp"
  android:autoSizeStepGranularity="2sp"

When using preset sizes with your TextView, you declare an array of sizes in your arrays.xml file and let your TextView know it should  scale according to the values listed in your arrays.xml file. A sample usage might look like:

<resources>
  <array
    name="text_view_sizes">
    <item>8sp</item>
    <item>12sp</item>
    <item>60sp</item>
    <item>100sp</item>
  </array>
</resources>

 

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:autoSizeTextType="uniform"
  android:autoSizePresetSizes="@array/text_view_sizes"/>

Conclusion

Android O has a lot of really great API changes coming, including Autosizing Textviews, Font declaration in XML, Unified layout margin and padding, and many other changes. Most of these changes will not be noticeable to the user, but they will certainly make a developer’s life easier.

 

About the Author

Object Partners profile.
Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]