Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create really small buttons in android from code?

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?

like image 534
user898160 Avatar asked Apr 21 '12 12:04

user898160


People also ask

How can I make my Android button more attractive?

Using colors (background, text, border) Using custom shapes like circle, rounded corners and more. You can add images to your buttons to customize them. Using drawables to make gradients, dotted borders and more.

Can two buttons have same ID Android?

This is no longer possible,android lint checks prevent you from adding same ids in the same xml file. A work-around would be to use the include tag to include other xmls with repeating ids.


1 Answers

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

like image 63
Maarten Pennings Avatar answered Sep 19 '22 15:09

Maarten Pennings