How to create really small buttons in android from code?
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:
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" />
user898160
Updated on October 20, 2020Comments
-
user898160 over 3 years
Android default Button class provides a really big button.
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.
Any hint how to decrease the size of the button further?
-
user898160 about 12 yearsThis didn't help either. For some reason I am not able to decrease the size of the button.
-
user898160 about 12 yearsActually does anyone know where is it defined the "android:attr/buttonStyleSmall", in which XML file ?
-
Boy about 11 yearsPerfect! Thanks a lot! was going crazy finding out how to get rid of that unnecessary space above and below the text!
-
Ricardo over 10 yearsI love this, I almost gave up!
-
Alexander Dunaev over 8 yearsI'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 almost 8 yearsCan this be used programmatically?
-
Dimitar over 6 yearsCould 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 over 2 yearsFrom a programmatic point of view, setting
setMinHeight
does nothing whilesetMinimumHeight
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 about 2 yearsDoesn't work for me. My button is large like the tableRow where appear it.