In your .xml layout, changing one line to the type of control (changing from a button to a custom button type) will solve your cast problem.
For your BuyButton subclass, find the button section in .xml; it might look something like this:
<Button android:id="@+id/btnBuy" android:layout_width="188dp" android:layout_height="70dp" android:padding="12dp" android:text="Buy" />
and change it to this:
<yourpackage.name.BuyButton android:id="@+id/btnBuy" android:layout_width="188dp" android:layout_height="70dp" android:padding="12dp" android:text="Buy" />
Joe seibert
source share