Search code examples
androiddialogtimepickerandroid-timepicker

TimePicker showing same time in every TextView


I am working on a module where I have to allow user to set 3 different times in different-different textviews for that I am using TimePicker.

But whenever I select time in TimePicker it showing same time in other two TextViews.

So what could be the reason ?

private String textViewTime1, textViewTime2, textViewTime3;
private String mTime1, mTime2, mTime3;

// On clicking Time picker
    public void setTime(View v){
        Calendar now = Calendar.getInstance();
        TimePickerDialog tpd = TimePickerDialog.newInstance(
                this,
                now.get(Calendar.HOUR_OF_DAY),
                now.get(Calendar.MINUTE),
                false
        );
        tpd.setThemeDark(false);
        tpd.show(getFragmentManager(), "Timepickerdialog");
    }

// Obtain time from time picker
    @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinute = minute;
        if (minute < 10) {
            mTime1 = hourOfDay + ":" + "0" + minute;
            mTime2 = hourOfDay + ":" + "0" + minute;
            mTime3 = hourOfDay + ":" + "0" + minute;
        } else {
            mTime1 = hourOfDay + ":" + minute;
            mTime2 = hourOfDay + ":" + minute;
            mTime3 = hourOfDay + ":" + minute;
        }
        textViewTime1.setText(mTime1);
        textViewTime2.setText(mTime2);
        textViewTime3.setText(mTime3);
    }

Solution

  • But whenever I select time in TimePicker it showing same time in other two TextViews.

    Because, you are setting them the same time!

    @Override
        public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMinute = minute;
            if (minute < 10) {
                /// Time set to mTime1
                mTime1 = hourOfDay + ":" + "0" + minute;
                /// Time set to mTime2 same
                mTime2 = hourOfDay + ":" + "0" + minute;
                /// Time set to mTime3 same
                mTime3 = hourOfDay + ":" + "0" + minute;
            } else {
                /// Time set to mTime1
                mTime1 = hourOfDay + ":" + minute;
                /// Time set to mTime2 same
                mTime2 = hourOfDay + ":" + minute;
                /// Time set to mTime3 same
                mTime3 = hourOfDay + ":" + minute;
            }
            textViewTime1.setText(mTime1);
            textViewTime2.setText(mTime2);
            textViewTime3.setText(mTime3);
        }