Search in sources :

Example 1 with CaldroidFragment

use of com.roomorama.caldroid.CaldroidFragment in project Caldroid by roomorama.

the class CaldroidSampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
    // Setup caldroid fragment
    // **** If you want normal CaldroidFragment, use below line ****
    caldroidFragment = new CaldroidFragment();
    // If Activity is created after rotation
    if (savedInstanceState != null) {
        caldroidFragment.restoreStatesFromKey(savedInstanceState, "CALDROID_SAVED_STATE");
    } else // If activity is created from fresh
    {
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
        // Uncomment this to customize startDayOfWeek
        // args.putInt(CaldroidFragment.START_DAY_OF_WEEK,
        // CaldroidFragment.TUESDAY); // Tuesday
        // Uncomment this line to use Caldroid in compact mode
        // args.putBoolean(CaldroidFragment.SQUARE_TEXT_VIEW_CELL, false);
        // Uncomment this line to use dark theme
        //            args.putInt(CaldroidFragment.THEME_RESOURCE, com.caldroid.R.style.CaldroidDefaultDark);
        caldroidFragment.setArguments(args);
    }
    setCustomResourceForDates();
    // Attach to the activity
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(R.id.calendar1, caldroidFragment);
    t.commit();
    // Setup listener
    final CaldroidListener listener = new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
            Toast.makeText(getApplicationContext(), formatter.format(date), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onChangeMonth(int month, int year) {
            String text = "month: " + month + " year: " + year;
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongClickDate(Date date, View view) {
            Toast.makeText(getApplicationContext(), "Long click " + formatter.format(date), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCaldroidViewCreated() {
            if (caldroidFragment.getLeftArrowButton() != null) {
                Toast.makeText(getApplicationContext(), "Caldroid view is created", Toast.LENGTH_SHORT).show();
            }
        }
    };
    // Setup Caldroid
    caldroidFragment.setCaldroidListener(listener);
    final TextView textView = (TextView) findViewById(R.id.textview);
    final Button customizeButton = (Button) findViewById(R.id.customize_button);
    // Customize the calendar
    customizeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (undo) {
                customizeButton.setText(getString(R.string.customize));
                textView.setText("");
                // Reset calendar
                caldroidFragment.clearDisableDates();
                caldroidFragment.clearSelectedDates();
                caldroidFragment.setMinDate(null);
                caldroidFragment.setMaxDate(null);
                caldroidFragment.setShowNavigationArrows(true);
                caldroidFragment.setEnableSwipe(true);
                caldroidFragment.refreshView();
                undo = false;
                return;
            }
            // Else
            undo = true;
            customizeButton.setText(getString(R.string.undo));
            Calendar cal = Calendar.getInstance();
            // Min date is last 7 days
            cal.add(Calendar.DATE, -7);
            Date minDate = cal.getTime();
            // Max date is next 7 days
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, 14);
            Date maxDate = cal.getTime();
            // Set selected dates
            // From Date
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, 2);
            Date fromDate = cal.getTime();
            // To Date
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, 3);
            Date toDate = cal.getTime();
            // Set disabled dates
            ArrayList<Date> disabledDates = new ArrayList<Date>();
            for (int i = 5; i < 8; i++) {
                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, i);
                disabledDates.add(cal.getTime());
            }
            // Customize
            caldroidFragment.setMinDate(minDate);
            caldroidFragment.setMaxDate(maxDate);
            caldroidFragment.setDisableDates(disabledDates);
            caldroidFragment.setSelectedDates(fromDate, toDate);
            caldroidFragment.setShowNavigationArrows(false);
            caldroidFragment.setEnableSwipe(false);
            caldroidFragment.refreshView();
            // Move to date
            // cal = Calendar.getInstance();
            // cal.add(Calendar.MONTH, 12);
            // caldroidFragment.moveToDate(cal.getTime());
            String text = "Today: " + formatter.format(new Date()) + "\n";
            text += "Min Date: " + formatter.format(minDate) + "\n";
            text += "Max Date: " + formatter.format(maxDate) + "\n";
            text += "Select From Date: " + formatter.format(fromDate) + "\n";
            text += "Select To Date: " + formatter.format(toDate) + "\n";
            for (Date date : disabledDates) {
                text += "Disabled Date: " + formatter.format(date) + "\n";
            }
            textView.setText(text);
        }
    });
    Button showDialogButton = (Button) findViewById(R.id.show_dialog_button);
    final Bundle state = savedInstanceState;
    showDialogButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Setup caldroid to use as dialog
            dialogCaldroidFragment = new CaldroidFragment();
            dialogCaldroidFragment.setCaldroidListener(listener);
            // If activity is recovered from rotation
            final String dialogTag = "CALDROID_DIALOG_FRAGMENT";
            if (state != null) {
                dialogCaldroidFragment.restoreDialogStatesFromKey(getSupportFragmentManager(), state, "DIALOG_CALDROID_SAVED_STATE", dialogTag);
                Bundle args = dialogCaldroidFragment.getArguments();
                if (args == null) {
                    args = new Bundle();
                    dialogCaldroidFragment.setArguments(args);
                }
            } else {
                // Setup arguments
                Bundle bundle = new Bundle();
                // Setup dialogTitle
                dialogCaldroidFragment.setArguments(bundle);
            }
            dialogCaldroidFragment.show(getSupportFragmentManager(), dialogTag);
        }
    });
}
Also used : CaldroidListener(com.roomorama.caldroid.CaldroidListener) Bundle(android.os.Bundle) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) View(android.view.View) Date(java.util.Date) CaldroidFragment(com.roomorama.caldroid.CaldroidFragment) FragmentTransaction(android.support.v4.app.FragmentTransaction) Button(android.widget.Button) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Bundle (android.os.Bundle)1 FragmentTransaction (android.support.v4.app.FragmentTransaction)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 CaldroidFragment (com.roomorama.caldroid.CaldroidFragment)1 CaldroidListener (com.roomorama.caldroid.CaldroidListener)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1