Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawable start not working with material button

Here is the button :

    <Button
    android:id="@+id/btn_choose_photo"
    style="@style/Widget.MaterialComponents.Button.TextButton.Dialog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableStart="@drawable/ic_camera"
    android:drawablePadding="8dp"
    android:text="@string/image_picker_dialog_choose_image"
    android:textAlignment="textStart" />

I am using material themes, so this will be inflated into a material button drawableStart has no effect at all, however drawableEnd, bottom and top work just fine When I make the button tag a text view, drawableStart works It seems like a bug or maybe I am missing something ?

Edit: My app theme is as follows :

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!---colors-->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryVariant</item>
    <item name="colorPrimaryDark">@color/colorPrimaryVariant</item>

    <item name="colorSecondary">@color/colorSecondary</item>
    <item name="colorSecondaryVariant">@color/colorSecondaryVariant</item>
    <item name="colorAccent">@color/colorSecondary</item>

    <!--
    <item name="android:colorBackground">@color/colorBackground</item>
    -->

    <!--components-->
    <item name="textInputStyle">@style/text_input_layout_style</item>
    <item name="bottomSheetDialogTheme">@style/bottom_sheet_dialog_theme</item>

    <item name="spinnerStyle">@style/spinner_style</item>
    <item name="android:spinnerStyle">@style/spinner_style</item>

    <item name="android:toolbarStyle">@style/toolbar_style</item>
    <item name="toolbarStyle">@style/toolbar_style</item>

    <item name="actionOverflowButtonStyle">@style/overflow_button_style</item>
    <item name="android:actionOverflowButtonStyle">@style/overflow_button_style</item>

</style>
like image 910
ahmed osama Avatar asked Apr 21 '20 21:04

ahmed osama


People also ask

What is MaterialButton in android?

MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.

What is the difference between Button and material button in Android Studio?

Attribute by attribute Material Buttons are slightly different to traditional Android buttons in that they do not include additional insets (4dp on the left/right) and have more letter-spacing, different default colors and other attributes that improve legibility and affordance amongst other components.


3 Answers

You should use app:icon like this:

In the layout:

<Button
   ...
   app:icon="@drawable/ic_camera"
   style="@style/Widget.MaterialComponents.Button.TextButton"
/>

It is displayed at the start, before the text label. You can change icon gravity, tint or size.

For more information

like image 152
Kasım Özdemir Avatar answered Oct 12 '22 05:10

Kasım Özdemir


Using

android:drawableLeft

will solve the problem, but will not give you RTL(Right to left) support. If your end user uses a different language which follow RTL, then your Button will not support it.

Are you managing the Button's property in the Activity or Fragment, as technically speaking,

android:drawableStart

should work.

like image 40
Karan Dhillon Avatar answered Oct 12 '22 04:10

Karan Dhillon


You can change to "App compat button"

enter image description here

<androidx.appcompat.widget.AppCompatButton
                android:id="@+id/continuar_anonimo_button"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_gravity="center"
                app:backgroundTint="@color/greenPrimary"
                android:background="@drawable/corner_boton_outline"
                android:text="Continuar anonimamente"
                android:paddingLeft="15dp"
                android:drawableStart="@drawable/ic_google_color"
                style="?android:textAppearanceSmall"/>
like image 2
Antony Nicolas Huaman Alikhan Avatar answered Oct 12 '22 04:10

Antony Nicolas Huaman Alikhan