1

While I was able to set a custom color for a Layout, inside a RecyclerView.Adapter using

viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.color_theme_1));

I am unable to set the following code

viewHolder.mLayout.setBackgroundColor(R.attr.colorLayoutBackground);

OR

viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context,R.attr.colorLayoutBackground));

using the attr:color value. What am I doing wrong?

My current code is as follows - - attrs.xml

    </resources>
  <!-- Color -->
    <attr name="colorLayoutBackground" format="color"/>
   </resources>

colors.xml

    <!-- Theme One Color -->
<color name="color_theme_1">#F8BBD0</color>
    <!-- Theme Two Color -->
<color name="color_theme_2">#C8E6C9</color>

styles.xml

<style name="AppTheme.Base.Light"
    parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Theme One -->
    <item name="colorLayoutBackground">@color/color_theme_1</item>
</style>
<style name="AppTheme.Base.Light"
    parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Theme Two -->
    <item name="colorLayoutBackground">@color/color_theme_2</item>
</style>

I want to dynamically set two sets of color to Linear_Layout on a list item (using recycleView), on theme change/s, but somehow am unable to get through.

The following code works, when no theme is applied, and i get three different colors on each item on a list.

if(position % 3 == 0){
                viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.color_one));
            }else if(position % 3 == 1){
                viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.color_two));
            }else if(position % 3 == 2){
                viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.color_three));
            }

I want to add another set of color, through themes and change it, as I change my theme. Any/All help and suggestions are greatly appreciated.

Prashant
  • 61
  • 1
  • 1
  • 9
  • https://stackoverflow.com/a/42986076/4168607 – ADM Feb 25 '19 at 12:03
  • You can maintain status of Theme using some `boolean` variable. Based on Theme set that variable value to true. and based on that value set the color of background. – Satyam Gondhale Feb 25 '19 at 12:07

0 Answers0