Skip to content Skip to sidebar Skip to footer

How To Create Really Small Buttons In Android From Code?

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 th

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:

<stylename="MyButtonStyleSmall"parent="android:Widget.Button.Small"><!-- Customizations  --><itemname="android:minHeight">0dp</item><itemname="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" />

Solution 3:

try this in your code:

pickBackColor.setWidth(VALUE);
pickBackColor.setHeight(VALUE);

Solution 4:

    <com.google.android.material.button.MaterialButton
        android:id="@+id/food_item_minus_button"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/custom_small_button_bg"
        android:drawableLeft="@drawable/ic_minus"
        android:paddingStart="6dp"
        android:paddingLeft="6dp" />

Post a Comment for "How To Create Really Small Buttons In Android From Code?"