itsdrnoob
itsdrnoob

Reputation: 5

Cannot change Button background after applying BottomSheetDialogFragment background

I needed rounded corners on my BottomSheetDialogFragment so I applied a custom drawable as its background. But after applying a custom background the button on the BottomSheet is not accepting a custom background. It just displays a white background. backgroundTint works fine and changes the color of the button.

themes.xml :

<style name="AppBottomSheetDialogTheme"
    parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>

<style name="AppModalStyle"
    parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/add_new_bg</item>
</style>

add_new_bg.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="@color/background"/>
    <corners
        android:topLeftRadius="40dp"
        android:topRightRadius="40dp"/>
</shape>

BottomSheetDialogFragment code :

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.AppBottomSheetDialogTheme);
    }

Upvotes: 0

Views: 1767

Answers (2)

Rajkumar
Rajkumar

Reputation: 109

See if you want to apply only background color to a button in bottom sheet Just use background tint "YOUR_COLOR_CODE" in your code

Upvotes: 0

xfathurrahman
xfathurrahman

Reputation: 81

Try this :

  1. if your main theme is Theme.AppCompat.Light :

example :

<style name="Theme.YourAppName" parent="Theme.AppCompat.Light">
    <!-- Primary brand color. -->
    <!-- Customize your theme here. -->
</style>

and your button is :

<Button <------ material button type
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/buttonTheme" <---- add this
    android:text="MyButton"/>

add this in your themes.xml below main theme

    <style name="buttonTheme" parent="Theme.MaterialComponents.Light">
       <item name="theme">@style/buttonTheme</item>
       <item name="android:textColor">@color/white</item>
       <item name="android:background">@color/black</item>
   </style>
  1. if your main theme is : Theme.MaterialComponents

example :

<style name="Theme.RecyclerView" parent="Theme.MaterialComponents.Light">
    <!-- Primary brand color. -->
    <!-- Customize your theme here. -->
</style>

but your button use :

<androidx.appcompat.widget.AppCompatButton <<<<--- appcompact button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/buttonTheme" 
        android:text="MyButton"/>

then add :

<style name="buttonTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="theme">@style/buttonTheme</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@color/black</item>
</style>

themes with button type must be same type.

let me know if this works for you.. and upvote it :)

Upvotes: 2

Related Questions