Using Material Button Gives Classnotfound Runtime Error
Solution 1:
As it suggest HERE you have to add a dependency in your build.gradle:
implementation 'com.google.android.material:material:1.0.0-beta01'
Or if you already use google support design library you must change your app theme to inherit from a Material Components theme
<stylename="Theme.MyApp"parent="Theme.MaterialComponents.Light"><!-- ... -->
If you cannot change your theme to inherit from a Material Components theme, you can inherit from a Material Components Bridge theme.
<stylename="Theme.MyApp"parent="Theme.MaterialComponents.Light.Bridge"><!-- ... -->
How Reyske said:
note that it is out of beta now! So you can use implementation 'com.google.android.material:material:1.0.0'
Solution 2:
In my opinion, the best choice is to create a separate own style to the material button where it extends the material theme:
<stylename="MatButton"parent="Theme.MaterialComponents"><itemname="colorOnPrimary">@android:color/holo_red_light</item><itemname="colorPrimary">@android:color/holo_orange_dark</item><itemname="colorOnSurface">@android:color/holo_orange_light</item></style><com.google.android.material.button.MaterialButtonandroid:id="@+id/searchBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Search"android:textColor="@android:color/white"android:textAppearance="@style/TextAppearance.MaterialComponents.Button"android:theme="@style/MatButton"
/>
Solution 3:
When crashing The following message was output to Logcat.
Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
Therefore, you can also display MaterialButton by adding TextAppearance.
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
Solution 4:
if you are using material theme component in your app, then try this.
make changes in your styles.xml from before to after ->
before:
<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark">@color/colorPrimaryDark</item><itemname="colorAccent">@color/colorAccent</item></style></resources>
after:
<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.MaterialComponents.Light.DarkActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark">@color/colorPrimaryDark</item><itemname="colorAccent">@color/colorAccent</item></style></resources>
Solution 5:
Try to update your app theme to inherit from Theme.MaterialComponents
(or a descendant)
Post a Comment for "Using Material Button Gives Classnotfound Runtime Error"