Why setMinWidth and setMinimumWidth both??

Niharika Arora
1 min readSep 14, 2019

--

Setting Minimum Width in Textview

Clean code says no related names of function in your code bases but still the Android devs at Google have kept setMinimumWidth and setMinWidth 🤔

My last article was on issues I faced during Unit Testing in MVVM. Sharing one more problem I had while setting minimum width in a Textview programmatically.

This sounds very simple, Right. So I had one textView, where I wanted some minimum width initially to show a background drawable and reset the background to null and minWidth to 0 after getting the text from the server.

This is my TextView XML -

Then after fetching the data, I set the minWidth to 0-

textView.setMinWidth(0);

But when I run the code, it didn’t work for me. So After some research, I changed the code like this :

textView.setMinWidth(0);
textView.setMinimumWidth(0);

And here is the reason -

setMinWidth is defined by TextView, while setMinimumWidth is defined by View.

“According to the docs, the greater of the two values is used, so both must be set”.

minWidth attribute in XML corresponds to the setMinimumWidth method.

Also, Another alternative we can use is setPadding(0,0,0,0).

Please feel free to share your opinion in comments.

If you liked this blog, hit the 👏 . Stay tuned for the next one!

--

--

Niharika Arora

Googler since 2021 | Ex- Google Developer Expert, Android | Visit : https://thedroidlady.com/ to know more.