Java Code Examples for android.widget.TimePicker#setOnTimeChangedListener()

The following examples show how to use android.widget.TimePicker#setOnTimeChangedListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimePickerDialog.java    From android_9.0.0_r45 with Apache License 2.0 7 votes vote down vote up
/**
 * Creates a new time picker dialog with the specified theme.
 * <p>
 * The theme is overlaid on top of the theme of the parent {@code context}.
 * If {@code themeResId} is 0, the dialog will be inflated using the theme
 * specified by the
 * {@link android.R.attr#timePickerDialogTheme android:timePickerDialogTheme}
 * attribute on the parent {@code context}'s theme.
 *
 * @param context the parent context
 * @param themeResId the resource ID of the theme to apply to this dialog
 * @param listener the listener to call when the time is set
 * @param hourOfDay the initial hour
 * @param minute the initial minute
 * @param is24HourView Whether this is a 24 hour view, or AM/PM.
 */
public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,
        int hourOfDay, int minute, boolean is24HourView) {
    super(context, resolveDialogTheme(context, themeResId));

    mTimeSetListener = listener;
    mInitialHourOfDay = hourOfDay;
    mInitialMinute = minute;
    mIs24HourView = is24HourView;

    final Context themeContext = getContext();
    final LayoutInflater inflater = LayoutInflater.from(themeContext);
    final View view = inflater.inflate(R.layout.time_picker_dialog, null);
    setView(view);
    setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
    setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
    setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);

    mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
    mTimePicker.setIs24HourView(mIs24HourView);
    mTimePicker.setCurrentHour(mInitialHourOfDay);
    mTimePicker.setCurrentMinute(mInitialMinute);
    mTimePicker.setOnTimeChangedListener(this);
}
 
Example 2
Source File: TimePickerPreference.java    From ploggy with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected View onCreateDialogView() {

        tp = new TimePicker(getContext());
        tp.setOnTimeChangedListener(this);

        String value = getPersistedString(this.defaultValue);
        int h = getHour(value);
        int m = getMinute(value);
        if (h >= 0 && m >= 0) {
                tp.setCurrentHour(h);
                tp.setCurrentMinute(m);
        }
        tp.setIs24HourView(DateFormat.is24HourFormat(getContext()));

        return tp;
}
 
Example 3
Source File: DateWidgets2.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.date_widgets_example_2);

    TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
    timePicker.setCurrentHour(12);
    timePicker.setCurrentMinute(15);

    mTimeDisplay = (TextView) findViewById(R.id.dateDisplay);

    updateDisplay(12, 15);

    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            updateDisplay(hourOfDay, minute);
        }
    });
}
 
Example 4
Source File: DateTimePickerDialog.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @param context The context the dialog is to run in.
 * @param callBack How the parent is notified that the date is set.
 * @param year The initial year of the dialog.
 * @param monthOfYear The initial month of the dialog.
 * @param dayOfMonth The initial day of the dialog.
 */
public DateTimePickerDialog(Context context,
        OnDateTimeSetListener callBack,
        int year,
        int monthOfYear,
        int dayOfMonth,
        int hourOfDay, int minute, boolean is24HourView,
        double min, double max) {
    super(context, 0);

    mMinTimeMillis = (long) min;
    mMaxTimeMillis = (long) max;

    mCallBack = callBack;

    setButton(BUTTON_POSITIVE, context.getText(
            R.string.date_picker_dialog_set), this);
    setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel),
            (OnClickListener) null);
    setIcon(0);
    setTitle(context.getText(R.string.date_time_picker_dialog_title));

    Context dialogContext = getDialogContext(context);
    LayoutInflater inflater =
            (LayoutInflater) dialogContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.date_time_picker_dialog, null);
    setView(view);
    mDatePicker = (DatePicker) view.findViewById(R.id.date_picker);
    DateDialogNormalizer.normalize(mDatePicker, this,
            year, monthOfYear, dayOfMonth, mMinTimeMillis, mMaxTimeMillis);

    mTimePicker = (TimePicker) view.findViewById(R.id.time_picker);
    mTimePicker.setIs24HourView(is24HourView);
    setHour(mTimePicker, hourOfDay);
    setMinute(mTimePicker, minute);
    mTimePicker.setOnTimeChangedListener(this);
    onTimeChanged(mTimePicker, getHour(mTimePicker), getMinute(mTimePicker));
}
 
Example 5
Source File: DateTimePickerDialog.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param context The context the dialog is to run in.
 * @param callBack How the parent is notified that the date is set.
 * @param year The initial year of the dialog.
 * @param monthOfYear The initial month of the dialog.
 * @param dayOfMonth The initial day of the dialog.
 */
public DateTimePickerDialog(Context context,
        OnDateTimeSetListener callBack,
        int year,
        int monthOfYear,
        int dayOfMonth,
        int hourOfDay, int minute, boolean is24HourView,
        long min, long max) {
    super(context, 0);

    mMinTimeMillis = min;
    mMaxTimeMillis = max;

    mCallBack = callBack;

    setButton(BUTTON_POSITIVE, context.getText(
            R.string.date_picker_dialog_set), this);
    setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel),
            (OnClickListener) null);
    setIcon(0);
    setTitle(context.getText(R.string.date_time_picker_dialog_title));

    LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.date_time_picker_dialog, null);
    setView(view);
    mDatePicker = (DatePicker) view.findViewById(R.id.date_picker);
    DateDialogNormalizer.normalize(mDatePicker, this,
            year, monthOfYear, dayOfMonth, hourOfDay, minute, min, max);

    mTimePicker = (TimePicker) view.findViewById(R.id.time_picker);
    mTimePicker.setIs24HourView(is24HourView);
    mTimePicker.setCurrentHour(hourOfDay);
    mTimePicker.setCurrentMinute(minute);
    mTimePicker.setOnTimeChangedListener(this);
    onTimeChanged(mTimePicker, mTimePicker.getCurrentHour(),
            mTimePicker.getCurrentMinute());
}
 
Example 6
Source File: DateTimePickerDialog.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param context The context the dialog is to run in.
 * @param callBack How the parent is notified that the date is set.
 * @param year The initial year of the dialog.
 * @param monthOfYear The initial month of the dialog.
 * @param dayOfMonth The initial day of the dialog.
 */
public DateTimePickerDialog(Context context,
        OnDateTimeSetListener callBack,
        int year,
        int monthOfYear,
        int dayOfMonth,
        int hourOfDay, int minute, boolean is24HourView,
        long min, long max) {
    super(context, 0);

    mMinTimeMillis = min;
    mMaxTimeMillis = max;

    mCallBack = callBack;

    setButton(BUTTON_POSITIVE, context.getText(
            R.string.date_picker_dialog_set), this);
    setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel),
            (OnClickListener) null);
    setIcon(0);
    setTitle(context.getText(R.string.date_time_picker_dialog_title));

    LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.date_time_picker_dialog, null);
    setView(view);
    mDatePicker = (DatePicker) view.findViewById(R.id.date_picker);
    DateDialogNormalizer.normalize(mDatePicker, this,
            year, monthOfYear, dayOfMonth, hourOfDay, minute, min, max);

    mTimePicker = (TimePicker) view.findViewById(R.id.time_picker);
    mTimePicker.setIs24HourView(is24HourView);
    mTimePicker.setCurrentHour(hourOfDay);
    mTimePicker.setCurrentMinute(minute);
    mTimePicker.setOnTimeChangedListener(this);
    onTimeChanged(mTimePicker, mTimePicker.getCurrentHour(),
            mTimePicker.getCurrentMinute());
}
 
Example 7
Source File: TimePickerPreference.java    From BatteryFu with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected View onCreateDialogView() {

	TimePicker tp = new TimePicker(getContext());
	tp.setOnTimeChangedListener(this);

	int h = getHour();
	int m = getMinute();
	if (h >= 0 && m >= 0) {
		tp.setCurrentHour(h);
		tp.setCurrentMinute(m);
	}

	return tp;
}
 
Example 8
Source File: TimeWidget.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
public TimeWidget(Context context, final FormEntryPrompt prompt) {
    super(context, prompt);

    mTimePicker = new TimePicker(getContext());
    mTimePicker.setFocusable(!prompt.isReadOnly());
    mTimePicker.setEnabled(!prompt.isReadOnly());
    mTimePicker.setOnTimeChangedListener(this);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mTimePicker.setSaveFromParentEnabled(false);
        mTimePicker.setSaveEnabled(true);
    }

    String clockType =
            android.provider.Settings.System.getString(context.getContentResolver(),
                    android.provider.Settings.System.TIME_12_24);
    if (clockType == null || clockType.equalsIgnoreCase("24")) {
        mTimePicker.setIs24HourView(true);
    }

    // If there's an answer, use it.
    setAnswer();

    setGravity(Gravity.LEFT);
    addView(mTimePicker);

}
 
Example 9
Source File: StartNewSensor.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(!Sensor.isActive()) {
        setContentView(R.layout.activity_start_new_sensor);
        button = (Button)findViewById(R.id.startNewSensor);
        dp = (DatePicker)findViewById(R.id.datePicker);
        tp = (TimePicker)findViewById(R.id.timePicker);
        tp.setIs24HourView(DateFormat.is24HourFormat(this));
        tp.setSaveFromParentEnabled(false);
        tp.setSaveEnabled(true);
        addListenerOnButton();
        
        tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
                Log.d("NEW SENSOR", "new time " + arg1  + " " + arg2);

                if(arg1 == 23 && last_hour == 0) {
                    Log.d("NEW SENSOR", "decreading day");
                    addDays(-1);

                }
                if (arg1 == 0 && last_hour == 23) {
                    Log.d("NEW SENSOR", "increasing day");
                    addDays(1);
                }
                last_hour = arg1;

            }
        });

        last_hour = tp.getCurrentHour();
        
    } else {
        Intent intent = new Intent(this, StopSensor.class);
        startActivity(intent);
        finish();
    }
}
 
Example 10
Source File: SimpleTimeDialog.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
protected View onCreateContentView(Bundle savedInstanceState) {

    picker = new TimePicker(getContext());

    if (savedInstanceState != null){
        picker.setCurrentHour(savedInstanceState.getInt(HOUR));
        picker.setCurrentMinute(savedInstanceState.getInt(MINUTE));

    } else {
        if (getArguments().containsKey(HOUR)) {
            picker.setCurrentHour(getArguments().getInt(HOUR));
        }
        if (getArguments().containsKey(MINUTE)) {
            picker.setCurrentMinute(getArguments().getInt(MINUTE));
        }
    }

    if (getArguments().containsKey(VIEW_24_HOUR)) {
        picker.setIs24HourView(getArguments().getBoolean(VIEW_24_HOUR));
    } else {
        picker.setIs24HourView(DateFormat.is24HourFormat(getContext())); // system default
    }
    picker.setOnTimeChangedListener(this);

    return picker;
}
 
Example 11
Source File: TimeSetterDialog.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void initWidget(View view) {
    Button done = view.findViewById(R.id.dialog_time_setter_done);
    done.setOnClickListener(this);

    Button cancel = view.findViewById(R.id.dialog_time_setter_cancel);
    cancel.setOnClickListener(this);

    TimePicker timePicker = view.findViewById(R.id.dialog_time_setter_time_picker);
    timePicker.setIs24HourView(true);
    timePicker.setOnTimeChangedListener(this);
}
 
Example 12
Source File: SimpleTimeDialog.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
protected View onCreateContentView(Bundle savedInstanceState) {

    picker = new TimePicker(getContext());

    if (savedInstanceState != null){
        picker.setCurrentHour(savedInstanceState.getInt(HOUR));
        picker.setCurrentMinute(savedInstanceState.getInt(MINUTE));

    } else {
        if (getArguments().containsKey(HOUR)) {
            picker.setCurrentHour(getArguments().getInt(HOUR));
        }
        if (getArguments().containsKey(MINUTE)) {
            picker.setCurrentMinute(getArguments().getInt(MINUTE));
        }
    }

    if (getArguments().containsKey(VIEW_24_HOUR)) {
        picker.setIs24HourView(getArguments().getBoolean(VIEW_24_HOUR));
    } else {
        picker.setIs24HourView(DateFormat.is24HourFormat(getContext())); // system default
    }
    picker.setOnTimeChangedListener(this);

    return picker;
}
 
Example 13
Source File: TimePreference.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected View onCreateDialogView() {
    Context context = getContext();
    TimePicker timePicker = new TimePicker(context);
    timePicker.setIs24HourView(is24HourFormat(context));
    timePicker.setOnTimeChangedListener(this);
    timePicker.setCurrentHour(getHour());
    timePicker.setCurrentMinute(getMinute());
    return timePicker;
}
 
Example 14
Source File: TimeFragment.java    From SlideDateTimePicker with Apache License 2.0 4 votes vote down vote up
/**
 * Create and return the user interface view for this fragment.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    int theme = getArguments().getInt("theme");
    int initialHour = getArguments().getInt("hour");
    int initialMinute = getArguments().getInt("minute");
    boolean isClientSpecified24HourTime = getArguments().getBoolean("isClientSpecified24HourTime");
    boolean is24HourTime = getArguments().getBoolean("is24HourTime");

    // Unless we inflate using a cloned inflater with a Holo theme,
    // on Lollipop devices the TimePicker will be the new-style
    // radial TimePicker, which is not what we want. So we will
    // clone the inflater that we're given but with our specified
    // theme, then inflate the layout with this new inflater.

    Context contextThemeWrapper = new ContextThemeWrapper(
            getActivity(),
            theme == SlideDateTimePicker.HOLO_DARK ?
                     android.R.style.Theme_Holo :
                     android.R.style.Theme_Holo_Light);

    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    View v = localInflater.inflate(R.layout.fragment_time, container, false);

    mTimePicker = (TimePicker) v.findViewById(R.id.timePicker);
    // block keyboard popping up on touch
    mTimePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
        {
            mCallback.onTimeChanged(hourOfDay, minute);
        }
    });

    // If the client specifies a 24-hour time format, set it on
    // the TimePicker.
    if (isClientSpecified24HourTime)
    {
        mTimePicker.setIs24HourView(is24HourTime);
    }
    else
    {
        // If the client does not specify a 24-hour time format, use the
        // device default.
        mTimePicker.setIs24HourView(DateFormat.is24HourFormat(
            getTargetFragment().getActivity()));
    }

    mTimePicker.setCurrentHour(initialHour);
    mTimePicker.setCurrentMinute(initialMinute);

    // Fix for the bug where a TimePicker's onTimeChanged() is not called when
    // the user toggles the AM/PM button. Only applies to 4.0.0 and 4.0.3.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
        Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    {
        fixTimePickerBug18982();
    }

    return v;
}
 
Example 15
Source File: TimeFragment.java    From SlideDayTimePicker with Apache License 2.0 4 votes vote down vote up
/**
 * Create and return the user interface view for this fragment.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    int theme = getArguments().getInt("theme");
    int initialHour = getArguments().getInt("hour");
    int initialMinute = getArguments().getInt("minute");
    boolean isClientSpecified24HourTime = getArguments().getBoolean("isClientSpecified24HourTime");
    boolean is24HourTime = getArguments().getBoolean("is24HourTime");

    // Unless we inflate using a cloned inflater with a Holo theme,
    // on Lollipop devices the TimePicker will be the new-style
    // radial TimePicker, which is not what we want. So we will
    // clone the inflater that we're given but with our specified
    // theme, then inflate the layout with this new inflater.

    Context contextThemeWrapper = new ContextThemeWrapper(
            getActivity(),
            theme == SlideDayTimePicker.HOLO_DARK ?
                    android.R.style.Theme_Holo : android.R.style.Theme_Holo_Light);
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    View v = localInflater.inflate(R.layout.fragment_time, container, false);

    mTimePicker = (TimePicker) v.findViewById(R.id.timePicker);
    // block keyboard popping up on touch
    mTimePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
        {
            mCallback.onTimeChanged(hourOfDay, minute);
        }
    });

    // If the client specifies a 24-hour time format, set it on
    // the TimePicker.
    if (isClientSpecified24HourTime)
    {
        mTimePicker.setIs24HourView(is24HourTime);
    }
    else
    {
        // If the client does not specify a 24-hour time format, use the
        // device default.
        mTimePicker.setIs24HourView(DateFormat.is24HourFormat(
            getTargetFragment().getActivity()));
    }

    mTimePicker.setCurrentHour(initialHour);
    mTimePicker.setCurrentMinute(initialMinute);

    // Fix for the bug where a TimePicker's onTimeChanged() is not called when
    // the user toggles the AM/PM button. Only applies to 4.0.0 and 4.0.3.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
        Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    {
        fixTimePickerBug18982();
    }

    return v;
}
 
Example 16
Source File: DateTimeWidget.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
public DateTimeWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    mDatePicker = new DatePicker(getContext());
    mDatePicker.setFocusable(!prompt.isReadOnly());
    mDatePicker.setEnabled(!prompt.isReadOnly());

    mTimePicker = new TimePicker(getContext());
    mTimePicker.setFocusable(!prompt.isReadOnly());
    mTimePicker.setEnabled(!prompt.isReadOnly());
    mTimePicker.setPadding(0, 20, 0, 0);
    mTimePicker.setOnTimeChangedListener(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mTimePicker.setSaveFromParentEnabled(false);
        mTimePicker.setSaveEnabled(true);
    }

    String clockType =
            android.provider.Settings.System.getString(context.getContentResolver(),
                    android.provider.Settings.System.TIME_12_24);
    if (clockType == null || clockType.equalsIgnoreCase("24")) {
        mTimePicker.setIs24HourView(true);
    }

    mDateListener = (view, year, month, day) -> {
        if (mPrompt.isReadOnly()) {
            setAnswer();
        } else {
            // handle leap years and number of days in month
            // TODO
            // http://code.google.com/p/android/issues/detail?id=2081
            Calendar c = Calendar.getInstance();
            c.set(year, month, 1);
            int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (day > max) {
                //If the day has fallen out of spec, set it to the correct max
                mDatePicker.updateDate(year, month, max);
            } else {
                if (!(mDatePicker.getDayOfMonth() == day && mDatePicker.getMonth() == month && mDatePicker.getYear() == year)) {
                    //CTS: No reason to change the day if it's already correct?
                    mDatePicker.updateDate(year, month, day);
                }
            }
        }
        widgetEntryChanged();
    };

    // If there's an answer, use it.
    setAnswer();

    setGravity(Gravity.LEFT);
    addView(mDatePicker);
    addView(mTimePicker);

}
 
Example 17
Source File: TimePickerDialog.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void initWidget(View view) {
    this.container = view.findViewById(R.id.dialog_time_picker_container);

    Button done = view.findViewById(R.id.dialog_time_picker_done);
    done.setOnClickListener(v -> {
        String hourText;
        String minuteText;

        if (hour < 10) {
            hourText = "0" + hour;
        } else {
            hourText = Integer.toString(hour);
        }

        if (minute < 10) {
            minuteText = "0" + minute;
        } else {
            minuteText = Integer.toString(minute);
        }

        if (startTime) {
            ThemeManager.getInstance(getActivity())
                    .setNightStartTime(getActivity(), hourText + ":" + minuteText);
        } else {
            ThemeManager.getInstance(getActivity())
                    .setNightEndTime(getActivity(), hourText + ":" + minuteText);
        }

        if (listener != null) {
            listener.timeChanged();
        }

        dismiss();
    });

    Button cancel = view.findViewById(R.id.dialog_time_picker_cancel);
    cancel.setOnClickListener(v -> dismiss());

    TimePicker timePicker = view.findViewById(R.id.dialog_time_picker_time_picker);
    timePicker.setIs24HourView(true);
    timePicker.setOnTimeChangedListener((view1, hourOfDay, minute) -> {
        this.hour = hourOfDay;
        this.minute = minute;
    });
}