savvisingh / DateRangePicker

Date Range Picker is a Calendar Picker View to show a Customized Date Range Picker with improved UI and functionality to add subtitles to the dates
Apache License 2.0
784 stars 160 forks source link

How to set two different colour to the month name and selected date name ? #55

Closed ArunVicky001 closed 5 years ago

ArunVicky001 commented 5 years ago

In my case I have two different calendar when I click the first calendar the months name and selected date colour should be blue and when I click the second calendar the months name and selected date colour should be displayed in orange colour.

I have checked your library below two lines are responsible for the months name and selected date colour, also i have prepared two different drawable file showing the colours

dayBackgroundResId = a.getResourceId(R.styleable.CalendarPickerView_tsquare_dayBackground, R.drawable.calendar_bg_selector); titleTextColor = a.getColor(R.styleable.CalendarPickerView_tsquare_titleTextColor, res.getColor(R.color.dateTimeRangePickerTitleTextColor));

So I have created the separate method for call this CalendarPickerView CalendarPickerView(Context context, AttributeSet attrs)

during the runtime I am passing the boolean value based on the value different colour has been assigned to the dayBackgroundResId and titleTextColor like below,

public CalendarPickerView(Context context, AttributeSet attrs) {
super(context, attrs);
attributeSet = attrs;

Resources res = context.getResources();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CalendarPickerView);
final int bg = a.getColor(R.styleable.CalendarPickerView_android_background,
    res.getColor(R.color.calendar_bg));
dividerColor = a.getColor(R.styleable.CalendarPickerView_tsquare_dividerColor,
    res.getColor(R.color.calendar_divider));
dayTextColorResId = a.getResourceId(R.styleable.CalendarPickerView_tsquare_dayTextColor,
    R.drawable.day_text_color);

CalendarTheme calendarTheme = new CalendarTheme();

if (calendarTheme.isFromCalendar()) {
  Log.d("WATCH_APP", "-----> true");
  // TODO: Date Selected Colour change
  dayBackgroundResId = a.getResourceId(R.styleable.CalendarPickerView_tsquare_dayBackground,
          R.drawable.calendar_bg_selector);
  // TODO: Month Title Colour change
  titleTextColor = a.getColor(R.styleable.CalendarPickerView_tsquare_titleTextColor,
          res.getColor(R.color.lineBlue));
}
else {
  Log.d("WATCH_APP", "-----> false");
  // TODO: Date Selected Colour change
  dayBackgroundResId = a.getResourceId(R.styleable.CalendarPickerView_tsquare_dayBackground,
          R.drawable.calendar_bg_selector_orange);
  // TODO: Month Title Colour change
  titleTextColor = a.getColor(R.styleable.CalendarPickerView_tsquare_titleTextColor,
          res.getColor(R.color.darkyellow));
}
displayHeader = a.getBoolean(R.styleable.CalendarPickerView_tsquare_displayHeader, true);
headerTextColor = a.getColor(R.styleable.CalendarPickerView_tsquare_headerTextColor,
    res.getColor(R.color.dateTimeRangePickerHeaderTextColor));

if (colourCode == "blue") {
  Log.d("colour code", "Colour code is blue");
}
else {
  Log.d("colour code", "Colour code is not blue");
}

a.recycle();

adapter = new MonthAdapter();
setDivider(null);
setDividerHeight(0);
setBackgroundColor(bg);
setCacheColorHint(bg);
timeZone = TimeZone.getDefault();
locale = Locale.getDefault();

if (isInEditMode()) {
  Calendar nextYear = Calendar.getInstance(timeZone, locale);
  nextYear.add(Calendar.YEAR, 1);

  init(new Date(), nextYear.getTime()) //
      .withSelectedDate(new Date());
}
}

the method is calling fine in the runtime but the colour is not changing to the month name and selected date.

Could you please tell me how to achieve my requirement ?

Thanks.

savvisingh commented 5 years ago

The calendar_bg_selector resource is a state selector file which has different colors for different states, For your case you can create two different selector files and pass them in xml as an attr app:tsquare_dayBackground to your calendarpicker views Please have a look at calendar_bg_selector.xml from rangepicker module to create your own custom selector files

ArunVicky001 commented 5 years ago

Thanks you very much for your library and support, I have solved this issues with different selector_bg.xml and made changes in this method in @Override public View getView(int position, View convertView, ViewGroup parent) { based on my condition now its working perfect.

savvisingh commented 5 years ago

Happy to help 👍