newman
newman

Reputation: 75

custom button always too big

I am trying to create a custom button with its shape being as similar as possible to the one of the Android default button. The problem is, no matter how much I reduce the height, it doesn't go below a certain value:

image of custom and default button

image

The text size of both buttons is 14sp. Here the xlm-code for my custom button:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <padding
        android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp" />
    <solid
        android:color="@color/colorBtnActive" />

    <corners android:radius="2dp" />

    <size
        android:height="16dp"/>

</shape>

So even if I write <size android:height="50dp"/>, the button looks the same.

Here part of the xlm-file of the activity:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_selector_selected"
    android:text="custom"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:text="default"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/button"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.501" />

What am I doing wrong? Thank you!

EDIT: So upon your suggestions I removed the size-tag.Sadly, the button still looks the same. However, I noticed that the buttoms have the same size, the default button just looks smaller: screenshot

How can I achieve this effect?

Upvotes: 1

Views: 642

Answers (2)

Goku
Goku

Reputation: 9692

when you set any Background to A Button its hight will increase check below test cases

  • when you set both button background the button size are same

Try this you also need to set android:background to your default button like this

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:background="@drawable/demo"
        android:text="custom"
        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:background="@color/colorAccent"
        android:text="default"
         />
</LinearLayout>

OUTPUT

![enter image description here

without setting custom background to your custom button

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="custom"
        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:background="@color/colorAccent"
        android:text="default"
         />
</LinearLayout>

Output

enter image description here

EDIT Android Button has default padding.

check the default Size of button

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:background="@drawable/demo"
        android:text="custom"
        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:text="default"
         />
</LinearLayout>

OUTPUT enter image description here

Upvotes: 1

KeLiuyue
KeLiuyue

Reputation: 8237

Android Button has default padding.

When I set android:background="@android:color/holo_blue_bright" in the Button. It will be the same of custom Button .

 <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:text="default"
    android:background="@android:color/holo_blue_bright"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/button"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.501" />

Output

enter image description here

Upvotes: 1

Related Questions