Android Open Source - ElyTheme Silk Date Picker






From Project

Back to project page ElyTheme.

License

The source code is released under:

GNU General Public License

If you think the Android project ElyTheme listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.afollestad.silk.views.time;
/*from w  w  w.  ja  v  a2  s .c  o m*/
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import com.afollestad.silk.R;
import com.afollestad.silk.adapters.SilkSpinnerAdapter;

import java.util.Calendar;

/**
 * A date picker that takes up less vertical space than the stock DatePicker.
 *
 * @author Aidan Follestad (afollestad)
 */
public class SilkDatePicker extends LinearLayout {

    private Calendar mCal;
    private int mCurrentYear;
    private int lastMonth = -1;
    private int lastDay = -1;
    private int lastYear = -1;
    private SilkSpinnerAdapter mMonth;
    private SilkSpinnerAdapter mDay;
    private SilkSpinnerAdapter mYear;

    public SilkDatePicker(Context context) {
        super(context);
        init();
    }

    public SilkDatePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SilkDatePicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public final Calendar getCalendar() {
        return mCal;
    }

    public final boolean isToday() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MONTH) == mCal.get(Calendar.MONTH) &&
                now.get(Calendar.DAY_OF_MONTH) == mCal.get(Calendar.DAY_OF_MONTH) &&
                now.get(Calendar.YEAR) == mCal.get(Calendar.YEAR);
    }

    public void setTime(long milliseconds) {
        mCal.setTimeInMillis(milliseconds);
        invalidateCalendar();
    }

    public final void setTime(Calendar time) {
        setTime(time.getTimeInMillis());
    }

    private void invalidateCalendar() {
        Spinner monthSpinner = (Spinner) getChildAt(0);
        monthSpinner.setSelection(mCal.get(Calendar.MONTH));
        Spinner daySpinner = (Spinner) getChildAt(1);
        daySpinner.setSelection(mCal.get(Calendar.DAY_OF_MONTH) - 1);
        Spinner yearSpinner = (Spinner) getChildAt(2);
        yearSpinner.setSelection(mCal.get(Calendar.YEAR) - getMinYear());
    }

    public int getMinYear() {
        return mCurrentYear - 100;
    }

    public int getMaxYear() {
        return mCurrentYear + 100;
    }

    private void init() {
        if (isInEditMode()) return;
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        setWeightSum(3);

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.silk_date_picker, this, true);

        mCal = Calendar.getInstance();
        mCurrentYear = mCal.get(Calendar.YEAR);
        Spinner monthSpinner = (Spinner) getChildAt(0);
        Spinner daySpinner = (Spinner) getChildAt(1);
        Spinner yearSpinner = (Spinner) getChildAt(2);

        mMonth = new SilkSpinnerAdapter(getContext());
        mDay = new SilkSpinnerAdapter(getContext());
        mYear = new SilkSpinnerAdapter(getContext());
        monthSpinner.setAdapter(mMonth);
        daySpinner.setAdapter(mDay);
        yearSpinner.setAdapter(mYear);

        fillMonths();
        fillDays();
        fillYears();

        monthSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (lastMonth == position) return;
                mCal.set(Calendar.MONTH, position);
                fillDays();
                lastMonth = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        daySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (lastDay == position) return;
                mCal.set(Calendar.DAY_OF_MONTH, position + 1);
                lastDay = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        yearSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (lastYear == position) return;
                mCal.set(Calendar.YEAR, getMinYear() + position);
                lastYear = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        invalidateCalendar();
    }

    private void fillMonths() {
        mMonth.clear();
        String[] months = getContext().getResources().getStringArray(R.array.months);
        mMonth.addAll(months);
        mMonth.notifyDataSetChanged();
    }

    private void fillDays() {
        int daysInMonth = mCal.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (daysInMonth == mDay.getCount()) return;
        mDay.clear();
        for (int i = 1; i <= daysInMonth; i++) mDay.add(i + "");
        mDay.notifyDataSetChanged();
    }

    private void fillYears() {
        mYear.clear();
        for (int i = getMinYear(); i <= getMaxYear(); i++) mYear.add(i + "");
        mYear.notifyDataSetChanged();
    }

    @Override
    public final String toString() {
        String year = mCal.get(Calendar.YEAR) + "";
        String month = (mCal.get(Calendar.MONTH) + 1) + "";
        if (month.length() == 1) month = "0" + month;
        String day = mCal.get(Calendar.DAY_OF_MONTH) + "";
        if (day.length() == 1) day = "0" + day;
        return year + "/" + month + "/" + day;
    }
}




Java Source Code List

com.afollestad.cardsui.ApplicationTest.java
com.afollestad.cardsui.CardAdapter.java
com.afollestad.cardsui.CardBase.java
com.afollestad.cardsui.CardCenteredHeader.java
com.afollestad.cardsui.CardCompressed.java
com.afollestad.cardsui.CardCursorAdapter.java
com.afollestad.cardsui.CardHeader.java
com.afollestad.cardsui.CardListView.java
com.afollestad.cardsui.CardTheme.java
com.afollestad.cardsui.Card.java
com.afollestad.silk.ApplicationTest.java
com.afollestad.silk.SilkComparable.java
com.afollestad.silk.SilkCursorItem.java
com.afollestad.silk.Silk.java
com.afollestad.silk.activities.SilkDrawerActivity.java
com.afollestad.silk.adapters.ScrollStatePersister.java
com.afollestad.silk.adapters.SilkAdapter.java
com.afollestad.silk.adapters.SilkCursorAdapter.java
com.afollestad.silk.adapters.SilkSpinnerAdapter.java
com.afollestad.silk.dialogs.SilkDialog.java
com.afollestad.silk.fragments.feed.SilkCursorFeedFragment.java
com.afollestad.silk.fragments.feed.SilkFeedFragment.java
com.afollestad.silk.fragments.list.SilkCursorListFragment.java
com.afollestad.silk.fragments.list.SilkListFragment.java
com.afollestad.silk.http.SilkHttpBase.java
com.afollestad.silk.http.SilkHttpBody.java
com.afollestad.silk.http.SilkHttpCallback.java
com.afollestad.silk.http.SilkHttpClient.java
com.afollestad.silk.http.SilkHttpException.java
com.afollestad.silk.http.SilkHttpHeader.java
com.afollestad.silk.http.SilkHttpResponse.java
com.afollestad.silk.utilities.IOUtils.java
com.afollestad.silk.utilities.TimeUtils.java
com.afollestad.silk.views.ClickableToast.java
com.afollestad.silk.views.list.OnSilkScrollListener.java
com.afollestad.silk.views.list.SilkGridView.java
com.afollestad.silk.views.list.SilkListView.java
com.afollestad.silk.views.time.SilkDatePicker.java
it.gcaliendo.elytheme.Adw.java
it.gcaliendo.elytheme.ApplicationTest.java
it.gcaliendo.elytheme.DocksProvider.java
it.gcaliendo.elytheme.Docks.java
it.gcaliendo.elytheme.IconActivity.java
it.gcaliendo.elytheme.IconPack.java
it.gcaliendo.elytheme.IconsProvider.java
it.gcaliendo.elytheme.Icons.java
it.gcaliendo.elytheme.RequestActivity.java
it.gcaliendo.elytheme.ThemeActivity.java
it.gcaliendo.elytheme.Wallpaper.java
it.gcaliendo.elytheme.fragments.FragmentAbout.java
it.gcaliendo.elytheme.fragments.FragmentContact.java
it.gcaliendo.elytheme.fragments.FragmentExtras.java
it.gcaliendo.elytheme.fragments.FragmentTheme.java
it.gcaliendo.elytheme.helper.AppInfo.java
it.gcaliendo.elytheme.iconfragment.IconFragmentGames.java
it.gcaliendo.elytheme.iconfragment.IconFragmentLatest.java
it.gcaliendo.elytheme.iconfragment.IconFragmentMisc.java
it.gcaliendo.elytheme.iconfragment.IconFragmentPlay.java
it.gcaliendo.elytheme.iconfragment.IconFragmentSystem.java