0

In my Button I have this XML:

 <Button
    android:id="@+id/btnFiltrarResultados"
    android:layout_width="18dp"
    android:layout_height="17dp"
    android:layout_above="@+id/searchView"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="-37dp"
    android:layout_marginEnd="29dp"
    android:background="@drawable/filtrar_explorar"
    android:cropToPadding="true"
    android:padding="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/txtExploreTitulo"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.01999998" />

But I can't seem to be able to add padding to it. I've tried using cropToPadding or using android:src but nothing seems to help...

André Silva
  • 121
  • 11

2 Answers2

3

You have

android:layout_width="18dp"
android:layout_height="17dp"

and expect padding 20dp

  • Does that have to do with the problem? – André Silva Jul 20 '18 at 20:21
  • Do you need negative margin? –  Jul 20 '18 at 20:22
  • I do, it's the only way I can set that in the position I want – André Silva Jul 20 '18 at 20:23
  • 1
    see my edited answer, your button is too small to have padding 20dp –  Jul 20 '18 at 20:27
  • Why do you need padding in such a small button? –  Jul 20 '18 at 20:28
  • I need padding because I want its 'clickable area' to be wider without making the image bigger – André Silva Jul 20 '18 at 20:29
  • 1
    This question has some nice pictures about padding vs. margin that may be relevant here https://stackoverflow.com/questions/21959050/android-beginner-difference-between-padding-and-margin Padding is internal to the element, so if you have a height of 17dp and padding of 5dp on top and bottom you are left with only 7dp of content. – Tyler V Jul 20 '18 at 20:29
  • The clickable area is 18x17, can't get any bigger because you set it –  Jul 20 '18 at 20:37
1

Well it's not a way to add image in button, as you did in your xml fileandroid:background="@drawable/filtrar_explorar". because by default background image try to scale as much as possible and ignore padding.So the good practice is use ImageButton with android:src="@drawable/use_your_image" and add android:scaletype="fitCenter"

<ImageButton
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/use_your_image"
        android:scaleType="fitCenter"
        android:padding="10dp"
        />
Khubaib Raza
  • 543
  • 6
  • 10