Loading...

Android - Simple Time Picker Dialog

View: 164    Dowload: 0   Comment: 0   Post by: daii   Category: Mobile applications   Fields: Game - Entertainment

Download  android-simple-time-picker.zip (465.53 KB)

You need to Sign In to download the file android-simple-time-picker.zip
If you do not have an account then Sign up for free here

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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpletimepickerdialog.MainActivity">
  11.  
  12. android:id="@+id/btn_date"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Show Date Picker"
  16. android:layout_centerInParent="true"/>
  17.  
  18. android:id="@+id/btn_time"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="Show Time Picker"
  22. android:layout_centerInParent="true"
  23. android:layout_below="@+id/btn_date"/>
  24.  
  25. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simpletimepickerdialog">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23.  
  24. </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.  
  12. datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
  13. @Override
  14. public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  15. Toast.makeText(MainActivity.this, "Month: " + month + " Day: " + dayOfMonth + " Year: " + year, Toast.LENGTH_SHORT).show();
  16. }
  17. }, year, month, day);
  18.  
  19. datePickerDialog.show();
  20. }
  21. });
  22. }
  23.  
  24. public void ShowTimePicker(){
  25. btn_time = (Button)findViewById(R.id.btn_time);
  26. btn_time.setOnClickListener(new View.OnClickListener() {
  27. @RequiresApi(api = Build.VERSION_CODES.N)
  28. @Override
  29. public void onClick(View v) {
  30. Calendar c_time = Calendar.getInstance();
  31. int hour = c_time.get(Calendar.HOUR);
  32. int minute = c_time.get(Calendar.MINUTE);
  33.  
  34. timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
  35. @Override
  36. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  37. Toast.makeText(MainActivity.this, "Hour: " + hourOfDay + " Minute: " + minute, Toast.LENGTH_SHORT).show();
  38. }
  39. },hour, minute, false);
  40.  
  41. timePickerDialog.show();
  42. }
  43. });
  44. }

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!!!

Android - Simple Time Picker Dialog

Learn on how 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.

Posted on 06-03-2018 

Comment:

To comment you must be logged in members.

Files with category

  • How to Display a Battery Charge Level in Xamarin Forms using Font Awesome

    How to Display a Battery Charge Level in Xamarin Forms using Font Awesome

    View: 36    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Other

    I was building a Xamarin Forms application with an interface that displays the charge level of a battery. I choose to do it with Font Awesome to give uniform aspect to this application with the website the same product has.

  • 8-bit LED Binary Counter

    8-bit LED Binary Counter

    View: 52    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Other

    This article offers a simple implementation of the binary counter powered by Raspberry PI3 and 8 LEDs. It is very similar to what was done before for Arduino board in the previous article Connecting an Arduino to a Breadboard to Light Up LEDs. In...

  • Chat App

    Chat App

    View: 84    Download: 3   Comment: 0

    Category: Android     Fields: none

    This is a Chat App for Android mobile. You can make separate chat with you partner. I'm using Mysql Database and Php code and All APIs are develop on PHP (REST APIs). My whatsApp:+918285672453: Email ID: nirbhayiise@gmail.com

  • Tower Defense 3D

    Tower Defense 3D

    View: 67    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    The goal in this game is to protect your territory against the attacking enemies. Place defensive structures on or along their path of attack to prevent them reaching your base. Strategic choice and positioning of defensive elements is an essential...

  • Flap the Bird

    Flap the Bird

    View: 41    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tap your screen to make the bird fly. While you are flying there are destruction that will stop you. Avoid hitting the floor and flying to high this will deduct your life. You can collect coins and life along the way. For more mobile games and...

  • Bad Santa

    Bad Santa

    View: 55    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This is a simple shooting game where the Bad Santa is shooting the bad snowman and reindeer. You can collect coins and life along the way. For more mobile games and tutorials you can visit this site.

  • Flappy Halloween Game

    Flappy Halloween Game

    View: 49    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Flappy Halloween is a simple game, tap the screen to move the player. Shoot the pumpkin in the ring to gain score. Avoid hitting the flying destructions and falling from the ground. Since the Halloween is coming this game is really perfect to play...

  • Tilt Ball Game

    Tilt Ball Game

    View: 40    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This game is simple you need to tilt your device to make the ball move in your own desired direction. Reach the finish line before the time ends and move to the next level.

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd