Mertcan Seğmen
Mertcan Seğmen

Reputation: 933

TimePicker.getHour() and TimePicker.getMinute() returns current hour and minute

I am making a reminder app. It has an activity where you see your alarms and a floating action button in it to add new alarm. From that button my custom dialog opens up to set an alarm. It is supposed to return the hour and minute selected in the TimePicker but instead it returns the current hour and minute
This is the class:

public class AlarmKurDialog extends AppCompatDialogFragment {

TimePicker timePicker;
SeekBar seekBar;
TextView bardakAdetiTxt;
Button alarmEkleBtn;

int quantity;
int hour;
int minute;

AlarmKurDialogListener listener;
public interface AlarmKurDialogListener{
    void alarmBilgisiGetir(int hour, int minute, int quantity);
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog_alarm_kur, null);
    builder.setView(view);

    timePicker = view.findViewById(R.id.timePicker1);
    seekBar = view.findViewById(R.id.seekBar);
    bardakAdetiTxt = view.findViewById(R.id.txt_adet);
    alarmEkleBtn = view.findViewById(R.id.button);

    timePicker.setIs24HourView(true);

    if (Build.VERSION.SDK_INT >= 23 ) {
        // getHour and getMinute returns current hour and minute
        hour = timePicker.getHour();
        minute = timePicker.getMinute();
        Log.i("mert", "getHour:getMinute - " + hour + ":" + minute);
    }
    else {
        hour = timePicker.getCurrentHour();
        minute = timePicker.getCurrentMinute();
        Log.i("mert", "getCurrentHour:getCurrentMinute - " + hour + ":" + minute);
    }
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
            quantity = 1 + progress;
            bardakAdetiTxt.setText(String.valueOf(quantity));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}
    });

    alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.alarmBilgisiGetir(hour, minute, quantity);
            dismiss();
        }
    });
    return builder.create();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        listener = (AlarmKurDialogListener) context;
    }catch (ClassCastException e){
        throw new ClassCastException(context.toString() + "AlarmKurDialogListener implement etmek gerekli");
    }
}}


enter image description here
What am I doing wrong here?

Upvotes: 0

Views: 769

Answers (2)

Vucko
Vucko

Reputation: 7479

This portion of your code is inside of onCreateDialog method:

if (Build.VERSION.SDK_INT >= 23 ) {
    // getHour and getMinute returns current hour and minute
    hour = timePicker.getHour();
    minute = timePicker.getMinute();
    Log.i("mert", "getHour:getMinute - " + hour + ":" + minute);
}
else {
    hour = timePicker.getCurrentHour();
    minute = timePicker.getCurrentMinute();
    Log.i("mert", "getCurrentHour:getCurrentMinute - " + hour + ":" + minute);
}

Which means that you are assigning a value to hour and minute AT THE MOMENT of the dialog's creation, at which point it just returns the current time. What you want to do is move that whole block of code inside of the submit button's on click listener:

alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // --------------->>>>>PUT THAT CODE HERE INSTEAD! <<<<<-------------------------
        listener.alarmBilgisiGetir(hour, minute, quantity);
        dismiss();
    }
});

This way you'll actually get the latest set value from the picker.

Upvotes: 1

mohammadReza Abiri
mohammadReza Abiri

Reputation: 1799

try this :

 alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hour = timePicker.getHour();
            minute = timePicker.getMinute();

            listener.alarmBilgisiGetir(hour, minute, quantity);
            dismiss();
        }
    });

Upvotes: 4

Related Questions