Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

Alternating Row at Expand RecyclerView

Hi All,

I use Android Studio.
I want to have set color for alternating row.
How could I do it ?

Thank you.
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Under file->settings->editor->code style is a list of rules on setting up your project. Not sure if what you want is doable though.
Avatar of emi_sastra
emi_sastra

ASKER

Hi Chris,

Usually setting alternation (odd and event row) color, we should set it using code, not by setting.

Are your sure of your solution ?

Thank you.
Are you talking about an app or inside the IDE? Because I thought you were talking about the IDE.
Hi Chris,

I am talking about ExpandableRecyclerView.

Thank you.
A bit of simple maths in your onBindViewHolder, eg

@Override public void onBindViewHolder(View_holder holder, int position)
{
     if(position % 2 == 0) 
     {
         //Set to your desired colour
         holder.rootView.setBackgroundResource(Color.black);
     }
     else 
     {      
         //Your alternate colour
         holder.rootView.setBackgroundResource(Color.white);
     }
}

Open in new window

Hi Chris,

Great. Let me try it first.

Thank you.
Hi Chris,

It works, but I am looking for using style.

Thank you.
If you mean using the xml file in the layout editor, I don't think that is possible. As far as I am aware it has to be in code.
Yes, it is using code.
But I don't want to hard code the color.

  holder.rootView.setBackgroundResource(Color.white);

Color.white change to style at global variable.

Thank you.
You can store values such as these via preferences

https://developer.android.com/guide/topics/ui/settings.html

These values are stored and retrieved using preferenceFragments or a preferenceActivity.
Hi Chris,

- You can store values such as these via preferences
The problem is not the preferences.

-   holder.rootView.setBackgroundResource(Color.white);
We can not change  "Color.ColorName" to something else. Isn't it?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Chris,

Could we use style file for the color ?

Thank you.
Hi Chris,

Thank you very much for your help.

I understand that it can not use syle file.