├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── mars │ │ └── com │ │ └── timepickerwithinterval │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── mars │ │ │ └── com │ │ │ └── timepickerwithinterval │ │ │ ├── DateDialog.java │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable │ │ ├── blue_bottom_line.xml │ │ └── inner_white.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── content_main.xml │ │ └── date_dialog.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── mars │ └── com │ └── timepickerwithinterval │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | TimePickerWithInterval -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TimerPicker with intervals 2 | 3 | Android sample demonstrate how to set a TimerPicker with interval 4 | 5 | # Introduction 6 | The TimePicker is a widget for selecting the time of day, in either 24-hour or AM/PM mode. 7 | 8 | # Screenshots 9 | ![ScreenShot](http://nsa38.casimages.com/img/2016/01/21/160121042744556183.png) 10 | 11 | # License 12 | Copyright 2015 The Android Open Source Project, Inc. 13 | 14 | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at 15 | 16 | http://www.apache.org/licenses/LICENSE-2.0 17 | 18 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 19 | 20 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "mars.com.timepickerwithinterval" 9 | minSdkVersion 14 10 | targetSdkVersion 19 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:design:23.1.1' 25 | } 26 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/rodeff/Downloads/adt-bundle-mac-x86_64-20130729/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/mars/com/timepickerwithinterval/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package mars.com.timepickerwithinterval; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/mars/com/timepickerwithinterval/DateDialog.java: -------------------------------------------------------------------------------- 1 | package mars.com.timepickerwithinterval; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Calendar; 5 | import java.util.GregorianCalendar; 6 | import java.util.List; 7 | 8 | import android.app.Activity; 9 | import android.app.AlertDialog; 10 | import android.app.Dialog; 11 | import android.content.res.Resources; 12 | import android.util.Log; 13 | import android.view.View; 14 | import android.view.View.OnClickListener; 15 | import android.widget.Button; 16 | import android.widget.NumberPicker; 17 | import android.widget.TimePicker; 18 | 19 | public class DateDialog implements OnClickListener { 20 | 21 | private final String TAG = DateDialog.this.getClass().getSimpleName(); 22 | 23 | private final static int TIME_PICKER_INTERVAL = 5; 24 | 25 | private Dialog mDateDialog; 26 | private Activity mActivity; 27 | 28 | private DateDialogListener dListener; 29 | private TimePicker timePicker; 30 | 31 | public interface DateDialogListener { 32 | void OnDateValidate(String time); 33 | } 34 | 35 | public DateDialog(Activity activity) { 36 | mActivity = activity; 37 | 38 | View rootView = mActivity.getLayoutInflater().inflate(R.layout.date_dialog, null, false); 39 | 40 | timePicker = (TimePicker) rootView.findViewById(R.id.timePicker); 41 | Button validateButton = (Button) rootView.findViewById(R.id.validate_btn); 42 | Button cancelButton = (Button) rootView.findViewById(R.id.cancel_btn); 43 | 44 | validateButton.setOnClickListener(this); 45 | cancelButton.setOnClickListener(this); 46 | 47 | // Set timer picker 48 | timePicker.setIs24HourView(true); 49 | 50 | Calendar calendar = new GregorianCalendar(); 51 | calendar.setTimeInMillis(System.currentTimeMillis()); 52 | int hour = calendar.get(Calendar.HOUR_OF_DAY); 53 | int minute = calendar.get(Calendar.MINUTE); 54 | 55 | setTimePickerInterval(timePicker); 56 | 57 | // Configure displayed time 58 | if (((minute % TIME_PICKER_INTERVAL) != 0)) { 59 | int minuteFloor = (minute + TIME_PICKER_INTERVAL) - (minute % TIME_PICKER_INTERVAL); 60 | minute = minuteFloor + (minute == (minuteFloor + 1) ? TIME_PICKER_INTERVAL : 0); 61 | if (minute >= 60) { 62 | minute = minute % 60; 63 | hour++; 64 | } 65 | 66 | timePicker.setCurrentHour(hour); 67 | timePicker.setCurrentMinute(minute / TIME_PICKER_INTERVAL); 68 | } 69 | 70 | // Implement dialog box 71 | AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); 72 | builder.setView(rootView); 73 | mDateDialog = builder.create(); 74 | 75 | } 76 | 77 | public void setDateDialogListener(DateDialogListener listener) { 78 | dListener = listener; 79 | } 80 | 81 | public void show() { 82 | mDateDialog.show(); 83 | } 84 | 85 | /** 86 | * Set TimePicker interval by adding a custom minutes list 87 | * 88 | * @param timePicker 89 | */ 90 | private void setTimePickerInterval(TimePicker timePicker) { 91 | try { 92 | 93 | NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier( 94 | "minute", "id", "android")); 95 | minutePicker.setMinValue(0); 96 | minutePicker.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1); 97 | List displayedValues = new ArrayList(); 98 | for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) { 99 | displayedValues.add(String.format("%02d", i)); 100 | } 101 | minutePicker.setDisplayedValues(displayedValues.toArray(new String[0])); 102 | } catch (Exception e) { 103 | Log.e(TAG, "Exception: " + e); 104 | } 105 | } 106 | 107 | @Override 108 | public void onClick(View view) { 109 | 110 | switch (view.getId()) { 111 | 112 | case R.id.validate_btn: 113 | 114 | String hour = String.format("%02d", timePicker.getCurrentHour()); 115 | String minute = String.format("%02d", timePicker.getCurrentMinute() * 5); 116 | 117 | String dateTime = String.format(mActivity.getString(R.string.time_text), hour, minute); 118 | if (dListener != null) { 119 | dListener.OnDateValidate(dateTime); 120 | } 121 | mDateDialog.cancel(); 122 | 123 | break; 124 | 125 | case R.id.cancel_btn: 126 | mDateDialog.cancel(); 127 | 128 | break; 129 | 130 | default: 131 | break; 132 | 133 | } 134 | 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /app/src/main/java/mars/com/timepickerwithinterval/MainActivity.java: -------------------------------------------------------------------------------- 1 | package mars.com.timepickerwithinterval; 2 | 3 | import android.os.Bundle; 4 | import android.support.design.widget.FloatingActionButton; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.Toolbar; 7 | import android.view.View; 8 | import android.widget.Toast; 9 | 10 | public class MainActivity extends AppCompatActivity implements DateDialog.DateDialogListener { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | 17 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 18 | setSupportActionBar(toolbar); 19 | 20 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 21 | fab.setOnClickListener(new View.OnClickListener() { 22 | @Override 23 | public void onClick(View view) { 24 | 25 | DateDialog dateDialog = new DateDialog(MainActivity.this); 26 | dateDialog.setDateDialogListener(MainActivity.this); 27 | dateDialog.show(); 28 | } 29 | }); 30 | } 31 | 32 | @Override 33 | public void OnDateValidate(String dateTime) { 34 | Toast.makeText(this, dateTime, Toast.LENGTH_SHORT).show(); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/blue_bottom_line.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/inner_white.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 25 | 26 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/date_dialog.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 20 | 21 |