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
I don't need to any custom views or hard coded things. If this is a bug (app:iconGravity), I will wait next release.
The bug fixed in the version 28.0.0-rc01,just change the version.
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.
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.
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"
../>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With