Search code examples
androidxmlandroid-layoutandroid-theme

how to change background color by changing the theme in android?


I want to change the background color when I change the theme in android. Is there a way to set the background in XML like

android:background="@styles/colorAccent"

so when I change the theme the background color automatically changes to the colorAccent of the respective theme. Any alternate approach would also help.


Solution

  • Sample layout

     <FrameLayout
        style="@style/colorAccentStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content/>
    

    styles.xml

    <resources>
    
    <style name="colorAccentStyle">
        <item name="android:background">?colorAccent</item>
    </style>
    

    Simpler solution

    <FrameLayout
        android:background="?colorAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>