Search code examples
androidmaterial-designandroid-buttonmaterial-components-androidmaterial-components

drawableLeft/Right etc. doesn't work with Theme.MaterialComponents


When using theme for Activity Theme.MaterialComponents.... if I want to use drawableLeft/Right etc. with a button they are not shown. But if I use theme Base.Theme.AppCompat they worked as expected. Here is Button:

       <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test Button"
            android:drawableLeft="@drawable/ic_envelope"/>

And this is how it looks with theme Base.Theme.MaterialComponents enter image description here

And this is with Base.Theme.AppCompat enter image description here

How can I make drawableLeft work with Base.Theme.MaterialComponents theme?


Solution

  • Just use a MaterialButton with the app:icon attribute:

    <com.google.android.material.button.MaterialButton
          style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
          app:icon="@drawable/ic_add_24px"
          ../> 
    

    Screenshot of an outlined button with an icon

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/ic_add_24px"
        ../>
    

    Screenshot of a filled button with an icon

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
        app:icon="@drawable/ic_add_24px"
        ../>
    

    Screenshot of a borderless button with an icon

    Use the app:iconGravity to config start|end|textStart|textEnd.

    <com.google.android.material.button.MaterialButton
        app:iconGravity="end"
        ../>
    

    enter image description here