badoualy / datepicker-timeline

An infinite scrolling timeline to pick a date
MIT License
463 stars 72 forks source link

how do I set color of the label #11

Open shahimclt opened 6 years ago

shahimclt commented 6 years ago

The GIF in the readme shows labels with different colors:

demo

but I don't see any mention of how to do that. Please advise.

shahimclt commented 6 years ago

I dug through the code a little bit and I think adding a new method getLabelColor to DateLabelAdapter will do the trick. @badoualy what do you think?

badoualy commented 6 years ago

If memory serves, for this case I used CharSequence with ForegroundColorSpan I'll check for the label color, I'll add when I have some time (or you can if you want to do a PR).

shahimclt commented 6 years ago

Wow. I didn't know you could do that with a CharSequence. So thanks a lot for that.

This is what I am going with :

public CharSequence getLabel(Calendar calendar, int index) {
  SpannableStringBuilder builder = new SpannableStringBuilder();
  String red = "full";
  SpannableString redSpannable= new SpannableString(red);
  redSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, red.length(), 0);
  builder.append(redSpannable);
  return builder;
}

So now I suppose there is no need for a PR. Maybe you can include this snippet in the readme so that others can benefit.

badoualy commented 6 years ago

Yeah, but using span if you're always using the same color is not the best idea, complicates life for nothing :) Tip, following code is simpler/more optimized:

SpannableString span = new SpannableString("full");
span.setSpan(new ForegroundColorSpan(Color.BLUE), 0, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
return span;
shahimclt commented 6 years ago

Yes, I did that. The code I posted was a generic one lifted from Stack Overflow. I am using the simpler version in my project. Thanks.