Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Material Button in XML causing app to crash

When I add the following code to the layout.xml file, the app crashes, when the layout is loaded.

Code:

<android.support.design.button.MaterialButton
    android:id="@+id/b_p_add"
    android:layout_width="143dp"
    android:layout_height="38dp"
    android:layout_below="@+id/et_p_addl_notes"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="43dp" />

I checked my app gradle file and found

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

So, the required design, support, appcompat dependencies are present.

Note: No other code apart from the above has been added, i.e, the app works fine if I don't add the Material button in XML and crashes, if I add the Material Button in the XML.

like image 896
user3760100 Avatar asked Dec 24 '22 03:12

user3760100


1 Answers

Please change your app theme from:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

to

<style name="AppTheme" parent="Theme.MaterialComponents.Light">

for MaterialButton to work, your app theme must inherit from Theme.MaterialComponents (or a descendant) Hope it helps.

like image 79
Shahid Anjum Avatar answered Dec 26 '22 00:12

Shahid Anjum