• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Simple Time Picker Dialog

cezarzamora5167

Risk Assessment Specialist
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
81
Likes
147
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Operating System

Android

In this tutorial we will try to create a Simple Time Picker Dialog using Android. This simple app can be used in providing some basic information about a person. Android is basically a piece of software which allows your hardware to function. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any devices such as TV, phones, watches etc. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.
  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    RelativeLayout xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. xmlns:

    app=

    "http://schemas.android.com/apk/res-auto"
  4. xmlns:

    tools=

    "http://schemas.android.com/tools"
  5. android:

    layout_width=

    "match_parent"
  6. android:

    layout_height=

    "match_parent"
  7. tools:

    context=

    "com.razormist.simpletimepickerdialog.MainActivity"

    >

  8. <

    Button


  9. android:

    id=

    "@+id/btn_date"
  10. android:

    layout_width=

    "wrap_content"
  11. android:

    layout_height=

    "wrap_content"
  12. android:

    text=

    "Show Date Picker"
  13. android:

    layout_centerInParent=

    "true"

    />

  14. <

    Button


  15. android:

    id=

    "@+id/btn_time"
  16. android:

    layout_width=

    "wrap_content"
  17. android:

    layout_height=

    "wrap_content"
  18. android:

    text=

    "Show Time Picker"
  19. android:

    layout_centerInParent=

    "true"
  20. android:

    layout_below=

    "@+id/btn_date"

    />

  21. </

    RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    manifest xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. package

    =

    "com.razormist.simpletimepickerdialog"

    >

  4. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>

  20. </

    manifest>

The Main Function

This code contains the main function of the application. This code will allow the user to select a desire time and date when clicking the button. To start with first locate your MainActivity java file and open it, then write these some important variables inside the MainActivity class.
  1. Button

    btn_date, btn_time;
  2. DatePickerDialog datePickerDialog;
  3. TimePickerDialog timePickerDialog;

Then write these several method inside the activity class to make the apps work properly.
  1. public

    void

    ShowDatePicker(

    )

    {
  2. btn_date =

    (

    Button

    )

    findViewById(

    R.id

    .btn_date

    )

    ;
  3. btn_date.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  4. @RequiresApi(

    api =

    Build.VERSION_CODES

    .N

    )
  5. @Override
  6. public

    void

    onClick(

    View

    v)

    {
  7. Calendar

    c_date =

    Calendar

    .getInstance

    (

    )

    ;
  8. int

    year =

    c_date.get

    (

    Calendar

    .YEAR

    )

    ;
  9. int

    month =

    c_date.get

    (

    Calendar

    .MONTH

    )

    ;
  10. int

    day =

    c_date.get

    (

    c_date.DAY_OF_MONTH

    )

    ;

  11. datePickerDialog =

    new

    DatePickerDialog(

    MainActivity.this

    , new

    DatePickerDialog.OnDateSetListener

    (

    )

    {
  12. @Override
  13. public

    void

    onDateSet(

    DatePicker view, int

    year, int

    month, int

    dayOfMonth)

    {
  14. Toast.makeText

    (

    MainActivity.this

    , "Month: "

    +

    month +

    " Day: "

    +

    dayOfMonth +

    " Year: "

    +

    year, Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  15. }
  16. }

    , year, month, day)

    ;

  17. datePickerDialog.show

    (

    )

    ;
  18. }
  19. }

    )

    ;
  20. }

  21. public

    void

    ShowTimePicker(

    )

    {
  22. btn_time =

    (

    Button

    )

    findViewById(

    R.id

    .btn_time

    )

    ;
  23. btn_time.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  24. @RequiresApi(

    api =

    Build.VERSION_CODES

    .N

    )
  25. @Override
  26. public

    void

    onClick(

    View

    v)

    {
  27. Calendar

    c_time =

    Calendar

    .getInstance

    (

    )

    ;
  28. int

    hour =

    c_time.get

    (

    Calendar

    .HOUR

    )

    ;
  29. int

    minute =

    c_time.get

    (

    Calendar

    .MINUTE

    )

    ;

  30. timePickerDialog =

    new

    TimePickerDialog(

    MainActivity.this

    , new

    TimePickerDialog.OnTimeSetListener

    (

    )

    {
  31. @Override
  32. public

    void

    onTimeSet(

    TimePicker view, int

    hourOfDay, int

    minute)

    {
  33. Toast.makeText

    (

    MainActivity.this

    , "Hour: "

    +

    hourOfDay +

    " Minute: "

    +

    minute, Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  34. }
  35. }

    ,hour, minute, false

    )

    ;

  36. timePickerDialog.show

    (

    )

    ;
  37. }
  38. }

    )

    ;
  39. }

Finally, initialize all the methods inside the onCreate method to run the application.
  1. ShowDatePicker(

    )

    ;
  2. ShowTimePicker(

    )

    ;

Try to run the app and see if it worked.

There you have it we have created a Simple Time Picker Dialog using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top