2

I've an app that uses a custom dateTime Picker. when the user selects a time and clicks the SET button, it gets the time set on the spinner. My app checks the time and if it's not suitable based on some criteria i show the spinner again.

The problem is when the user changes the time on the spinner a second or subsequent time, the initial time is still set. How can i get the time from the spinner reflected in the dateTime object?

I've tried calling TPic.getCurrentHour etc and also refreshDrawableState on the TPic object but it still doesn't work. Any ideas?

Button ShowDTPicker;
Button ShowDatePicker;
Button ShowTimePicker;
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TimePicker TPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;

public void showDateTimeDialog(){


        //final Calendar c = Calendar.getInstance();

        final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");



            dialog = new Dialog(NfcscannerActivity.this);
            dialog.setContentView(R.layout.datetimepicker);
            switcher = (ViewSwitcher) dialog.findViewById(R.id.DateTimePickerVS);

            TPic = (TimePicker) dialog.findViewById(R.id.TimePicker);
            DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
            TPic.setIs24HourView(true);



            ShowDatePicker = ((Button) dialog.findViewById(R.id.SwitchToDate));
            ShowDatePicker.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switcher.showPrevious();
                    ShowDatePicker.setEnabled(false);
                    ShowTimePicker.setEnabled(true);
                }
            });
            ShowTimePicker = ((Button) dialog.findViewById(R.id.SwitchToTime));
            ShowTimePicker.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switcher.showNext();
                    ShowDatePicker.setEnabled(true);
                    ShowTimePicker.setEnabled(false);
                }
            });

            Set = ((Button) dialog.findViewById(R.id.SetDateTime));
            Set.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    Calendar c = Calendar.getInstance();
                    c.set(DPic.getYear(),  DPic.getMonth(), DPic.getDayOfMonth(), TPic.getCurrentHour(), TPic.getCurrentMinute());
                    Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour() + " " +  TPic.getCurrentMinute());
                    timeSetOnSpinner = new DateTime(c);


................
................
...............
            //check if time is suitable, if not call showDateTimeDialog() again


                }
            });
            ReSet = ((Button) dialog.findViewById(R.id.ResetDateTime));
            ReSet.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar c = Calendar.getInstance();
                    DPic.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                    TPic.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
                    TPic.setCurrentMinute(c.get(Calendar.MINUTE));
                }
            });
            Cancel = ((Button) dialog.findViewById(R.id.CancelDialog));
            Cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.cancel();



                }
            });

            dialog.setTitle("Please enter time of last logout");

            try{
                ReSet.performClick();
            dialog.show();
            }catch(Exception e){
                //ignore
            }
            Log.e(TAG,"Just executed dialog.show() and at the end of showDateTimeDialog method");
        }//showDateTimeDialog()

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_TIME_DIALOG_ID:

                return dialog;
            }
            return null;
        }

.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout 

    android:padding="5dip" 
    android:fitsSystemWindows="true" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/DateTimePicker" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout 

        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:id="@+id/ViewSwitchButtons" 
        android:layout_marginBottom="5dip"> 

        <Button 
            android:layout_height="wrap_content" 
            android:layout_width="0dip" 
            android:id="@+id/SwitchToDate" 
            android:text="Set date" 
            android:enabled="false" 
            android:layout_weight="1"/>

        <Button 
            android:layout_height="wrap_content" 
            android:layout_width="0dip" 
            android:id="@+id/SwitchToTime" 
            android:text="Set time" 
            android:layout_weight="1"/>

        </LinearLayout> 

        <ViewSwitcher 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            android:id="@+id/DateTimePickerVS" 
            android:layout_below="@+id/ViewSwitchButtons" 
            android:outAnimation="@android:anim/fade_out" 
            android:inAnimation="@android:anim/fade_in"> 



                <LinearLayout 
                    android:layout_height="fill_parent" 
                    android:layout_width="fill_parent" 
                    android:id="@+id/TimeLayout" 
                    android:fillViewport="true"> 

                    <TimePicker 
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent" 
                        android:id="@+id/TimePicker" 
                        android:layout_marginRight="5dip" 
                        android:layout_marginLeft="5dip"/>

                    </LinearLayout> 

                <LinearLayout android:layout_height="fill_parent" 
                android:layout_width="fill_parent" 
                android:id="@+id/DateLayout" 
                android:fillViewport="true"> 

                <DatePicker 
                    android:layout_height="wrap_content" 
                    android:layout_width="fill_parent" 
                    android:id="@+id/DatePicker" 
                    android:layout_marginRight="5dip" 
                    android:layout_marginLeft="5dip"/>

                </LinearLayout> 

                    </ViewSwitcher> 

                    <LinearLayout 
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent" 
                        android:id="@+id/ControlButtons" 
                        android:layout_below="@+id/DateTimePicker" 
                        android:paddingTop="185dip"> 

                        <Button 
                            android:layout_height="wrap_content" 
                            android:layout_width="0dip" 
                            android:id="@+id/SetDateTime" 
                            android:text="@android:string/ok" 
                            android:layout_weight="1"/> 

                            <Button 
                                android:layout_height="wrap_content" 
                                android:layout_width="0dip" 
                                android:id="@+id/ResetDateTime" 
                                android:text="Reset" 
                                android:layout_weight="1"/>

                            <Button 
                                android:layout_height="wrap_content" 
                                android:layout_width="0dip" 
                                android:id="@+id/CancelDialog" 
                                android:text="@android:string/cancel" 
                                android:layout_weight="1"/> 

                                </LinearLayout> 

                                </RelativeLayout>
Adnan Abdollah Zaki
  • 4,328
  • 6
  • 52
  • 58
turtleboy
  • 8,210
  • 27
  • 100
  • 199

3 Answers3

3

Android: How to get the time from a TimePicker when it is typed in

It seems like that DatePicker or TimePicker updates own values when its focus is gone. Use clearFocus() method in OnClickListener of set button.

I think the code would be like this:

Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        DPic.clearFocus();
        TPic.clearFocus();

        Calendar c = Calendar.getInstance();
        c.set(DPic.getYear(),  DPic.getMonth(), DPic.getDayOfMonth(),
                TPic.getCurrentHour(), TPic.getCurrentMinute());
        Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour()
                + " " +  TPic.getCurrentMinute());
        timeSetOnSpinner = new DateTime(c);
    }
});

Please note that ShowDTPicker, ShowDatePicker, ShowTimePicker, Set, ReSet, Cancel, DPic, TPic, Date are not following Java naming conventions. Names with a capitalized first letter are for classes or interfaces (and Date class is also exists!). If they are public class variables, each of their first letter should be lowercase. Also use mSwitcher instead of switcher since it is private class variable.

Community
  • 1
  • 1
Yous
  • 728
  • 6
  • 22
1

for kotlin you can test this code.

     var timePicker : TimePicker = findViewById(R.id.timePicker)
        timePicker.setOnTimeChangedListener(object:TimePicker.OnTimeChangedListener { 
override fun onTimeChanged(p0: TimePicker?, hour: Int,minute: Int) {

          // get hour and minute
            hour_set =hour
            minute_set =minute

         } })
Foroogh Varmazyar
  • 1,057
  • 1
  • 14
  • 18
0

I'm having the same issue (with asp.net though)... i know that it's due to the fact that the new selected time is not been considered during the postback so the updates doesn't even occure ... i also know that i had the same issue with the date picker and solve it saving the values in hiddenfields taht i'm retrieving in the code behind . so, basically it's the same here, we need javascript function to store th values in hiddenfield to be retrieved later this function could be triggered with OnClientClick action on your submit button. But i'm having issues finding how to get the values form the timePicker in javascript i did not found no accurate documentation. i hope this helped,keep us tuned please if you find any solution.

Slimane Agouram
  • 365
  • 1
  • 5
  • 17