0

I have a button that I set a particular height for and have the button text fill the entire button. I achieved this before by setting textSize to a specific dimension to fill the button.

But the problem is whenever the user changes the system font of his/her device, it affects the button text and the text overflows and gets cut in the button. How can I make the button text size match the height of the button?

Edit

The button's height is set to match_parent for it's parent widget

 <android.support.v7.widget.Toolbar
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="?attr/actionBarSize"
 android:background="?attr/colorPrimary"
 app:popupTheme="@style/AppTheme.PopupOverlay">
 <RelativeLayout
 android:layout_marginEnd="16dp"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginRight="16dp">
 <Button
 android:id="@+id/text_toolbar"
 style="?android:attr/borderlessButtonStyle"
 android:layout_centerInParent="true"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:textSize="36sp"
 android:textColor="@color/white"
 android:text="@string/k_i_n_e_c_t"/>
 </RelativeLayout>
 </android.support.v7.widget.Toolbar>
asked Jun 8, 2017 at 5:12
2
  • Set your button height to wrap content then it'll automatically adjust to change in text size. Commented Jun 8, 2017 at 5:15
  • See the edit I made Commented Jun 8, 2017 at 5:26

5 Answers 5

1

try to wrap_content property to both RelativeLayout and Button like above code ,

 <android.support.v7.widget.Toolbar
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="?attr/actionBarSize"
 android:background="?attr/colorPrimary"
 app:popupTheme="@style/AppTheme.PopupOverlay">
 <RelativeLayout
 android:layout_marginEnd="16dp"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginRight="16dp">
 <Button
 android:id="@+id/text_toolbar"
 style="?android:attr/borderlessButtonStyle"
 android:layout_centerInParent="true"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textSize="36sp"
 android:textColor="@color/white"
 android:text="@string/k_i_n_e_c_t"
 android:padding="2sp"/>
 </RelativeLayout>
 </android.support.v7.widget.Toolbar>
answered Jun 8, 2017 at 5:39
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That worked. Had to change the width to wrap_content though
1

Just take the height from the button with:

button.getHeight();

set your text size with:

button.setTextSize();
answered Jun 8, 2017 at 5:18

1 Comment

That won't work cos they have different dimensions. Besides, I need an xml fix, not Java
1

If you use this wrap_content the text does't overflow

<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Upload Image"
 android:id="@+id/buttonUpload" />
answered Jun 8, 2017 at 5:19

Comments

0

I guess you must have used dp as the unit for your textsize. It is preferable to use sp for text dimensions.

Why should we use sp for font sizes in Android?

answered Jun 8, 2017 at 5:21

1 Comment

That is what I'm using
0

You can achieve this by setting width, height & wrap_content of the button.

Or you can try by fixing the size of the button and change the button size dynamically as per the text size.

SYAM KRISHNA MG
9301 gold badge13 silver badges30 bronze badges
answered May 24, 2018 at 11:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.