Giter Site home page Giter Site logo

customcalendarview's Introduction

Logo CustomCalendarView .github/workflows/build.yml JitPack

Android custom calendar view - not quite forked from https://github.com/npanigrahy/Custom-Calendar-View

Calendar

The npanigrahy/Custom-Calendar-View library, although the best I could find, has several issues

  • It is slow starting, probably due to finding views using keys rather than ids
  • If used in a dialog it can be very wide, especially in landscape, possibly due to using android:layout_width="match_parent" rather than wrap_content
  • Uses a colour rather than a drawable for the selected entry background, so you can't use a rounded rectangle, for example, without providing a DayDecorator

This is an attempt to fix some of the above without breaking it. It all works as it should, but the calendar is about the same size as CalendarView.

Use in project

Add the driver to your build.gradle with

allprojects {
    repositories {
        jcenter()
        google()
        maven { url "https://jitpack.io" }
    }
}

and:

dependencies {
    implementation 'com.github.billthefarmer:CustomCalendarView:v1.05'
}

Using CustomCalendarDialog

Constructors

    public CustomCalendarDialog(Context context,
                                OnDateSetListener listener,
                                int year, int month, int date)

    public CustomCalendarDialog(Context context, int themeResId, 
                                OnDateSetListener listener, 
                                int year, int month, int date)

Listener

    public interface OnDateSetListener
    {
        public abstract void onDateSet (CustomCalendarView view,
                                        int year, int month, int date);
    }

Using

    // showCustomCalendar
    public void showCustomCalendarDialog(Calendar date)
    {
        CustomCalendarDialog dialog = new
            CustomCalendarDialog(this, this,
                                 date.get(Calendar.YEAR),
                                 date.get(Calendar.MONTH),
                                 date.get(Calendar.DATE));

        List<DayDecorator> decorators = new ArrayList<DayDecorator>();
        decorators.add(new EntryDecorator());
        CustomCalendarView calendarView = dialog.getCalendarView();
        calendarView.setDecorators(decorators);
        dialog.show();
    }

Using CustomCalendarView Library

The GitHub project source includes a simple test application. Once the library is added to your project, you can include the CustomCalendarView into your activity/fragment layout using the following code snippets.

<org.billthefarmer.view.CustomCalendarView
	android:id="@+id/calendar_view"
	android:layout_width="match_parent"
	android:layout_height="wrap_content" />

The above code snippet will show the simple Calendar View with the default design. You can use the following attributes to customize the appearance of the calendar.

<org.billthefarmer.view.CustomCalendarView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/off_white"
        app:calendarBackgroundColor="@color/off_white"
        app:calendarTitleTextColor="@color/black"
        app:currentDayOfMonthColor="@color/blue"
        app:dayOfMonthTextColor="@color/black"
        app:dayOfWeekTextColor="@color/black"
        app:disabledDayBackgroundColor="@color/off_white"
        app:disabledDayTextColor="@color/grey"
        app:selectedDayBackground="@[color|drawable]/[blue|selected]"
        app:titleLayoutBackgroundColor="@color/white"
        app:weekLayoutBackgroundColor="@color/white" />

Because the android build system merges library resources when it builds an app, you may replace the drawables in the library with your own by using the same names.

ic_action_back.png
ic_action_forward.png
ic_up.xml
ic_down.xml
ic_background.xml
selected.xml

Initialize the calendar view to control the behavior of the calendar using the following methods.

//Initialize CustomCalendarView from layout
calendarView = (CustomCalendarView) findViewById(R.id.calendar_view);

//Initialize calendar with date
Calendar currentCalendar = Calendar.getInstance(Locale.getDefault());

//Show Monday as first date of week
calendarView.setFirstDayOfWeek(Calendar.MONDAY);

//Show/hide overflow days of a month
calendarView.setShowOverflowDate(false);

//call refreshCalendar to update calendar the view
calendarView.refreshCalendar(currentCalendar);

//Handling custom calendar events
calendarView.setCalendarListener(new CalendarListener()
{
    @Override
    public void onDateSelected(Date date)
    {
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Toast.makeText(MainActivity.this, df.format(date), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onMonthChanged(Date date)
    {
        SimpleDateFormat df = new SimpleDateFormat("MM-yyyy");
        Toast.makeText(MainActivity.this, df.format(date), Toast.LENGTH_SHORT).show();
    }
});

Using Custom TypeFace

//Setting custom font
final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Arch_Rival_Bold.ttf");
if (typeface != null)
{
    calendarView.setCustomTypeface(typeface);
    calendarView.refreshCalendar(currentCalendar);
}

Using Day Decorators

//adding calendar day decorators
List decorators = new ArrayList<>();
decorators.add(new ColorDecorator());
calendarView.setDecorators(decorators);
calendarView.refreshCalendar(currentCalendar);

customcalendarview's People

Contributors

billthefarmer avatar tacothedank avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.