/Calendar

Primary LanguageJava

Custom-Calendar-View

Custom Android Calendar HomeCollection.date_collection_arr=new ArrayList(); HomeCollection.date_collection_arr.add( new HomeCollection("2017-07-08" ,"Diwali","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2017-07-08" ,"Holi","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2017-07-08" ,"Statehood Day","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2017-08-08" ,"Republic Unian","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2017-07-09" ,"ABC","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2017-06-15" ,"demo","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2017-09-26" ,"weekly off","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2018-01-08" ,"Events","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2018-01-16" ,"Dasahara","Holiday","this is holiday")); HomeCollection.date_collection_arr.add( new HomeCollection("2018-02-09" ,"Christmas","Holiday","this is holiday"));

    cal_month = (GregorianCalendar) GregorianCalendar.getInstance();
    cal_month_copy = (GregorianCalendar) cal_month.clone();
    hwAdapter = new HwAdapter(this, cal_month,HomeCollection.date_collection_arr);

    tv_month = (TextView) findViewById(R.id.tv_month);
    tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));


    ImageButton previous = (ImageButton) findViewById(R.id.ib_prev);
    previous.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (cal_month.get(GregorianCalendar.MONTH) == 4&&cal_month.get(GregorianCalendar.YEAR)==2017) {
                //cal_month.set((cal_month.get(GregorianCalendar.YEAR) - 1), cal_month.getActualMaximum(GregorianCalendar.MONTH), 1);
                Toast.makeText(MainActivity.this, "Event Detail is available for current session only.", Toast.LENGTH_SHORT).show();
            }
            else {
                setPreviousMonth();
                refreshCalendar();
            }


        }
    });
    ImageButton next = (ImageButton) findViewById(R.id.Ib_next);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (cal_month.get(GregorianCalendar.MONTH) == 5&&cal_month.get(GregorianCalendar.YEAR)==2018) {
                //cal_month.set((cal_month.get(GregorianCalendar.YEAR) + 1), cal_month.getActualMinimum(GregorianCalendar.MONTH), 1);
                Toast.makeText(MainActivity.this, "Event Detail is available for current session only.", Toast.LENGTH_SHORT).show();
            }
            else {
                setNextMonth();
                refreshCalendar();
            }
        }
    });
    GridView gridview = (GridView) findViewById(R.id.gv_calendar);
    gridview.setAdapter(hwAdapter);
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            String selectedGridDate = HwAdapter.day_string.get(position);
            ((HwAdapter) parent.getAdapter()).getPositionList(selectedGridDate, MainActivity.this);
        }

    });
}
protected void setNextMonth() {
    if (cal_month.get(GregorianCalendar.MONTH) == cal_month.getActualMaximum(GregorianCalendar.MONTH)) {
        cal_month.set((cal_month.get(GregorianCalendar.YEAR) + 1), cal_month.getActualMinimum(GregorianCalendar.MONTH), 1);
    } else {
        cal_month.set(GregorianCalendar.MONTH,
                cal_month.get(GregorianCalendar.MONTH) + 1);
    }
}

protected void setPreviousMonth() {
    if (cal_month.get(GregorianCalendar.MONTH) == cal_month.getActualMinimum(GregorianCalendar.MONTH)) {
        cal_month.set((cal_month.get(GregorianCalendar.YEAR) - 1), cal_month.getActualMaximum(GregorianCalendar.MONTH), 1);
    } else {
        cal_month.set(GregorianCalendar.MONTH, cal_month.get(GregorianCalendar.MONTH) - 1);
    }
}

public void refreshCalendar() {
    hwAdapter.refreshDays();
    hwAdapter.notifyDataSetChanged();
    tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));
}