Psypher Psypher - 1 month ago 11
Android Question

Reduce custom button size

I have created a custom button and placed it in drawable folder, code as below for it:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#f50057" />
<padding
android:left="30dp"
android:top="0dp"
android:right="30dp"
android:bottom="0dp" />
<corners android:radius="5dp" />
</shape>


I am using the above as background for the button, the button is occupying too much space vertically eventhough I have put the padding as 0dp for it. Can you please help me reduce the space occupied by button vertically.

Code in layout:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/start_button"
android:background="@drawable/myrect"
android:focusable="false"
android:text="Start"
android:layout_below="@+id/expandview"
android:layout_centerHorizontal="true" />


enter image description here

Answer

Just specify the desired height and width of the button in your layout XML. i.e

<Button
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/start_button"
    android:background="@drawable/myrect"
    android:focusable="false"
    android:text="Start"
    android:layout_below="@+id/expandview"
    android:layout_centerHorizontal="true" />

If you want to do it in drawable.... then

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#f50057" />
    <padding
        android:left="30dp"
        android:top="0dp"
        android:right="30dp"
        android:bottom="0dp" />
    <corners android:radius="5dp" />
    <size 
        android:height="50dp"
        android:width="50dp" />
</shape>