Icon is not showing when using app:srcCompat android MaterialButton instead of app:icon

679 views Asked by At

I am using MaterialButton and using app:srcCompat but unfortunately icon is not showing. Tried both vector drawables and webp images but nothing work.

  <com.google.android.material.button.MaterialButton
                android:id="@+id/facebookButton"
                style="@style/ButtonS.Normal"
                android:layout_width="match_parent"
                android:layout_height="@dimen/spacing_48"
                android:layout_marginTop="@dimen/spacing_8"
                android:background="@drawable/bg_blue_round_corner"
                android:gravity="center"
                android:text="@string/welcome_screen_continue_with_facebook"
                app:srcCompat="@drawable/ic_facebook"
                app:iconGravity="textStart"
                app:iconPadding="7dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

Does anyone have any idea why it's not working?

Note: I must need to use app:srcCompat instead of app:icon

Since getting a random exception and I suspect its because of using app:icon Details stacktrace:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.client/com.android.onboarding.welcome.ui.WelcomePageActivity}: android.view.InflateException: Binary XML file line #264 in com.android.client:layout/activity_landing_page: Binary XML file line #264 in com.android.client:layout/activity_landing_page: Error inflating class <unknown>
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3477)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3620)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2183)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:241)
       at android.app.ActivityThread.main(ActivityThread.java:7617)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

And XML line 264 where the exception happening:

<com.google.android.material.button.MaterialButton
.......
app:layout_constraintTop_toBottomOf="@+id/emailButton" /> // Line 264
1

There are 1 answers

9
Gabriele Mariotti On

Use the app:icon attribute:

    <com.google.android.material.button.MaterialButton
        app:icon="@drawable/ic_facebookx"/>

The MaterialButton doesn't use the app:srcCompat.