Example usage for android.widget TimePicker setOnTimeChangedListener

List of usage examples for android.widget TimePicker setOnTimeChangedListener

Introduction

In this page you can find the example usage for android.widget TimePicker setOnTimeChangedListener.

Prototype

public void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener) 

Source Link

Document

Set the callback that indicates the time has been adjusted by the user.

Usage

From source file:some.fragments.TimePickerFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    currentTime = (String) getArguments().getSerializable(EXTRA_CURRENT_TIME);

    Integer hour = Integer.parseInt(currentTime.split(":")[0]);
    Integer minute = Integer.parseInt(currentTime.split(":")[1]);

    View calendarView = getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);

    TimePicker timePicker = (TimePicker) calendarView.findViewById(R.id.dialog_time_timePicker);
    timePicker.setCurrentHour(hour);//from  w  ww . j  av a  2  s  . c o  m
    timePicker.setCurrentMinute(minute);

    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            String newTime = hourOfDay + ":" + minute;
            getArguments().putSerializable(EXTRA_CURRENT_TIME, newTime);
            currentTime = newTime;
            Log.i(TAG, "Nueva hora: " + newTime);
        }

    });

    return new AlertDialog.Builder(getActivity()).setView(calendarView).setTitle(R.string.time_picker_title)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.i(TAG, "Enviando resultado de Time");
                    sendResult(Activity.RESULT_OK);
                }
            }).create();
}

From source file:de.escoand.readdaily.ReminderDialogFragment.java

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    TimePicker v = new TimePicker(getContext());

    settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
    hour = settings.getInt(SETTINGS_HOUR, 9);
    minute = settings.getInt(SETTINGS_MINUTE, 0);

    v.setIs24HourView(true);//from   w w  w. j  a  va 2 s.  c  o m
    v.setOnTimeChangedListener(this);
    v.setCurrentHour(hour);
    v.setCurrentMinute(minute);

    return new AlertDialog.Builder(getContext()).setView(v).setPositiveButton(R.string.button_ok, this)
            .setNegativeButton(R.string.button_cancel, this).create();
}

From source file:com.cmput301w17t07.moody.EditMoodActivity.java

/**
 * set up the datetimePicker <br>/*from   w  ww.j  ava 2s .c o  m*/
 * link: http://blog.csdn.net/hzflogo/article/details/62423240 <br>
 * author: hzflogo 2017-03-16 14:58 <br>
 * taken by Xin Huang 2017-03-29 21:42 <br>
 */

private void innit() {
    final View dateView = View.inflate(getApplicationContext(), R.layout.datepicker, null);
    final View timeView = View.inflate(getApplicationContext(), R.layout.timepicker, null);
    TimePicker timePicker = (TimePicker) timeView.findViewById(R.id.time);
    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            currentHour = hourOfDay;
            currentMinte = minute;
        }
    });
    DatePicker datePicker = (DatePicker) dateView.findViewById(R.id.pick);
    datePicker.setDrawingCacheBackgroundColor(getResources().getColor(R.color.colorAccent));
    datePicker.init(currentYear, currentMonth, currentDay, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            currentYear = year;
            currentMonth = monthOfYear + 1;
            currentDay = dayOfMonth;
        }
    });
    DateDialog = new AlertDialog.Builder(EditMoodActivity.this).setView(dateView)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dateString = currentYear + "-" + currentMonth + "-" + currentDay + " " + currentHour + ":"
                            + currentMinte;
                    try {
                        java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        date = formatter.parse(dateString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(EditMoodActivity.this, "" + date, Toast.LENGTH_LONG).show();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DateDialog.dismiss();
                }
            }).create();
    TimeDialog = new AlertDialog.Builder(EditMoodActivity.this).setView(timeView)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DateDialog.show();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TimeDialog.dismiss();
                }
            }).create();

}

From source file:com.cmput301w17t07.moody.CreateMoodActivity.java

private void innit() {
    final View dateView = View.inflate(getApplicationContext(), R.layout.datepicker, null);
    final View timeView = View.inflate(getApplicationContext(), R.layout.timepicker, null);
    TimePicker timePicker = (TimePicker) timeView.findViewById(R.id.time);
    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override/*from w  ww . j ava2s . c o  m*/
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            currentHour = hourOfDay;
            currentMinute = minute;
        }
    });
    DatePicker datePicker = (DatePicker) dateView.findViewById(R.id.pick);
    datePicker.setDrawingCacheBackgroundColor(getResources().getColor(R.color.colorAccent));
    datePicker.init(currentYear, currentMonth, currentDay, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            currentYear = year;
            currentMonth = monthOfYear + 1;
            currentDay = dayOfMonth;
        }
    });
    DateDialog = new AlertDialog.Builder(CreateMoodActivity.this).setView(dateView)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dateString = currentYear + "-" + currentMonth + "-" + currentDay + " " + currentHour + ":"
                            + currentMinute;
                    try {
                        java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        date = formatter.parse(dateString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(CreateMoodActivity.this, "" + date, Toast.LENGTH_LONG).show();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DateDialog.dismiss();
                }
            }).create();
    TimeDialog = new AlertDialog.Builder(CreateMoodActivity.this).setView(timeView)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DateDialog.show();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TimeDialog.dismiss();
                }
            }).create();

}

From source file:com.simplaapliko.wakeup.sample.ui.DialogDateTime.java

private void initUiWidgets(View rootView) {
    Log.d(TAG, "initUiWidgets()");

    // set up tabhost
    TabHost tabHost = (TabHost) rootView.findViewById(R.id.tabhost);
    tabHost.setup();/*from   ww w  . jav  a2 s .c o  m*/

    TabHost.TabSpec tabSpec;

    // adding tabs
    tabSpec = tabHost.newTabSpec("date");
    tabSpec.setIndicator("Date");
    tabSpec.setContent(R.id.date);
    tabHost.addTab(tabSpec);

    tabSpec = tabHost.newTabSpec("time");
    tabSpec.setIndicator("Time");
    tabSpec.setContent(R.id.time);
    tabHost.addTab(tabSpec);

    TimePicker timePicker = (TimePicker) rootView.findViewById(R.id.time);
    timePicker.setIs24HourView(true); //set to true, because it is more compact
    timePicker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

    // init time picker before OnTimeChangedListener() is set
    // otherwise minutes will be set to current time, once setCurrentHour() is called
    timePicker.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
    timePicker.setCurrentMinute(mCalendar.get(Calendar.MINUTE));

    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

            mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            mCalendar.set(Calendar.MINUTE, minute);
            mCalendar.set(Calendar.SECOND, 0);
        }
    });

    DatePicker datePicker = (DatePicker) rootView.findViewById(R.id.date);
    datePicker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
            mCalendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    mCalendar.set(Calendar.YEAR, year);
                    mCalendar.set(Calendar.MONTH, monthOfYear);
                    mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                }
            });
}

From source file:com.roque.rueda.cashflows.fragments.AddMovementFragment.java

/**
 * Create a date time dialog to allow the user choose the date for the movement.
 *
 */// w w  w .  ja  v a2  s  .  c  om
private void createDateTimeDialog() {

    // Add a listener to display a dialog when the user taps on this view.
    mDateText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Set the current date if it's one the saved instance object.
            final Calendar c = Calendar.getInstance();
            c.setTimeInMillis(mCurrentDate);

            // Dialog settings.
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_DARK);
            LayoutInflater layoutInflater = getActivity().getLayoutInflater();
            builder.setTitle(R.string.time_date_dialog_title);
            // Custom view used to display date picker and time picker.
            View dateTimeLayout = layoutInflater.inflate(R.layout.date_time_layout, null);

            // Text used to display the selected date.
            final TextView selectedDate = (TextView) dateTimeLayout.findViewById(R.id.selected_date);
            final Date selectedDateByUser = new Date(getInputDate());

            selectedDate.setText(StringFormatter.formatDate(selectedDateByUser));
            final DatePicker datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.date_picker);
            final TimePicker timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.time_picker);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(selectedDateByUser);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int hourOfTheDay = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            timePicker.setCurrentHour(hourOfTheDay);
            timePicker.setCurrentMinute(minute);

            // Initialize the date picker, also add a listener to
            // update the label when user change the values.
            datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.YEAR, year);
                    calendar.set(Calendar.MONTH, monthOfYear);
                    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                    calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                    calendar.set(Calendar.SECOND, 0);
                    Date modifyDate = calendar.getTime();
                    // Update the text view.
                    selectedDate.setText(StringFormatter.formatDate(modifyDate));
                }
            });

            // Listener used to update the values when time is selected.
            timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                @Override
                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.YEAR, datePicker.getYear());
                    calendar.set(Calendar.MONTH, datePicker.getMonth());
                    calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calendar.set(Calendar.MINUTE, minute);
                    calendar.set(Calendar.SECOND, 0);
                    Date modifyDate = calendar.getTime();
                    // Update the text view.
                    selectedDate.setText(StringFormatter.formatDate(modifyDate));
                }
            });

            // Set the view to the dialog.
            builder.setView(dateTimeLayout);

            // Accept button behaviour.
            builder.setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Calendar dialogDate = Calendar.getInstance();
                    dialogDate.set(Calendar.YEAR, datePicker.getYear());
                    dialogDate.set(Calendar.MONTH, datePicker.getMonth());
                    dialogDate.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                    dialogDate.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                    dialogDate.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                    dialogDate.set(Calendar.SECOND, 0);

                    setCurrentDateText(dialogDate.getTime());
                    dialog.dismiss();
                }
            });

            // Cancel button behaviour.
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.cancel();
                }
            });

            // Create and display the dialog.
            Dialog dateDialog = builder.create();
            dateDialog.show();

        }
    });
}