2

I can't figure out how to create a Button or ImageButton or ImageView or anything that has:

  • A bitmap drawable with the icon
  • A padding

What i have try now is this:

<ImageButton
            android:id="@+id/ShowInstructionsImageButton"
            android:contentDescription="@string/Common_InformationIconContentDescription"
            android:layout_alignParentRight="true"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_margin="14dp"
            android:backgroundTint="@color/normalTextColorPrimaryDark"
            android:background="@drawable/ic_info_bitmap"
            android:layout_centerVertical="true" />

Which works but the clickable area of the ImageButton is 20dp, how i can make the clickable area 48dp?

I have check this discussion here Padding not working on ImageButton but if i use android:src instead of android:background i get a black box in the place of the icon

the icon is this: enter image description here

CDrosos
  • 2,418
  • 4
  • 26
  • 49
  • That should be easy i guess. Just have the selector circular background without Bitmap . and keep the icon as src . If that doesn't not work pls add the drawable with question and also add expected output image . – ADM Oct 11 '20 at 08:33

3 Answers3

1

You can use a MaterialButton:

    <com.google.android.material.button.MaterialButton
        android:layout_width="48dp"
        android:layout_height="48dp"
        style="@style/Widget.App.Button.IconOnly"
        app:icon="@drawable/ic_add_24px"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Circle"
        />

with:

<style name="Widget.App.Button.IconOnly" parent="Widget.MaterialComponents.Button.OutlinedButton" >
    <item name="iconPadding">0dp</item>
    <item name="android:insetTop">0dp</item>
    <item name="android:insetBottom">0dp</item>
    <item name="android:paddingLeft">12dp</item>
    <item name="android:paddingRight">12dp</item>
    <item name="android:minWidth">12dp</item>
    <item name="android:minHeight">12dp</item>
    <item name="iconGravity">textStart</item>
    <item name="strokeWidth">3dp</item>
    <item name="strokeColor">@color/...</item>
</style>

and a circular shape:

<style name="ShapeAppearanceOverlay.MyApp.Button.Circle" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Im getting an error that attr/cornerSize and attr/cornerFamily not found, Maybe Xamarin.Android does not have the same styles with Java, if you have use this code in Java – CDrosos Oct 11 '20 at 09:01
0

First, try to use androidx.appcompat.widget.AppCompatImageButton or androidx.appcompat.widget.AppCompatImageView instead of ImageButton to gain the advantage of backward compatibility.

After that, to show a drawable using it, you should use app:srcCompat, and not android:background. Now, if you add some padding to the view, it will be applied to the showing drawable.

<androidx.appcompat.widget.AppCompatImageButton
    android:id="@+id/ShowInstructionsImageButton"
    android:contentDescription="@string/Common_InformationIconContentDescription"
    android:layout_alignParentRight="true"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:padding="14dp"
    android:layout_centerVertical="true"
    app:tint="@color/normalTextColorPrimaryDark"
    app:srcCompat="@drawable/ic_info_bitmap" />
aminography
  • 21,986
  • 13
  • 70
  • 74
0

I found a solution that works for me:

<ImageButton
            android:id="@+id/ShowInstructionsImageButton"
            android:contentDescription="@string/Common_InformationIconContentDescription"
            android:layout_alignParentRight="true"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_margin="14dp"
            android:backgroundTint="@color/normalTextColorPrimaryDark"
            android:background="@drawable/ic_info_layer"
            android:layout_centerVertical="true" />

with ic_info_layer.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item
      android:drawable="@drawable/ic_info_bitmap"
      android:width="@dimen/IconSize"
      android:height="@dimen/IconSize"
      android:right="@dimen/ButtonIconPadding"
      android:left="@dimen/ButtonIconPadding"
      android:top="@dimen/ButtonIconPadding"
      android:bottom="@dimen/ButtonIconPadding"
      />

</layer-list >

On API < 23 it is important the ImageButton to have fixed width and height because layer width and height is being ignored.

CDrosos
  • 2,418
  • 4
  • 26
  • 49