# Controlling TextView MinWidth

DevFeed: [Controlling TextView MinWidth](<https://devfeed.tech/articles/controlling-textview-minwidth-38652.md>)

Original publisher: [Read original article](<https://krossovochkin.com/posts/2021_09_14_controlling_textview_minwidth/>)

Published: 2021-09-14T00:00:00Z

Content type: tutorial

Language: en

Sources: [Vasya Drobushkov](<https://devfeed.tech/sources/vasya-drobushkov.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [constraintlayout](<https://devfeed.tech/topics/constraintlayout.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-layout](<https://devfeed.tech/tags/android-layout.md>), [background](<https://devfeed.tech/tags/background.md>), [constraintlayout](<https://devfeed.tech/tags/constraintlayout.md>), [gravity](<https://devfeed.tech/tags/gravity.md>), [layout](<https://devfeed.tech/tags/layout.md>), [properties](<https://devfeed.tech/tags/properties.md>), [textview](<https://devfeed.tech/tags/textview.md>), [xml](<https://devfeed.tech/tags/xml.md>)

## AI overview

A technical explanation of how Android TextView minimum-width APIs interact. Setting minWidth or minimumWidth alone may not reset a dimension declared in XML; both properties must be cleared, and a background can still impose a minimum width.

## Source excerpt

Introduction Sometimes we might need our TextView with wrap_content width to occupy more space than it will based on amount of characters it displays. For that we can set minimum width. Usually, we do that via XML like this: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:gravity="center" android:minWidth="200dp" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> Having setup like this we ensure that our TextView will occupy a minimum of 200dp. Also, we can control minimum width programmatically via: TextView#setMinWidth. But controlling minimum width programmatically has one caveat that I'd like to describe below.