datepicker-timeline icon indicating copy to clipboard operation
datepicker-timeline copied to clipboard

how do I set color of the label

Open shahimclt opened this issue 8 years ago • 5 comments

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 avatar Nov 02 '17 13:11 shahimclt

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?

shahimclt avatar Nov 02 '17 13:11 shahimclt

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).

badoualy avatar Nov 02 '17 13:11 badoualy

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.

shahimclt avatar Nov 02 '17 14:11 shahimclt

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;

badoualy avatar Nov 02 '17 14:11 badoualy

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.

shahimclt avatar Nov 02 '17 14:11 shahimclt