Home > database >  How to make android button match the exact size of the inner icon?
How to make android button match the exact size of the inner icon?

Time:03-16

I want my button match the exact size of the inner icon.

The button:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginBottom="0dp"
    android:background="@android:color/transparent"
    android:padding="0dp"
    app:icon="@drawable/circle"
    app:iconGravity="textStart"
    app:iconPadding="0dp"
    app:iconSize="100dp"
    app:iconTint="@color/design_default_color_primary" />

circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
  <stroke
      android:width="1dp"
      android:color="#78d9ff"/>
</shape>

The resulting button has some unexpected top and bottom padding: button

Is it possible to get rid of the paddings?

CodePudding user response:

try to add this properties to the button:

 android:insetTop="0dp"
 android:insetBottom="0dp"

CodePudding user response:

You should try

Step 1: Create Circle

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
  android:width="1dp"
  android:color="#78d9ff"/>
</shape>

Step 2: Import Icon to Android Project

Step 3: Using Layer-list Drawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_corner_black"  />
<item android:drawable="@drawable/ic_arrow_back" android:top="12dp"
    android:bottom="12dp" android:left="12dp"
    android:right="12dp"/>
</layer-list>

Explain : Use padding to shrink the icon inside the button

Hope my answer is helpful to you

  • Related