Created
November 20, 2012 07:51
-
-
Save sumitsharma/4116636 to your computer and use it in GitHub Desktop.
Android Calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:background="@drawable/background" | |
android:orientation="vertical" > | |
<RelativeLayout | |
android:id="@+id/header" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:background="@drawable/calendar_top" > | |
<RelativeLayout | |
android:id="@+id/previous" | |
android:layout_width="40dip" | |
android:layout_height="30dip" | |
android:layout_alignParentLeft="true" > | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:background="@drawable/arrow_left" /> | |
</RelativeLayout> | |
<TextView | |
android:id="@+id/title" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="5dip" | |
android:textColor="#000000" | |
android:textSize="18dip" | |
android:textStyle="bold" /> | |
<RelativeLayout | |
android:id="@+id/next" | |
android:layout_width="40dip" | |
android:layout_height="30dip" | |
android:layout_alignParentRight="true" > | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:background="@drawable/arrow_right" /> | |
</RelativeLayout> | |
</RelativeLayout> | |
<GridView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/gridview" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:listSelector="@android:color/transparent" | |
android:numColumns="7" | |
android:stretchMode="columnWidth" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@drawable/calendar_cell" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:padding="2dip" > | |
<TextView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/date" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textColor="#0000D7" | |
android:textSize="14dip" | |
android:textStyle="bold" > | |
</TextView> | |
<ImageView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/date_icon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:src="@drawable/dot" | |
android:visibility="gone" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.examples.android.calendar; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.List; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
public class CalendarAdapter extends BaseAdapter { | |
private Context mContext; | |
private java.util.Calendar month; | |
public Calendar pmonth; // calendar instance for previous month | |
/** | |
* calendar instance for previous month for getting complete view | |
*/ | |
public Calendar pmonthmaxset; | |
private Calendar 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> dayString; | |
private View previousView; | |
// private List<String> mnthString; | |
public CalendarAdapter(Context c, Calendar monthCalendar) { | |
CalendarAdapter.dayString = new ArrayList<String>(); | |
month = monthCalendar; | |
pmonth = (Calendar) month.clone(); | |
selectedDate = (Calendar) monthCalendar.clone(); | |
mContext = c; | |
month.set(Calendar.DAY_OF_MONTH, 1); | |
this.items = new ArrayList<String>(); | |
df = new SimpleDateFormat("yyyy-MM-dd"); | |
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 dayString.size(); | |
} | |
public Object getItem(int position) { | |
return dayString.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) mContext | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
v = vi.inflate(R.layout.calendar_item, null); | |
} | |
dayView = (TextView) v.findViewById(R.id.date); | |
// separates daystring into parts. | |
String[] separatedTime = dayString.get(position).split("-"); | |
// taking last part of date. ie; 2 from 2012-12-02 | |
String gridvalue = separatedTime[2].replaceFirst("^0*", ""); | |
// checking whether the day is in current month or not. | |
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) { | |
// setting offdays to white color. | |
dayView.setTextColor(Color.WHITE); | |
dayView.setClickable(false); | |
dayView.setFocusable(false); | |
// i++; | |
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) { | |
dayView.setTextColor(Color.WHITE); | |
dayView.setClickable(false); | |
dayView.setFocusable(false); | |
// i++; | |
} else { | |
// setting curent month's days in blue color. | |
dayView.setTextColor(Color.BLUE); | |
} | |
if (dayString.get(position).equals(curentDateString)) { | |
setSelected(v); | |
previousView = v; | |
} else { | |
v.setBackgroundResource(R.drawable.list_item_background); | |
} | |
dayView.setText(gridvalue); | |
// create date string for comparison | |
String date = dayString.get(position); | |
if (date.length() == 1) { | |
date = "0" + date; | |
} | |
String monthStr = "" + (month.get(Calendar.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.INVISIBLE); | |
} | |
return v; | |
} | |
public View setSelected(View view) { | |
if (previousView != null) { | |
previousView.setBackgroundResource(R.drawable.list_item_background); | |
} | |
previousView = view; | |
view.setBackgroundResource(R.drawable.calendar_cel_selectl); | |
return view; | |
} | |
public void refreshDays() { | |
// clear items | |
items.clear(); | |
dayString.clear(); | |
// month start day. ie; sun, mon, etc | |
firstDay = month.get(Calendar.DAY_OF_WEEK); | |
// finding number of weeks in current month. | |
maxWeeknumber = month.getActualMaximum(Calendar.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 = (Calendar) pmonth.clone(); | |
/** | |
* setting the start date as previous month's required date. | |
*/ | |
pmonthmaxset.set(Calendar.DATE, calMaxP + 1); | |
/** | |
* filling calendar gridview. | |
*/ | |
for (int n = 0; n < mnthlength; n++) { | |
itemvalue = df.format(pmonthmaxset.getTime()); | |
pmonthmaxset.add(Calendar.DATE, 1); | |
dayString.add(itemvalue); | |
} | |
} | |
private int getMaxP() { | |
int maxP; | |
if (month.get(Calendar.MONTH) == month.getActualMinimum(Calendar.MONTH)) { | |
pmonth.set((month.get(Calendar.YEAR) - 1), | |
month.getActualMaximum(Calendar.MONTH), 1); | |
} else { | |
pmonth.set(Calendar.MONTH, month.get(Calendar.MONTH) - 1); | |
} | |
maxP = pmonth.getActualMaximum(Calendar.DAY_OF_MONTH); | |
Log.i("", "maxP is : "+maxP); | |
Log.e("", "calendar month is : "+Calendar.MONTH); | |
return maxP; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.examples.android.calendar; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
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.RelativeLayout; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class CalendarView extends Activity { | |
public Calendar month, itemmonth;// calendar instances. | |
public CalendarAdapter adapter;// adapter instance | |
public Handler handler;// for grabbing some event values for showing the dot | |
// marker. | |
public ArrayList<String> items; // container to store calendar items which | |
// needs showing the event marker | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.calendar); | |
month = Calendar.getInstance(); | |
itemmonth = (Calendar) month.clone(); | |
items = new ArrayList<String>(); | |
adapter = new CalendarAdapter(this, month); | |
GridView gridview = (GridView) findViewById(R.id.gridview); | |
gridview.setAdapter(adapter); | |
handler = new Handler(); | |
handler.post(calendarUpdater); | |
TextView title = (TextView) findViewById(R.id.title); | |
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month)); | |
RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous); | |
previous.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
setPreviousMonth(); | |
refreshCalendar(); | |
} | |
}); | |
RelativeLayout next = (RelativeLayout) findViewById(R.id.next); | |
next.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
setNextMonth(); | |
refreshCalendar(); | |
} | |
}); | |
gridview.setOnItemClickListener(new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> parent, View v, | |
int position, long id) { | |
((CalendarAdapter) parent.getAdapter()).setSelected(v); | |
String selectedGridDate = CalendarAdapter.dayString | |
.get(position); | |
String[] separatedTime = selectedGridDate.split("-"); | |
String gridvalueString = separatedTime[2].replaceFirst("^0*", | |
"");// taking last part of date. ie; 2 from 2012-12-02. | |
int gridvalue = Integer.parseInt(gridvalueString); | |
// navigate to next or previous month on clicking offdays. | |
if ((gridvalue > 10) && (position < 8)) { | |
setPreviousMonth(); | |
refreshCalendar(); | |
} else if ((gridvalue < 7) && (position > 28)) { | |
setNextMonth(); | |
refreshCalendar(); | |
} | |
((CalendarAdapter) parent.getAdapter()).setSelected(v); | |
showToast(selectedGridDate); | |
} | |
}); | |
} | |
protected void setNextMonth() { | |
if (month.get(Calendar.MONTH) == month.getActualMaximum(Calendar.MONTH)) { | |
month.set((month.get(Calendar.YEAR) + 1), | |
month.getActualMinimum(Calendar.MONTH), 1); | |
} else { | |
month.set(Calendar.MONTH, month.get(Calendar.MONTH) + 1); | |
} | |
} | |
protected void setPreviousMonth() { | |
if (month.get(Calendar.MONTH) == month.getActualMinimum(Calendar.MONTH)) { | |
month.set((month.get(Calendar.YEAR) - 1), | |
month.getActualMaximum(Calendar.MONTH), 1); | |
} else { | |
month.set(Calendar.MONTH, month.get(Calendar.MONTH) - 1); | |
} | |
} | |
protected void showToast(String string) { | |
Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); | |
} | |
public void refreshCalendar() { | |
TextView title = (TextView) findViewById(R.id.title); | |
adapter.refreshDays(); | |
adapter.notifyDataSetChanged(); | |
handler.post(calendarUpdater); // generate some calendar items | |
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month)); | |
} | |
public Runnable calendarUpdater = new Runnable() { | |
@Override | |
public void run() { | |
items.clear(); | |
// Print dates of the current week | |
DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); | |
String itemvalue; | |
for (int i = 0; i < 7; i++) { | |
itemvalue = df.format(itemmonth.getTime()); | |
itemmonth.add(Calendar.DATE, 1); | |
items.add("2012-09-12"); | |
items.add("2012-10-07"); | |
items.add("2012-10-15"); | |
items.add("2012-10-20"); | |
items.add("2012-11-30"); | |
items.add("2012-11-28"); | |
} | |
adapter.setItems(items); | |
adapter.notifyDataSetChanged(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to display clicked date selected when come back any month