YearPickerView.java revision 3e9818e0267619fecebd55095ab26c53eff92e93
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.datetimepicker.date;
18
19import android.content.Context;
20import android.view.Gravity;
21import android.view.ViewGroup;
22import android.widget.FrameLayout;
23import android.widget.NumberPicker;
24import android.widget.NumberPicker.OnValueChangeListener;
25
26/**
27 * A number picker allowing a user to choose a specific year.
28 */
29public class YearPickerView extends FrameLayout implements OnValueChangeListener {
30
31    private final NumberPicker mPicker;
32    private final DatePickerController mController;
33
34    public YearPickerView(Context context, DatePickerController controller) {
35        super(context);
36        mController = controller;
37        ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
38                LayoutParams.WRAP_CONTENT);
39        setLayoutParams(frame);
40        mPicker = new NumberPicker(context);
41        LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
42                LayoutParams.WRAP_CONTENT);
43        params.gravity = Gravity.CENTER;
44        mPicker.setLayoutParams(params);
45        mPicker.setOnLongPressUpdateInterval(100);
46        mPicker.setMinValue(controller.getMinYear());
47        mPicker.setMaxValue(controller.getMaxYear());
48        mPicker.setWrapSelectorWheel(false);
49        mPicker.setValue(controller.getSelectedDay().year);
50        mPicker.setOnValueChangedListener(this);
51        addView(mPicker);
52    }
53
54    public void setValue(int value) {
55        mPicker.setValue(value);
56    }
57
58    public void onChange() {
59        mPicker.setMinValue(mController.getMinYear());
60        mPicker.setMaxValue(mController.getMaxYear());
61        requestLayout();
62    }
63
64    @Override
65    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
66        mController.onYearPickerSelectionChanged(newVal);
67    }
68}
69