This is a migrated thread and some comments may be shown as answers.

Cell Decorator for a specif cell

4 Answers 110 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Fabio
Top achievements
Rank 1
Fabio asked on 15 May 2015, 01:30 AM

Hello

 I am trying to decorate a specif cell, I have only achieve this to aply this decorator when the cell is tabbed/clicked, but How I apply a decorator to specif cells only.

 I am using this decorator:

CellDecorator decorator = new CircularCellDecorator(calendarView);
decorator.Color = (Android.Graphics.Color.ParseColor("#ed742c"));
decorator.StrokeWidth = 3;
decorator.Scale = .75f;
 
calendarView.CellDecorator = decorator;

Also can I fill the circle with the color?

I can see that there is a function called:

calendarView.CellDecorator.ToggleDecorationForCell();

But I need to pass a CalendarCell which is an Abstract class. 

4 Answers, 1 is accepted

Sort by
0
Fabio
Top achievements
Rank 1
answered on 15 May 2015, 03:16 AM

I may have found a way but nothing is being rendered, here is my code:

CustomCalendarCell test = new CustomCalendarCell(calendarView);
test.Date = DateTime.Now.Ticks;
 
calendarView.CellDecorator.ToggleDecorationForCell(test);
 
public class CustomCalendarCell : CalendarCell
{
     public CustomCalendarCell(RadCalendarView p0) : base(p0) { }
}

Any Ideas?

0
Antony Jekov
Telerik team
answered on 20 May 2015, 08:42 AM
Hello Fabio,

Thank you for contacting the Android team.

You are going in the right direction. The cell decorators only trigger when the cell is selected. The other option for customization without extensions is the custom rules, but they still cannot provide the effect you are looking for.

In your case you would like to extend the CalendarDayCell and add logic to its render method. I have prepared a simple implementation by following your request:
[Activity (Label = "App", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
 
            RadCalendarView calendarView = new RadCalendarView (this);
            calendarView.Adapter = new CustomAdapter (calendarView);
            calendarView.Adapter.Style = CalendarStyles.MaterialLight (this);
            calendarView.Adapter.DateTextPosition = CalendarElement.Center;
 
            SetContentView (calendarView);
        }
 
        internal class CustomAdapter : CalendarAdapter {
             
            public CustomAdapter(RadCalendarView owner) : base(owner){}
 
            protected override CalendarDayCell GenerateCalendarDayCell ()
            {
                return new DayCell (Owner);
            }
 
            internal class DayCell : CalendarDayCell {
 
                private readonly Paint paint;
                private int radius;
 
                private int halfHeight;
                private int halfWidth;
 
                public DayCell(RadCalendarView owner) : base(owner){
                    this.paint = new Paint(PaintFlags.AntiAlias);
                    this.paint.Color = Color.LightSteelBlue;
                }
 
                public override void Arrange (int p0, int p1, int p2, int p3)
                {
                    base.Arrange (p0, p1, p2, p3);
 
                    radius = (int)(System.Math.Min(Width, Height) * .8f) / 2;
                    halfWidth = Width / 2;
                    halfHeight = Height / 2;
                    paint.StrokeWidth = System.Math.Min (Width, Height) * .05f;
                }
 
                public override void Render (Canvas canvas)
                {
                    base.Render (canvas);
 
                    if (this.CellType == CalendarCellType.Date) {
                        canvas.DrawCircle (Left + halfWidth, Top + halfHeight, radius, paint);
                        canvas.DrawText (Text, TextPositionX(), TextPositionY(), TextPaint);
                    }
                }
            }
        }
    }

This will produce the result you are looking for.

1. Check if the cell is a date cell (not a day name cell).
2. Render the circle.
3. Render the text above the circle using the text coordinates and the text paint.

Please let me know if this suits you purposes.

Thank you for your time and all best!

Regards,
Antony Jekov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Fabio
Top achievements
Rank 1
answered on 21 May 2015, 11:36 PM

Hello Thanks so much for the response. Is there a way to change the color and the size of the text?

I have tried this:

public DayCell(RadCalendarView owner) : base(owner){
rnd = new System.Random();
paint = new Paint(PaintFlags.AntiAlias);
switch (GetRandomBoolean())
{
 case 0:
paint.Color = Android.Graphics.Color.White;
paint.TextSize = 16;
//TextSize = 16;
break;
 
case 1:
paint.Color = Android.Graphics.Color.Rgb(235,124,93);
paint.TextSize = 16;
//TextColor = Android.Graphics.Color.White;
//TextSize = 16;
break;
 
case 2:
paint.Color = Android.Graphics.Color.Rgb(64,158,186);
paint.TextSize = 16;
//TextColor = Android.Graphics.Color.White;
//TextSize = 16;
break;
}
}

0
Antony Jekov
Telerik team
answered on 25 May 2015, 01:39 PM
Hi Fabio,

You can use a customization rule for changing the existing properties of the cell. You will find it very difficult to try and use another way. Here is the sample code:
    class Customization : Java.Lang.Object, IProcedure {
    int[] colors = {Color.Red, Color.Green, Color.Blue, Color.DarkRed, Color.AntiqueWhite};
    System.Random random = new System.Random ();
 
    public void Apply (Java.Lang.Object cell)
    {
        try {
            CalendarDayCell dayCell = cell.JavaCast<CalendarDayCell> ();
            int color = colors[random.Next(colors.Length)];
            dayCell.SetTextColor(color, color);
        } catch (System.Exception ex) {
             
        }
    }
}
 And setting this to the calendar would look like this:
calendarView.CustomizationRule = new Customization ();
 I hope this is what you are looking for.

Regards,
Antony Jekov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Calendar
Asked by
Fabio
Top achievements
Rank 1
Answers by
Fabio
Top achievements
Rank 1
Antony Jekov
Telerik team
Share this question
or