In the activation of my application there is a list of events consisting of tasks. Tasks are separated by a separator, represented as a RelativeLayout.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="27dp" android:text="Separator" android:gravity="center_horizontal|center_vertical" android:paddingLeft="16dp" android:textSize="16sp" android:id="@+id/tvSeparatorName"/> </RelativeLayout> 

I implemented a change of themes in the application. There was a difficulty in changing the color of the separator - in the light theme there should be one shade, in the dark one another.

  <color name="backgroundSeparatorLight">#e4f062</color> <color name="backgroundSeparator">#e16d07</color> <resources> <style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primaryLight</item> <item name="colorPrimaryDark">@color/primaryDarkLight</item> <item name="colorAccent">@color/accentLight</item> <item name="background">@color/backgroundSeparatorLight</item> </style> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primaryDark</item> <item name="colorAccent">@color/accent</item> <item name="background">@color/backgroundSeparator</item> </style> </resources> 

How to set the separator background correctly? I would be grateful for the help.

    1 answer 1

    It is necessary in the attr file to set your custom attribute, which will be responsible for the color. For example, the attribute will be as follows:

     <attr name="separator_color" format="color"/> 

    Then you specify the value of this attribute in your topics:

     <resources> <style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primaryLight</item> <item name="colorPrimaryDark">@color/primaryDarkLight</item> <item name="colorAccent">@color/accentLight</item> <item name="separator_color">@color/backgroundSeparatorLight</item> </style> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primaryDark</item> <item name="colorAccent">@color/accent</item> <item name="separator_color">@color/backgroundSeparator</item> </style> </resources> 

    Now you can set the desired color depending on the topic. The important point is to indicate where to use this color. Example:

     <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="27dp" android:text="Separator" android:gravity="center_horizontal|center_vertical" android:paddingLeft="16dp" android:textSize="16sp" android:textColor="?separator_color" android:id="@+id/tvSeparatorName"/> </RelativeLayout> 

    where the android: textColor = "? separator_color" attribute is substituted from your theme.