package com.marshalchen.common.demoofui.sampleModules; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.res.Configuration; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.marshalchen.common.commonUtils.logUtils.Logs; import com.marshalchen.common.uimodule.timessquare.CalendarPickerView; import com.marshalchen.common.demoofui.R; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import static android.widget.Toast.LENGTH_SHORT; public class CalendarSquareActivity extends Activity { private CalendarPickerView calendar; private AlertDialog theDialog; private CalendarPickerView dialogView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calendar_sample_calendar_picker); final Calendar nextYear = Calendar.getInstance(); nextYear.add(Calendar.YEAR, 1); final Calendar lastYear = Calendar.getInstance(); lastYear.add(Calendar.YEAR, -1); calendar = (CalendarPickerView) findViewById(R.id.calendar_view); calendar.init(lastYear.getTime(), nextYear.getTime()) // .inMode(CalendarPickerView.SelectionMode.SINGLE) // .withSelectedDate(new Date()); final Button single = (Button) findViewById(R.id.button_single); final Button multi = (Button) findViewById(R.id.button_multi); final Button range = (Button) findViewById(R.id.button_range); final Button displayOnly = (Button) findViewById(R.id.button_display_only); final Button dialog = (Button) findViewById(R.id.button_dialog); final Button customized = (Button) findViewById(R.id.button_customized); single.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { single.setEnabled(false); multi.setEnabled(true); range.setEnabled(true); displayOnly.setEnabled(true); calendar.init(lastYear.getTime(), nextYear.getTime()) // .inMode(CalendarPickerView.SelectionMode.SINGLE) // .withSelectedDate(new Date()); } }); multi.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { single.setEnabled(true); multi.setEnabled(false); range.setEnabled(true); displayOnly.setEnabled(true); Calendar today = Calendar.getInstance(); ArrayList<Date> dates = new ArrayList<Date>(); for (int i = 0; i < 5; i++) { today.add(Calendar.DAY_OF_MONTH, 3); dates.add(today.getTime()); } calendar.init(new Date(), nextYear.getTime()) // .inMode(CalendarPickerView.SelectionMode.MULTIPLE) // .withSelectedDates(dates); } }); range.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { single.setEnabled(true); multi.setEnabled(true); range.setEnabled(false); displayOnly.setEnabled(true); Calendar today = Calendar.getInstance(); ArrayList<Date> dates = new ArrayList<Date>(); today.add(Calendar.DATE, 3); dates.add(today.getTime()); today.add(Calendar.DATE, 5); dates.add(today.getTime()); calendar.init(new Date(), nextYear.getTime()) // .inMode(CalendarPickerView.SelectionMode.RANGE) // .withSelectedDates(dates); } }); displayOnly.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { single.setEnabled(true); multi.setEnabled(true); range.setEnabled(true); displayOnly.setEnabled(false); calendar.init(new Date(), nextYear.getTime()) // .inMode(CalendarPickerView.SelectionMode.SINGLE) // .withSelectedDate(new Date()) .displayOnly(); } }); dialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { dialogView = (CalendarPickerView) getLayoutInflater().inflate(R.layout.calendar_dialog, null, false); dialogView.init(lastYear.getTime(), nextYear.getTime()) // .withSelectedDate(new Date()); theDialog = new AlertDialog.Builder(CalendarSquareActivity.this).setTitle("I'm a dialog!") .setView(dialogView) .setNeutralButton("Dismiss", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }) .create(); theDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialogInterface) { Logs.d("onShow: fix the dimens!"); dialogView.fixDialogDimens(); } }); theDialog.show(); } }); customized.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { dialogView = (CalendarPickerView) getLayoutInflater() // .inflate(R.layout.calendar_dialog_customized, null, false); dialogView.init(lastYear.getTime(), nextYear.getTime()).withSelectedDate(new Date()); theDialog = new AlertDialog.Builder(CalendarSquareActivity.this).setTitle("Pimp my calendar !") .setView(dialogView) .setNeutralButton("Dismiss", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }).create(); theDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialogInterface) { Logs.d("onShow: fix the dimens!"); dialogView.fixDialogDimens(); } }); theDialog.show(); } }); findViewById(R.id.done_button).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Logs.d("Selected time in millis: " + calendar.getSelectedDate().getTime()); String toast = "Selected: " + calendar.getSelectedDate().getTime(); Toast.makeText(CalendarSquareActivity.this, calendar.getSelectedDate() + " " + toast, LENGTH_SHORT).show(); } }); } @Override public void onConfigurationChanged(Configuration newConfig) { boolean applyFixes = theDialog != null && theDialog.isShowing(); if (applyFixes) { Logs.d("Config change: unfix the dimens so I'll get remeasured!"); dialogView.unfixDialogDimens(); } super.onConfigurationChanged(newConfig); if (applyFixes) { dialogView.post(new Runnable() { @Override public void run() { Logs.d("Config change done: re-fix the dimens!"); dialogView.fixDialogDimens(); } }); } } }