package com.devdiv.calenderDemo;
import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class calenderDemo extends Activity {
private ArrayList<DateWidgetDayCell> days = new ArrayList<DateWidgetDayCell>();
private Calendar calStartDate = Calendar.getInstance();
private Calendar calToday = Calendar.getInstance();
private Calendar calCalendar = Calendar.getInstance();
private Calendar calSelected = Calendar.getInstance();
LinearLayout layContent = null;
Button btnToday = null;
private int iFirstDayOfWeek = Calendar.SUNDAY;
private int iMonthViewCurrentMonth = 0;
private int iMonthViewCurrentYear = 0;
public static final int SELECT_DATE_REQUEST = 111;
private static final int iDayCellSize = 38;
private static final int iDayHeaderHeight = 24;
private static final int iTotalWidth = (iDayCellSize * 7);
private TextView tv,monthTextView,yearTextView;
private int mYear;
private int mMonth;
private int mDay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
iFirstDayOfWeek = Calendar.SUNDAY;
mYear = calSelected.get(Calendar.YEAR);
mMonth = calSelected.get(Calendar.MONTH);
mDay = calSelected.get(Calendar.DAY_OF_MONTH);
setContentView(generateContentView());
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main);
calStartDate = getCalendarStartDate();
DateWidgetDayCell daySelected = updateCalendar();
updateControlsState();
if (daySelected != null)
daySelected.requestFocus();
}
@Override
public void onStart() {
super.onStart();
}
private LinearLayout createLayout(int iOrientation) {
LinearLayout lay = new LinearLayout(this);
lay.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
lay.setOrientation(iOrientation);
return lay;
}
private Button createButton(String sText, int iWidth, int iHeight) {
Button btn = new Button(this);
btn.setText(sText);
btn.setLayoutParams(new LayoutParams(iWidth, iHeight));
return btn;
}
private void generateTopButtons(LinearLayout layTopControls) {
final int iSmallButtonWidth = 40;
btnToday = createButton("", iTotalWidth - iSmallButtonWidth*4,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
monthTextView = new TextView(this);
monthTextView.setPadding(8, 8, 8, 8);
monthTextView.setText(mYear+"");
monthTextView.setWidth(55);
yearTextView = new TextView(this);
yearTextView.setPadding(20, 8, 8, 8);
yearTextView.setText(format(mMonth+1));
yearTextView.setWidth(55);
Button btnPrevMonth = new Button(this);
btnPrevMonth.setLayoutParams(new LayoutParams(iSmallButtonWidth,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
btnPrevMonth.setBackgroundResource(R.drawable.prev_month);
Button btnPrevYear = new Button(this);
btnPrevYear.setLayoutParams(new LayoutParams(iSmallButtonWidth,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
btnPrevYear.setBackgroundResource(R.drawable.prev_year);
Button btnNextMonth = new Button(this);
btnNextMonth.setLayoutParams(new LayoutParams(iSmallButtonWidth,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
btnNextMonth.setBackgroundResource(R.drawable.next_month);
Button btnNextYear = new Button(this);
btnNextYear.setLayoutParams(new LayoutParams(iSmallButtonWidth,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
btnNextYear.setBackgroundResource(R.drawable.next_year);
// set events
btnPrevMonth.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
setPrevMonthViewItem();
}
});
btnNextMonth.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
setNextMonthViewItem();
}
});
btnPrevYear.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
setPrevYearViewItem();
}
});
btnNextYear.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
setNextYearViewItem();
}
});
layTopControls.setGravity(Gravity.CENTER_HORIZONTAL);
layTopControls.addView(btnPrevYear);
layTopControls.addView(btnPrevMonth);
layTopControls.addView(monthTextView);
layTopControls.addView(yearTextView);
layTopControls.addView(btnNextMonth);
layTopControls.addView(btnNextYear);
}
private View generateContentView() {
LinearLayout layMain = createLayout(LinearLayout.VERTICAL);
layMain.setPadding(8, 8, 8, 8);
LinearLayout layTopControls = createLayout(LinearLayout.HORIZONTAL);
layContent = createLayout(LinearLayout.VERTICAL);
layContent.setPadding(20, 0, 20, 0);
generateTopButtons(layTopControls);
generateCalendar(layContent);
layMain.addView(layTopControls);
layMain.addView(layContent);
tv = new TextView(this);
tv.setPadding(20, 0, 20, 0);
layMain.addView(tv);
return layMain;
}
private View generateCalendarRow() {
LinearLayout layRow = createLayout(LinearLayout.HORIZONTAL);
for (int iDay = 0; iDay < 7; iDay++) {
DateWidgetDayCell dayCell = new DateWidgetDayCell(this,
iDayCellSize, iDayCellSize);
dayCell.setItemClick(mOnDayCellClick);
days.add(dayCell);
layRow.addView(dayCell);
}
return layRow;
}
private View generateCalendarHeader() {
LinearLayout layRow = createLayout(LinearLayout.HORIZONTAL);
for (int iDay = 0; iDay < 7; iDay++) {
DateWidgetDayHeader day = new DateWidgetDayHeader(this,
iDayCellSize, iDayHeaderHeight);
final int iWeekDay = DayStyle.getWeekDay(iDay, iFirstDayOfWeek);
day.setData(iWeekDay);
layRow.addView(day);
}
return layRow;
}
private void generateCalendar(LinearLayout layContent) {
layContent.addView(generateCalendarHeader());
days.clear();
for (int iRow = 0; iRow < 6; iRow++) {
layContent.addView(generateCalendarRow());
}
}
private Calendar getCalendarStartDate() {
calToday.setTimeInMillis(System.currentTimeMillis());
calToday.setFirstDayOfWeek(iFirstDayOfWeek);
if (calSelected.getTimeInMillis() == 0) {
calStartDate.setTimeInMillis(System.currentTimeMillis());
calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
} else {
calStartDate.setTimeInMillis(calSelected.getTimeInMillis());
calStartDate.setFirstDayOfWeek(iFirstDayOfWeek);
}
updateStartDateForMonth();
return calStartDate;
}
private DateWidgetDayCell updateCalendar() {
DateWidgetDayCell daySelected = null;
boolean bSelected = false;
final boolean bIsSelection = (calSelected.getTimeInMillis() != 0);
final int iSelectedYear = calSelected.get(Calendar.YEAR);
final int iSelectedMonth = calSelected.get(Calendar.MONTH);
final int iSelectedDay = calSelected.get(Calendar.DAY_OF_MONTH);
calCalendar.setTimeInMillis(calStartDate.getTimeInMillis());
for (int i = 0; i < days.size(); i++) {
final int iYear = calCalendar.get(Calendar.YEAR);
final int iMonth = calCalendar.get(Calendar.MONTH);
final int iDay = calCalendar.get(Calendar.DAY_OF_MONTH);
final int iDayOfWeek = calCalendar.get(Calendar.DAY_OF_WEEK);
DateWidgetDayCell dayCell = days.get(i);
// check today
boolean bToday = false;
if (calToday.get(Calendar.YEAR) == iYear)
if (calToday.get(Calendar.MONTH) == iMonth)
if (calToday.get(Calendar.DAY_OF_MONTH) == iDay)
bToday = true;
// check holiday
boolean bHoliday = false;
if ((iDayOfWeek == Calendar.SATURDAY)
|| (iDayOfWeek == Calendar.SUNDAY))
bHoliday = true;
if ((iMonth == Calendar.JANUARY) && (iDay == 1))
bHoliday = true;
dayCell.setDat