# AutoSize TextField in Android Jetpack Compose

DevFeed: [AutoSize TextField in Android Jetpack Compose](<https://devfeed.tech/articles/autosize-textfield-in-android-jetpack-compose-25846.md>)

Original publisher: [Read original article](<https://medium.com/@banmarkovic/autosize-textfield-in-android-jetpack-compose-7d2a601636f5?source=rss-8aced922ece------2>)

Author: Ban Markovic

Published: 2022-08-08T08:55:06Z

Content type: tutorial

Language: en

Sources: [Stories by Ban Markovic on Medium](<https://devfeed.tech/sources/stories-by-ban-markovic-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Android](<https://devfeed.tech/topics/android.md>), [ui](<https://devfeed.tech/topics/ui.md>), [XML](<https://devfeed.tech/topics/xml.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [android-jetpack](<https://devfeed.tech/tags/android-jetpack.md>), [compose-ui](<https://devfeed.tech/tags/compose-ui.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This tutorial explains how to implement an autosizing TextField in Android Jetpack Compose. It calculates the input field's available width, measures text using ParagraphIntrinsics, and decreases the font size until the text fits within the field while accounting for default horizontal padding.

## Source excerpt

By introducing Jetpack Compose UI framework, Android has made huge improvement towards speeding up the process of developing Android apps. The only drawback at the moment is that the framework is still young, therefore quite a few features are lacking when compared to the previous View system (XML). One of the latest struggles I had with Jetpack Compose was the implementation of the input field that resizes its font when the text is wider than the input field. Autosizing input field In this post, I'm going to cover steps necessary for the implementation of mentioned TextField in JetpackCompose. In case you are impatient and you want to check Github repo immediately, here is the link with working example. What are we trying to accomplish here Every time our text exceeds the width of our TextField box, we must calculate the new font size to make our content fit within the boundaries of the TextField box. Solution For this specific purpose, we will leverage a few of the features that Jetpack Compose has made available. But first, let's break this work down into three smaller ones so that we can tackle them one at a time: calculate the max width of the input field, calculate the width of the text with given font size, decrease the font size until the width of the given text fits the max width of the input field. 1. Max width of the input field Since working solely with TextField won't give us the information about its width, we are going to use one of the Jetpack Compose composables called BoxWithConstraints, as a parent of our TextField. It can help us get the maxWidth info. BoxWithConstraints(modifier = modifier.fillMaxWidth()) { val textFieldDefaultHorPad = 16.dp val maxInputWidth = maxWidth - 2 * textFieldDefaultHorPad TextField(...) } Here, it's important to note that TextField contains default horizontal padding that is 16 dp on each side, that's why we are calculating our maxInputWidth by subtracting two paddings from BoxWithConstraints maxWidth. If we didn't sub