LiveCode LessonsHow To - LiveCode Mobile Tasks LiveCode Mobile TasksHow do I use the Date Picker View on mobile?

How do I use the Date Picker View on mobile?

This lesson describes how to use mobilePickDate to set a date, a time or both a date and time. Screen captures and source code are provided.

Introduction

The command mobilePickDate provides a native date picker interface that allows users to set the date, the time or the date and time, depending on the parameters passed to mobilePickDate. The parameters to this command allow the selection mode, selection range, a current value and the minute interval to be specified.

Parameters are specified in seconds since the UNIX Epoch, except for the minute interval, that is specified in minutes. The selected date and time is returned in the result variable.

Getting the Date

Getting the Date

You can launch the date picker to select a date only with the following code:

local tDateResult

// launch the date picker

mobilePickDate "date"

// get the result

put the result into tDateResult

// check and display the result in the text field

if tDateResult is 0 then

put "No Selection Made" into field "DateField"

else

convert tDateResult from seconds to abbreviated date

put tDateResult into field "DateField"

end if

This code does not provide any restrictions on the date range from which a selection can be made or provide a default date value. This means that the default date is set to the date at which the date picker is launched. The screen capture shows the pick representation for date selection and the result returned to field "DateField", once the date picker is dismissed.

Getting the Time

Getting the Time

You can launch the time picker with the following code:

local tDateResult

// launch the time picker

mobilePickDate "time",,,,10

// get the result

put the result into tDateResult

// check and display the result in the text field

if tDateResult is 0 then

put "No Selection Made" into field "DateField"

else

convert tDateResult from seconds to short time

put tDateResult into field "DateField"

end if

This code specifies a minute interval of ten minutes, providing a minute picker wheel with the values 00, 10, ..., 40, 50. Other settings are not provided. This means that the default time is set to the time at which the time picker is launched. The screen capture shows the pick representation for time selection and the result returned to field "DateField", once the time picker is dismissed.

Getting the Date and Time (iOS Only)

Getting the Date and Time (iOS Only)

You can launch the date and time picker by using the following code:

local tSelected, tRangeStart, tRangeEnd, tInterval, tDateResult

put "07/29/2011" into tSelected // the date and time selected by default

put "07/20/2011" into tRangeStart // the minimum date and time that can be selected

put "08/01/2011" into tRangeEnd // the maximum date and time that can be selected

put 5 into tInterval

// convert the dates into seconds since the UNIX Epoch

convert tSelected to seconds

convert tRangeStart to seconds

convert tRangeEnd to seconds

// launch the date and time picker

mobilePickDate "dateTime", tSelected, tRangeStart, tRangeEnd, tInterval

// get the result

put the result into tDateResult

// check and display the result in the text field

if tDateResult is 0 then

put "No Selection Made" into field "DateField"

else

convert tDateResult from seconds to internet date

put tDateResult into field "DateField"

end if

This code restricts the date range to a value between 20 July 2011 and 8 August 2011 from which a selection can be made. The default date is set to 29 July 2011. The screen capture shows the pick representation for date selection and the result returned to field "DateField", once the date picker is dismissed.

Note: The dateTime option is only available on iOS.

6 Comments

paulo gomes

Hello,
I reproduced the example above putting the provided code in a button, but I'm getting the following error:

1. if I use the command as described in the Dictionary i get the error

button "Button-getDate": execution error at line 5 (Handler: can't find handler) near "mobilePickDate", char 1

2. if I use the code example I get the error

button "Button-getDate": execution error at line 5 (Handler: can't find handler) near "iphonePickDate", char 1

I'm using Live Code 5.5.1 for Android. Am I missing a addon or plugin ?

Thanks,
Paulo

Hanson Schmidt-Cornelius

Hi Paulo,

no, you are not missing a plugin. The example you see in this lesson is for iOS.
If you would like to use the date picker on Android then you should use "mobilePickDate". I see you are using "mobilePickDate" and "iphonePickDate" in your comment.

"mobilePickDate" on Android also does not support as many features as are supported on iOS. This information is covered in the LiveCode dictionary. In particular, look out for the wording: (iOS Only). This indicates that a particular feature is only supported on iOS and not on Android.

Kind Regards,

Hanson

Paulo Gomes

Hi Cornelius,

Thanks for your reply.

However, I dont know were to go from here to get the date picker to work.
The Dictionary says --> "The default style on Android is "date"

My command in the mouseUp button handler is:

mobilePickDate "date"

So I dont understand why I'm getting the error since Android is supportd.

Maybe a 5.5.1 version bug and I should open a Support Ticket with RunRev?

Once more, thanks for your help.

Paulo

Hanson Schmidt-Cornelius

Hi Paulo,

yes, the default for Android is "date", so all you have to specify is:

mobilePickDate

Kind Regards,

Hanson

paulo gomes

Hi Hanson,

Thanks for the answer. However, still does not work. I believe it does not work with Livecode, Android version. It is a simple button with mobilePickDate command in the mouseUp handler! That is the entire stack.

I opened a support ticket because if it is not supported by the Android version of Livecode, the documentation has to be changed. For the other side, following the documentation it does not work, so something has to be done. If I'm having this trouble, others may have too, right?

Well, I really appreciate your prompt answers.:)

Thanks,
Paulo

Hanson Schmidt-Cornelius

Hi Paulo,

that is odd. I tried mobilePickDate on Android and it launches the date picker for me.
I was using an Android 2.3.3 simulator with LiveCode 5.5.1.

If you launched a support ticket, please ensure you supply a sample stack with it, in case there is something wrong with the settings of the stack.

Kind Regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment