Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing icon on top of text in MaterialButton

I wanted to put the icon on top of the materialButton like shown here: enter image description here

But I couldn't find any properties in MaterialButton to set the gravity of the app:icon to top. I could only able to set it to start or end. Is there any way to set the create a similar button without using Vertical LinearLayout with ImageView and TextView?

like image 392
Jazib Khan Avatar asked Sep 03 '25 09:09

Jazib Khan


2 Answers

You can use the iconGravity="textTop".

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

enter image description here

Note: It requires at least the version 1.3.0-alpha02.

like image 132
Gabriele Mariotti Avatar answered Sep 04 '25 23:09

Gabriele Mariotti


You can use android:drawableTop attribute for this

Note : This is not only restricted for the MaterialButton you can use it on TextViews and other views also

Example Code :

<com.google.android.material.button.MaterialButton
    android:layout_width="match_parent"
    android:text="@string/app_name"  <!-- Your Text -->
    android:drawableTop="@drawable/ic_launcher_background" <!-- Your Icon -->
    android:layout_height="wrap_content"/>
like image 21
AgentP Avatar answered Sep 04 '25 23:09

AgentP