Use different style for same item for different activity in android studio (Please read description)

353 views Asked by At

I am working on a project wherein I want to use BottomSheetDialog in different activities. But for different activities I want the Dialog to have diffrent background drawable. I have set the background drawable in themes.xml but that makes the drawable to be applicable for BottomSheetDialog for the entire app, i.e, it gets applied successfully but it is constant for all activities. What I want is for diffrent activites it must have different background drawable.

This is what my themes.xml looks like:

<resources xmlns:tools="http://schemas.android.com/tools">
   <style>
    <!-- Base application theme. -->
         ...
         ...
        <!-- Customize your theme here. -->

        <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>

    </style>

    <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/top_rounded_corners</item>  <!-- I want to change the backgroundDrawable here. -->
    </style>
</resources>

Is there a way I can change drawable depending on the activity I am on. I thought of using AndroidManifest.xml but I am not sure how can I achieve this. Any help is appreciated. Thank You.

1

There are 1 answers

0
SIDDHARTH SINGH On BEST ANSWER

I found an alternative way to solve my problem: I created another instance of themes in themes.xml and then change the drawable asset in this instance (see the code below).

<style name="Theme.Blogaro" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

        <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>

    </style>

    <style name="Theme.Blogaro.splash" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

        <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme.splash</item>  ---> Made changes here

    </style>

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

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

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

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

Then I programmatically change the default theme inside the onCreate in my Activity.java:

        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Blogaro_splash);
        setContentView(R.layout.activity_splash_screen);