Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap_content is not working in button height android

Tags:

android

"wrap_content" is not working in my button, it currently looks like this:

 now                        want
 ____________
|            |           _____________
|   aaaaaa   |     =>   | aaaaaaaaaaa |
|____________|           -------------

and the xml for my buttons :

 <Button 
     android:layout_gravity="center"
     android:id="@+id/input_expend"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"                               
     android:textSize="12sp"
     android:text="aaaaaaa"                         
     android:background="@drawable/btn_01"/>
 <Button 
     android:layout_gravity="center"
     android:id="@+id/input_expend"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"                               
     android:textSize="12sp"
     android:text="aaaaaaa"                         
     android:background="@drawable/btn_02"/> 

How can I solve this problem? Thanks!

like image 254
Urban GK Avatar asked Dec 04 '13 21:12

Urban GK


People also ask

How to set action on button in android studio?

To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

How to change button background in android studio?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


3 Answers

Button is a View and the android:minHeight attribute is set 48dip default. You can set minHeight lower than your actual text 'aaaaa' such as 1dp. Then wrap_content will work. But I don't think this is recommended.

like image 100
haluk7 Avatar answered Oct 13 '22 01:10

haluk7


One solution is to add android:padding="@null" to the button in your XML. This will override any existing padding being enforced.

There is also an issue I've come across where a button will not shrink smaller than the padding defined in the default button style. I highlighted this in my own question some time ago but haven't had an answer that resolves it yet.

like image 23
Sound Conception Avatar answered Oct 13 '22 03:10

Sound Conception


Changing the background didn't work, as well as padding="@null".

I ended up using a TextView because my button was simple enough for it to suffice:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Join"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="5dp"
        android:textSize="@dimen/xsmall_text_size"
        android:textColor="@drawable/button_text_selector"
        android:background="@color/turquoise"
        android:textStyle="bold"
        android:onClick="buttonOnClick"
        android:clickable="true"/>
like image 28
bentzy Avatar answered Oct 13 '22 02:10

bentzy