How do you set MaterialButton icon size to fit the constraints?
Asked Answered
F

0

6

I have a MaterialButton constrained on every side and want to fit a vector icon to those constraints so that it expands or contracts based on the button constraints.

These are the basic attributes:

<com.google.android.material.button.MaterialButton
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:icon="@drawable/icon_name"
    app:layout_constraintBottom_toTopOf="view1"
    app:layout_constraintEnd_toStartOf="view2"
    app:layout_constraintStart_toEndOf="view3"
    app:layout_constraintTop_toBottomOf="view4" />

I have tried the following:

These are static dimensions based on screen size, so they don't scale.

<com.google.android.material.button.MaterialButton
    android:scaleX="3F"
    android:scaleY="3F" />

This displays the image in its original size.

<com.google.android.material.button.MaterialButton
    app:iconSize="0dp" />
  • I cannot use android:foreground because I am supporting versions lower than API 23.
  • I cannot place an image in android:background because it breaks MaterialButton's functionality, as stated in the official documents.
  • I don't know the purpose of the android:drawable... options. There's no way to resize those in xml and they don't have a drawableCenter option.
Fancied answered 13/10, 2021 at 23:41 Comment(2)
I'm not sure I understand but I think what you're trying to do directly contradicts guidelines from the Material Design so you really should just make your own implementation of a Button and not use the MaterialButton class. It's written specifically in a way which enforces you to abide by the design rules.Cheetah
you can use constraints only inside ConstraintLayoutMalarkey

© 2022 - 2024 — McMap. All rights reserved.