android-custom-calendar

Android calendar view

Android calendar view

Hi friends today we are going to learn android custom calendar view. Here we show our events in list and those events in our custom calendar. Click on calendar button to see the events in calendar. The events shows under green circle.

Watch Video how it's work:-

 

Source Code:-

CalenderActivity.java


 

package com.androidfromhome.calendar;

import java.util.GregorianCalendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import com.androidfromhome.calendar.adapter.CalendarAdapter;
import com.androidfromhome.calendar.util.CalendarCollection;

public class CalenderActivity extends Activity {
    public GregorianCalendar cal_month, cal_month_copy;
    private CalendarAdapter cal_adapter;
    private TextView tv_month;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calender);
        
        
        cal_month = (GregorianCalendar) GregorianCalendar.getInstance();
        cal_month_copy = (GregorianCalendar) cal_month.clone();
        cal_adapter = new CalendarAdapter(this, cal_month,CalendarCollection.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 OnClickListener() {

            @Override
            public void onClick(View v) {
                setPreviousMonth();
                refreshCalendar();
            }
        });

        ImageButton next = (ImageButton) findViewById(R.id.Ib_next);
        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setNextMonth();
                refreshCalendar();

            }
        });

        GridView gridview = (GridView) findViewById(R.id.gv_calendar);
        gridview.setAdapter(cal_adapter);
        gridview.setOnItemClickListener(new OnItemClickListener() {
            
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
            
                ((CalendarAdapter) parent.getAdapter()).setSelected(v,position);
                String selectedGridDate = CalendarAdapter.day_string
                        .get(position);
                
                String[] separatedTime = selectedGridDate.split("-");
                String gridvalueString = separatedTime[2].replaceFirst("^0*","");
                int gridvalue = Integer.parseInt(gridvalueString);

                if ((gridvalue > 10) && (position < 8)) {
                    setPreviousMonth();
                    refreshCalendar();
                } else if ((gridvalue < 7) && (position > 28)) {
                    setNextMonth();
                    refreshCalendar();
                }
                ((CalendarAdapter) parent.getAdapter()).setSelected(v,position);

                ((CalendarAdapter) parent.getAdapter()).getPositionList(selectedGridDate, CalenderActivity.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() {
        cal_adapter.refreshDays();
        cal_adapter.notifyDataSetChanged();
        tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));
    }

}

 


 

 

ListViewActivity.java


 

package com.androidfromhome.calendar;

import java.util.ArrayList;
import com.androidfromhome.calendar.R;
import com.androidfromhome.calendar.adapter.AndroidListAdapter;
import com.androidfromhome.calendar.util.CalendarCollection;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class ListViewActivity extends Activity implements OnClickListener {

    private ListView lv_android;
    private AndroidListAdapter list_adapter;
    private Button btn_calender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        
        CalendarCollection.date_collection_arr=new ArrayList<CalendarCollection>();
        CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-01","John Birthday"));
        CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-04","Client Meeting at 5 p.m."));
        CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-06","A Small Party at my office"));
        CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-05-02","Marriage Anniversary"));
        CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-11","Live Event and Concert of sonu"));
        
    
        getWidget();
    }

    
    
    public void getWidget(){
        btn_calender = (Button) findViewById(R.id.btn_calender);
        btn_calender.setOnClickListener(this);
        
        lv_android = (ListView) findViewById(R.id.lv_android);
        list_adapter=new AndroidListAdapter(ListViewActivity.this,R.layout.list_item, CalendarCollection.date_collection_arr);
        lv_android.setAdapter(list_adapter);
        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_calender:
            startActivity(new Intent(ListViewActivity.this,CalenderActivity.class));
            
            break;

        default:
            break;
        }
        
    }
    
}


 

 

CalendarAdapter.java


 

package com.androidfromhome.calendar.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.androidfromhome.calendar.R;
import com.androidfromhome.calendar.util.CalendarCollection;

public class CalendarAdapter extends BaseAdapter {
    private Context context;

    private java.util.Calendar month;
    public GregorianCalendar pmonth;
    /**
     * calendar instance for previous month for getting complete view
     */
    public GregorianCalendar pmonthmaxset;
    private GregorianCalendar selectedDate;
    int firstDay;
    int maxWeeknumber;
    int maxP;
    int calMaxP;
    int lastWeekDay;
    int leftDays;
    int mnthlength;
    String itemvalue, curentDateString;
    DateFormat df;

    private ArrayList<String> items;
    public static List<String> day_string;
    private View previousView;
public ArrayList<CalendarCollection>  date_collection_arr;

    public CalendarAdapter(Context context, GregorianCalendar monthCalendar,ArrayList<CalendarCollection> date_collection_arr) {
        this.date_collection_arr=date_collection_arr;
        CalendarAdapter.day_string = new ArrayList<String>();
        Locale.setDefault(Locale.US);
        month = monthCalendar;
        selectedDate = (GregorianCalendar) monthCalendar.clone();
        this.context = context;
        month.set(GregorianCalendar.DAY_OF_MONTH, 1);
        
        this.items = new ArrayList<String>();
        df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        curentDateString = df.format(selectedDate.getTime());
        refreshDays();
        
    }

    public void setItems(ArrayList<String> items) {
        for (int i = 0; i != items.size(); i++) {
            if (items.get(i).length() == 1) {
                items.set(i, "0" + items.get(i));
            }
        }
        this.items = items;
    }

    public int getCount() {
        return day_string.size();
    }

    public Object getItem(int position) {
        return day_string.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new view for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        TextView dayView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.cal_item, null);

        }
            
        
        dayView = (TextView) v.findViewById(R.id.date);
        String[] separatedTime = day_string.get(position).split("-");
        
        
        String gridvalue = separatedTime[2].replaceFirst("^0*", "");
        if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
            dayView.setTextColor(Color.GRAY);
            dayView.setClickable(false);
            dayView.setFocusable(false);
        } else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
            dayView.setTextColor(Color.GRAY);
            dayView.setClickable(false);
            dayView.setFocusable(false);
        } else {
            // setting curent month's days in blue color.
            dayView.setTextColor(Color.WHITE);
        }

        
        if (day_string.get(position).equals(curentDateString)) {
            
            v.setBackgroundColor(Color.CYAN);
        } else {
            v.setBackgroundColor(Color.parseColor("#343434"));
        }
        
        
        dayView.setText(gridvalue);

        // create date string for comparison
        String date = day_string.get(position);

        if (date.length() == 1) {
            date = "0" + date;
        }
        String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
        if (monthStr.length() == 1) {
            monthStr = "0" + monthStr;
        }

        // show icon if date is not empty and it exists in the items array
        /*ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
        if (date.length() > 0 && items != null && items.contains(date)) {
            iw.setVisibility(View.VISIBLE);
        } else {
            iw.setVisibility(View.GONE);
        }
        */
        
        setEventView(v, position,dayView);
        
        return v;
    }

    public View setSelected(View view,int pos) {
        if (previousView != null) {
        previousView.setBackgroundColor(Color.parseColor("#343434"));
        }
        
        view.setBackgroundColor(Color.CYAN);
        
        int len=day_string.size();
        if (len>pos) {
            if (day_string.get(pos).equals(curentDateString)) {
                
            }else{
                
                previousView = view;    
                    
            }
                
        }
        
        
        return view;
    }

    public void refreshDays() {
        // clear items
        items.clear();
        day_string.clear();
        Locale.setDefault(Locale.US);
        pmonth = (GregorianCalendar) month.clone();
        // month start day. ie; sun, mon, etc
        firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
        // finding number of weeks in current month.
        maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
        // allocating maximum row number for the gridview.
        mnthlength = maxWeeknumber * 7;
        maxP = getMaxP(); // previous month maximum day 31,30....
        calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
        /**
         * Calendar instance for getting a complete gridview including the three
         * month's (previous,current,next) dates.
         */
        pmonthmaxset = (GregorianCalendar) pmonth.clone();
        /**
         * setting the start date as previous month's required date.
         */
        pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);

        /**
         * filling calendar gridview.
         */
        for (int n = 0; n < mnthlength; n++) {

            itemvalue = df.format(pmonthmaxset.getTime());
            pmonthmaxset.add(GregorianCalendar.DATE, 1);
            day_string.add(itemvalue);

        }
    }

    private int getMaxP() {
        int maxP;
        if (month.get(GregorianCalendar.MONTH) == month
                .getActualMinimum(GregorianCalendar.MONTH)) {
            pmonth.set((month.get(GregorianCalendar.YEAR) - 1),
                    month.getActualMaximum(GregorianCalendar.MONTH), 1);
        } else {
            pmonth.set(GregorianCalendar.MONTH,
                    month.get(GregorianCalendar.MONTH) - 1);
        }
        maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);

        return maxP;
    }

    
    
    
    public void setEventView(View v,int pos,TextView txt){
        
        int len=CalendarCollection.date_collection_arr.size();
        for (int i = 0; i < len; i++) {
            CalendarCollection cal_obj=CalendarCollection.date_collection_arr.get(i);
            String date=cal_obj.date;
            int len1=day_string.size();
            if (len1>pos) {
            
            if (day_string.get(pos).equals(date)) {
                v.setBackgroundColor(Color.parseColor("#343434"));
                v.setBackgroundResource(R.drawable.rounded_calender_item);
                
                    txt.setTextColor(Color.WHITE);
            }         
        }}
        
        
    
    }
    
    
public void getPositionList(String date,final Activity act){
        
        int len=CalendarCollection.date_collection_arr.size();
        for (int i = 0; i < len; i++) {
            CalendarCollection cal_collection=CalendarCollection.date_collection_arr.get(i);
            String event_date=cal_collection.date;
            
            String event_message=cal_collection.event_message;
            
            if (date.equals(event_date)) {
            
            Toast.makeText(context, "You have event on this date: "+event_date, Toast.LENGTH_LONG).show();    
             new AlertDialog.Builder(context)
              .setIcon(android.R.drawable.ic_dialog_alert)
              .setTitle("Date: "+event_date)
               .setMessage("Event: "+event_message)
                .setPositiveButton("OK",new android.content.DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which)
                {
                    act.finish();    
                }
                }).show();
            break;        
        }else{
            
            
        }}
        
        
    
    }

}

 


 

 

AndroidListAdapter.java


 

package com.androidfromhome.calendar.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.androidfromhome.calendar.R;
import com.androidfromhome.calendar.util.CalendarCollection;

public class AndroidListAdapter extends ArrayAdapter<CalendarCollection>{
    
    private final Context context;
    private final ArrayList<CalendarCollection> values;
    private ViewHolder viewHolder;
    private final int resourceId;

    public AndroidListAdapter(Context context, int resourceId,ArrayList<CalendarCollection> values) {
        super(context, resourceId, values);
        // TODO Auto-generated constructor stub
        
        this.context = context;
        this.values = values;
        this.resourceId = resourceId;
    }
    
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resourceId, parent, false);
            
                
            viewHolder = new ViewHolder();
            viewHolder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            viewHolder.tv_event = (TextView) convertView.findViewById(R.id.tv_event);
            
            
                convertView.setTag(viewHolder);
            
            
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CalendarCollection list_obj=values.get(position);
        viewHolder.tv_date.setText(list_obj.date);
        viewHolder.tv_event.setText(list_obj.event_message);
            
        return convertView;
    }
    
    
    
    
    
    public class ViewHolder {

         TextView tv_event;
         TextView tv_date;

    }

}


 

 

CalendarCollection.java


 

package com.androidfromhome.calendar.util;

import java.util.ArrayList;

public class CalendarCollection {
    public String date="";
    public String event_message="";
    
    public static ArrayList<CalendarCollection> date_collection_arr;
    public CalendarCollection(String date,String event_message){
    
        this.date=date;    
        this.event_message=event_message;
        
    }

}

 


 

 

 

Source Code of xml view

 

activity_calender.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.calendar.CalenderActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/TextView07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:background="#343434"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#78C165"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp" >

            <FrameLayout
                android:layout_width="45dp"
                android:layout_height="45dp" >

                <ImageButton
                    android:id="@+id/ib_prev"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="2dp"
                    android:background="@drawable/prev" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/tv_month"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dip"
                    android:gravity="center"
                    android:text="January"
                    android:textColor="#ffffff"
                    android:textSize="18dip"
                    android:textStyle="bold" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="45dp"
                android:layout_height="45dp" >

                <ImageButton
                    android:id="@+id/Ib_next"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:background="@drawable/next" />

            </FrameLayout>
        </LinearLayout>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:paddingTop="6dp" >

            <TextView
                android:id="@+id/TextView06"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="S"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/TextView05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="M"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="T"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="W"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="T"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="F"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="S"
                android:textColor="#ffffff" />

        </TableRow>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#dfdfdf"
            android:padding="2dp" />

        <GridView
            android:id="@+id/gv_calendar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:cacheColorHint="#80000000"
            android:gravity="center"
            android:listSelector="@android:color/transparent"
            android:numColumns="7"
            android:padding="2dp"
            android:stretchMode="columnWidth"
            android:textAlignment="gravity" >

        </GridView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="#343434"
            android:orientation="vertical" >

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

 


 

 

activity_list_view.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.calendar.ListViewActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#DFDFDF"
        android:gravity="right"
        android:orientation="vertical"
        android:padding="15dp" >

        <Button
            android:id="@+id/btn_calender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#77C265"
            android:padding="10dp"
            android:text="Calendar"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />

    </LinearLayout>

    <ListView
        android:id="@+id/lv_android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


 

 

cal_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="#343434"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="4dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="1dp" >

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center_vertical"
            android:padding="2dp"
            android:textColor="#000000"
            android:textSize="14dip"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/date_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

    </LinearLayout>

</LinearLayout>


 

 

 

list_item.xml


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:gravity="left"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_event"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="TextView"
            android:textColor="#000000"
            android:textSize="16dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="4dp"
            android:text="TextView" />

    </LinearLayout>

</LinearLayout>

 


 

 

Related Post:-

Android navigation drawer with no action bar

Font Style For Android

Sqlite database example in android

ListView With section header in android example

 

Comments

    • just Add your event data to CalendarCollection.date_collection_arr arraylist in ListViewActivity.java page. Also can add field to CalendarCollection class like event_message,date.

  • Hi, after downloading the source file how shall I open this project or add it to my existing project?
    I tried to just copy the code but in java there were many errors

  • Hi, I want to know what format to add events in this calendar
    (ex: public String date="2015-10-10";
    public String event_message="123"
    in CalendarCollection ?

    • Please download source code and check in "ListViewActivity.java" . Just add your event date as "yyyy-MM-dd" this format in "CalendarCollection.date_collection_arr" arraylist. suppose your event date 1st October 2015 ,you need to add your event as
      CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-10-01","Your event text here")); in "ListViewActivity.java" java class.

    • Please debug the code,put break point in "getPositionList()" method in CalendarAdapter.java class. check here
      [CalendarCollection cal_collection=CalendarCollection.date_collection_arr.get(i);]

      cal_collection CalendarCollection object return null value or check "event_date"/"event_message" give null value, if "event_date"/"event_message" gives null value just initialize those field variable in CalendarCollection class with blank data. just likes [public String event_message="";].

  • haha, my bad.
    I use android studio and I forget to set my AndroidManifest file when I add these classes into my project.
    No problems now. Thanks a lot.

  • Nevermind, fixed it. If anyone gets and error at CalendarAdapter.java in setEventView and the error is a nullPointerException. Go to CallendarCollection.java and at:

    public static ArrayList date_collection_arr;

    set to:

    public static ArrayList date_collection_arr = new ArrayList(1);

    this worked for me.

  • Hey bhaskar ,
    Thanks for sharing knowledge, Superb work . Had gone through many tutorials and not much information like this.
    I'm retrieving eventname and date from sqlite db and displaying it in listview, the issue is only current date(todays date) event is not displaying in calendar(other dates events are displayed in calendar) and also need to refresh calendar page each time to display event in corresponding dates. can you help to solve this. Thank you.

  • what is here your Mainactivity ? i would like to use your calender in a fragment. How can i implement your calender into a fragment ?

  • This is my Log
    Can anyone correct this error .

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setDrawingCacheBackgroundColor(int)' on a null object reference
    at android.widget.AbsListView.obtainView(AbsListView.java:2357)
    at android.widget.GridView.onMeasure(GridView.java:1060)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
    at android.view.View.measure(View.java:17430)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

  • the project project works fine to me, but the event is not show, i already add event in my android mobile but in the project doesn't view. thanks , can you help me?

    • Hi Lloric,
      App only show the event which mention on the app's arraylist.you can edit the code and add date as it's required format.

  • Hello !!!
    I try to use this project on my smartphone and whent I go to January of 2016 the calendar show me all days until 30 but january have 31 days and when I go to May of 2016 the calendar show me all moth and part of the next month. What's wrong with the app.
    Help Please!!!

  • Hi Bhaskar, First of all , thank you for your guide . I am really happy that I found this tutorial.
    I have this one problem, which the event that is already set by you, is not shown in the calendar . may i know why? and there is no error at the coding. P/s:I am new to android 😛

  • Hey! Bhaskar, Thanks for this.Found exactly what i was looking for.
    But i am new to android and i cant get my app working could you plz give step wise instructions of what to do once i hv downloaded the source code

  • This is the log file

    08-13 10:13:13.585 1540-1540/? E/Zygote: v2
    08-13 10:13:13.585 1540-1540/? I/libpersona: KNOX_SDCARD checking this for 10128
    08-13 10:13:13.585 1540-1540/? I/libpersona: KNOX_SDCARD not a persona
    08-13 10:13:13.585 1540-1540/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-A300F_5.0.2_0016
    08-13 10:13:13.585 1540-1540/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
    08-13 10:13:13.585 1540-1540/? I/art: Late-enabling -Xcheck:jni
    08-13 10:13:13.635 1540-1540/? D/TimaKeyStoreProvider: in addTimaSignatureService
    08-13 10:13:13.645 1540-1540/? D/TimaKeyStoreProvider: Cannot add TimaSignature Service, License check Failed
    08-13 10:13:13.645 1540-1540/? D/ActivityThread: Added TimaKesytore provider
    08-13 10:13:13.865 1540-1540/hype.calendar D/DisplayManager: DisplayManager()
    08-13 10:13:14.125 1540-1600/hype.calendar D/OpenGLRenderer: Render dirty regions requested: true
    08-13 10:13:14.195 1540-1540/hype.calendar D/AndroidRuntime: Shutting down VM
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: FATAL EXCEPTION: main
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: Process: hype.calendar, PID: 1540
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at hype.calendar.CalendarAdapter.setEventView(CalendarAdapter.java:235)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at hype.calendar.CalendarAdapter.getView(CalendarAdapter.java:151)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2396)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.GridView.onMeasure(GridView.java:1060)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.View.measure(View.java:17826)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.View.measure(View.java:17826)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.View.measure(View.java:17826)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.View.measure(View.java:17826)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.View.measure(View.java:17826)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.View.measure(View.java:17826)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5873)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5536)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
    08-13 10:13:14.255 1540-1540/hype.calendar E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)
    08-13 10:13:17.935 1540-1540/hype.calendar I/Process: Sending signal. PID: 1540 SIG: 9

  • Oh, i get it, daystrings.get(pos) at selected method can display the selected day... Thanks for this wonderful work, it helps me alot...

  • I like your application, but how to make custom events in it? for example when I click the day a new activity will pop out and in there you could add event such as date/time subject description then will be entered in the database?

  • Its great help !! I was searching this from so many days. Thanks a lot for sharing this. 🙂 Super like
    If i require any help in modifying this as per my requirement then will post here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget