Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set materialButton icon to the center

I am using supportLibrary = "28.0.0-beta01" version.
Here is my code in .xml file:

<android.support.design.button.MaterialButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:icon="@drawable/ic_my_orders"
    app:iconGravity="textStart"
    android:gravity="center"/>

To my code icon of the drawable locating left side of the button. I want to set button to the center. I want to achieve this result enter image description here


Edit

I don't need to any custom views or hard coded things. If this is a bug (app:iconGravity), I will wait next release.

EDIT

The bug fixed in the version 28.0.0-rc01,just change the version.

like image 961
Muhammadakbar Rafikov Avatar asked Aug 01 '18 12:08

Muhammadakbar Rafikov


People also ask

How can set image in center of button in Android?

Create a RelativeLayout "wrap_content" with the button image as the background or the button itself as the first element of the layout. Get a LinearLayout and set it to "layout_centerInParent" and "wrap_content". Then set your Drawable as an Imageview.


2 Answers

The code snippet you have in your original question is correct. This has been identified as a bug and will be fixed in the upcoming release.

like image 62
Gautham Sajith Avatar answered Sep 20 '22 21:09

Gautham Sajith


With the Material Components library just use the app:iconGravity="textStart" attribute:

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

enter image description here

If you want to cut the corners use the app:shapeAppearanceOverlay attribute:

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

with:

  <style name="Button.Cut" parent="">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">4dp</item>
  </style>

enter image description here

like image 35
Gabriele Mariotti Avatar answered Sep 21 '22 21:09

Gabriele Mariotti