How to create really small buttons in android from code?

33,894

Solution 1

Buttons have a minimal height and width (typically of 48dp respectively 64dp by default). So add

android:minHeight="0dp"
android:minWidth="0dp"

to get really small buttons:

enter image description here

Solution 2

For your information there is also the default style provided by Android that defines smaller buttons:

style="?android:attr/buttonStyleSmall"

You can also if necessary modify this default style and define custom attributes in your file styles.xml. Here is an example with the modifications described in the question:

<style name="MyButtonStyleSmall" parent="android:Widget.Button.Small">
    <!-- Customizations  -->
    <item name="android:minHeight">0dp</item>
    <item name="android:minWidth">0dp</item>
</style>

Then you just need to call it in the XML:

<Button 
   android:id="@+id/small_button"
   android:text="@string/example"
   android:contentDescription="@string/example_desc"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   style="@style/MyButtonStyleSmall" />
Share:
33,894
user898160
Author by

user898160

Updated on October 20, 2020

Comments

  • user898160
    user898160 over 3 years

    Android default Button class provides a really big button.

    button with caption "Save"

    There is a lot of wasted space around. Now I want to create really small buttons, that are only slightly bigger than the text/caption. How to do this from code? So far I found the following method, but it is still providing too big buttons and wasting too much space:

    A. Create an XML (smallbutton.xml) in the res/layout:

    <?xml version="1.0" encoding="utf-8"?>
    

        style="?android:attr/buttonStyleSmall"
        android:text="color"
        android:padding="0dp"
        android:layout_margin="0dp"
    
        android:textSize="8dp"
        android:maxHeight="2dp"
        android:maxWidth="2dp"
    
    />
    

    B. From your code inflate this and add to the view:

    Button pickBackColor = (Button) this.getLayoutInflater().inflate(R.layout.smalbutton,null);
    

    ...

    LinearLayout linLayout = new LinearLayout(this);
    
    linLayout.addView(pickBackColor);
    

    Now the problem is that the button is still too big, it uses too much space around (on the left, right, above, under) of the text.

    enter image description here

    Any hint how to decrease the size of the button further?

  • user898160
    user898160 about 12 years
    This didn't help either. For some reason I am not able to decrease the size of the button.
  • user898160
    user898160 about 12 years
    Actually does anyone know where is it defined the "android:attr/buttonStyleSmall", in which XML file ?
  • Boy
    Boy about 11 years
    Perfect! Thanks a lot! was going crazy finding out how to get rid of that unnecessary space above and below the text!
  • Ricardo
    Ricardo over 10 years
    I love this, I almost gave up!
  • Alexander Dunaev
    Alexander Dunaev over 8 years
    I've faced this problem after moving my app from API 7 to API 10 (alright, I want my app to run on some ancient crap); I originally had style="?android:attr/buttonStyleSmall" on my buttons, but that style stopped working after the migration to a newer API. Thanks Maarten, now I don't need the style, as minHeight and minWidth do the work perfectly!
  • Hephaestus
    Hephaestus almost 8 years
    Can this be used programmatically?
  • Dimitar
    Dimitar over 6 years
    Could you explain why would that help? IMHO this is not right, the way is through the minimal size of the buttons, which is hard-set and should be explicitly changed.
  • pieroxy
    pieroxy over 2 years
    From a programmatic point of view, setting setMinHeight does nothing while setMinimumHeight does the trick. Why there are two methods is anyone's guess. Took me a while to get around that one. Same for width of course.
  • Cipo
    Cipo about 2 years
    Doesn't work for me. My button is large like the tableRow where appear it.