Xamarin.Android | ComponentOne
Controls / Calendar / Features / Customizing Day Content
In This Topic
    Customizing Day Content
    In This Topic

    The C1 Calendar control allows users to add custom content to day slot. For this, all you need to do is subscribe to the DaySlotLoading event and apply custom content such as images in the background of these slots. This feature allows users to display weather related information on the calendar.

    The image below shows a Xamarin Calendar after adding custom content to day slots. The calendar displays weather related information through various icons.

    The following code example demonstrates how to add custom content to day slots on the calendar using C#. This example uses the sample created in Quick Start section.

    Add the image icons as resource files at YourProject\Resources\drawable location in your VisualStudio project. By default, you can find these images at C:\<User>\Documents\ComponentOne Samples\Xamarin\Android\C1Calendar101\Resources\drawable

    In Code

    Complete the following steps to add custom content to day slots.

    MainActivity.cs
    Copy Code
    public class MainActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                ActionBar.SetDisplayHomeAsUpEnabled(true);
                SetContentView(Resource.Layout.CustomDayContent);
                var calendar = FindViewById<C1Calendar>(Resource.Id.Calendar);
                calendar.DaySlotLoading += OnDaySlotLoading;
            }
            private void OnDaySlotLoading(object sender, CalendarDaySlotLoadingEventArgs e)
            {
                // add weather image for certain days in the current month
                if (e.Date.Year == DateTime.Today.Year &&
                    e.Date.Month == DateTime.Today.Month &&
                    e.Date.Day >= 14 && e.Date.Day <= 23)
                {
                    var slot = LayoutInflater.Inflate(Resource.Layout.DaySlot, null);
                    var iv = slot.FindViewById<ImageView>(Resource.Id.Image);
                    var tv = slot.FindViewById<TextView>(Resource.Id.Day);
    
                    tv.Text = e.Date.Day.ToString();
    
                    switch (e.Date.Day % 5)
                    {
                        case 0:
                            iv.SetImageResource(Resource.Drawable.Cloudy);
                            break;
                        case 1:
                            iv.SetImageResource(Resource.Drawable.PartlyCloudy);
                            break;
                        case 2:
                            iv.SetImageResource(Resource.Drawable.Rain);
                            break;
                        case 3:
                            iv.SetImageResource(Resource.Drawable.Storm);
                            break;
                        case 4:
                            iv.SetImageResource(Resource.Drawable.Sun);
                            break;
    
                    }
                    e.DaySlot = slot;
                }
    
            }
    
            public override bool OnOptionsItemSelected(IMenuItem item)
            {
                if (item.ItemId == global::Android.Resource.Id.Home)
                {
                    Finish();
                    return true;
                }
                else
                {
                    return base.OnOptionsItemSelected(item);
                }
            }
        }