-1

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

Psypher
  • 10,717
  • 12
  • 59
  • 83

3 Answers3

3

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>
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Harry Sharma
  • 2,190
  • 2
  • 15
  • 41
3

The android button view has a minimum height so if you want something smaller, you have to override it with

android:minHeight

Like in this code :

<Button
    android:id="@+id/btn_cancel"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:minHeight="10dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:text="@string/cancel"
    android:textColor="@color/white" />

Then you can obviously the layout_height parameter but it won't work if your value is below 48 dp (default minimimum height). For instance, the minimum width of the Button view is 64 dp.

Jejefcgb
  • 390
  • 3
  • 14
  • I don't want to change in Button, I want it be global and change it ony in the tag. – Psypher Dec 03 '14 at 15:25
  • Why don't you want to change this ? Will it be OK to create a style with your shape in background and the minHeight parameter ? – Jejefcgb Dec 03 '14 at 16:20
  • That might do if its possible..but is there no way to correct it in shape. – Psypher Dec 03 '14 at 16:28
  • The thing is that your shape file defines only the background of your button and it's shape (border & corners). The size of your view cannot be managed from this file. – Jejefcgb Dec 03 '14 at 16:44
2

you can use the size tag to specify the size of drawable and button height ,width will be wrap_content

<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>

or you can specify for height and width for specific button like this

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" />
lalit vasan
  • 450
  • 3
  • 6