0

Recently, we moved to use the material design theme for our app (we are using version 1.4.0). As part of this, we are converting explicit <com.google.android.material.button.MaterialButton declarations to <Button since Button will be converted to MaterialButton behind the scene. Everything else works fine except in the following case.

We have a <com.google.android.material.button.MaterialButton in a FrameLayout as shown below

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <Button
      ...
      ...
    />
</FrameLayout>

and it's being used as a custom view via view binding something like below

class MyView: FrameLayout {

private val binding by ViewGroupBindingProperty(MyViewBinding::inflate)

    constructor(...)
    /// other code
}

When I tried to convert above <com.google.android.material.button.MaterialButton to <Button, style is not applied (we have border color, strokeColor, etc applied as style for this button in xml) so I tried to cast this button as MaterialButton in MyView and tried to apply those style properties but it's throwing following error

Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to com.google.android.material.button.MaterialButton
        at jp.ne.paypay.android.app.view.paymentMethod.MyView.init(MyView.kt:51)
        at xx.xx.xx.xx.MyView.<init>(MyView.kt:29)

Any guidance on what could be wrong is highly appreciated. Thank you in advance.

Braj
  • 2,164
  • 2
  • 26
  • 44
  • I hope [this](https://stackoverflow.com/a/53924244/11102027) link will help you – Rustam Oct 18 '21 at 07:34
  • @Rustam Thanks but I am not looking to create the button programmatically. I already have the button in XML. It's throwing the exception while inflating and using it – Braj Oct 18 '21 at 08:19

2 Answers2

0

I think you need to specify the Button in your xml as com.google.android.material.button.MaterialButton and not as plain Button although that should be done by the layout inflater when using material style

binarynoise
  • 364
  • 3
  • 14
0

@braj Please provide me your full code , then you are asking why

<Button declaration is not working in my case

Because Button is part of Android SDK , you are using Material Button that part of Material Design they are similar but Material Button have a lot of advantages , you can write beautiful code with Material Design

In your case you must implement view as Material Button not just button

Rustam
  • 71
  • 1
  • 6
  • I have updated the question with more details. Please take a look. When you use the AppCompat theme, you don't have to specifically use AppCompatButton, AppCompatTextView, etc. You just need to specify the Button and it will automatically be converted into AppCompatButton behind the scene. Similarly, when you use the MaterialCompat theme, you don't need to specify MaterialButton, MaterialTextView, etc. Just mentioning the Button should be enough. I understand and am aware of the advantages of MaterialButton. All I want to know is why it's not working in the case mentioned above. – Braj Oct 19 '21 at 05:23
  • So , I wanna explain with example ) Here MaterialButton is wrapper for class Button (that means MaterialButton class is child of Button class and Button class is Parent class for MaterialButton) So , MaterialButton have a lot of wrapper functions but parent class Button did not have because Button did not extends MaterialButton So , You can not cast Button class to MaterialButton and conversely... You can create class which whose extends MaterialButton – Rustam Oct 20 '21 at 10:21
  • If I answered your questions please mark as answered ,thank you – Rustam Oct 20 '21 at 10:28