Choosing a Time Using the New TimePickerDialog

    While browsing the tape in my G +, I came across a message that the latest version of the Calendar application on Android devices uses a new dialog box to select the time. Having launched the application on my Nexus 7 tablet with Android 4.3, I was convinced that this is indeed so. But I, as a developer, were more interested in the phrase that the source code for the dialog box is available at https://android.googlesource.com/platform/frameworks/opt/datetimepicker/+/master/ . I decided to fiddle just for fun.

    The article is designed for more or less experienced developers, so I will not chew on the explanations very much.
    I did not clone the Git repository, but simply began to copy various classes and resources into my test project. First, in the project, I created a new package and copied all the classes from the src / com / android / datetimepicker / time / folder . I also copied two classes from the src / com / android / datetimepicker folder: Utils.java and AccessibleTextView.java. If you wish, you can copy other classes to yourself, but to create a dialog for selecting the time, this is quite enough. Then I copied all the necessary resources: strings, colors, sizes, themes and Russian localization. The final touch - in the manifest you need to register permission to use the vibrator.
    On this preparatory work is completed.
    The new dialog box is based on the DialogFragment fragment. We outline a simple markup with a single button with the android attribute : onClick = “onClick” , which will bring up a dialog box. And write the code:
    	public void onClick(View v) {
    		openDialog();
    	}
    	void openDialog() {
    		TimePickerDialog timepickerdialog = TimePickerDialog.newInstance(
    				new OnTimeSetListener() {
    					@Override
    					public void onTimeSet(RadialPickerLayout view,
    							int hourOfDay, int minute) {
    						// TODO Auto-generated method stub
    						Toast.makeText(getApplicationContext(),
    								"Вы выбрали время " + hourOfDay + ":" + minute,
    								Toast.LENGTH_LONG).show();
    					}
    				}, 19, 17, false);
    		timepickerdialog.show(getFragmentManager(), "myDialogFragment");
    	}
    


    We try to run the application in the emulator:



    Hurray, it worked!
    We switch the device to Russian and look at the result.



    Frankly, it’s not impressive - the letters do not fit. But on the other hand, I prefer the 24-hour format. Therefore, we change the last parameter of OnTimeSetListener from false to true and start the project again.





    From the pictures it is not entirely clear how this works, so I will explain it in words. First, an hour circle appears with two inscribed circles of numbers. The inner circle is responsible for the hours of the first half of the day, the outer - for the second half. When the user selects the desired hour and releases his finger, the hour circle with the help of animation changes to the minute circle, in which the numbers go with an interval of 5 minutes. The user can select an intermediate position between two divisions to select a more accurate time. If you need to switch back to the hour circle, then just tap on the clock (which now has a gray inactive color) at the top of the dialog.
    If the time is finally selected, then click on the Finish button.and return to the main activity. In my example, a Toast pop-up message will tell you about the selected time.
    In my opinion, the new dialog box looks pretty. I would like this novelty to be available as a separate library or built into the Android system. However, as you see, if you wish, you yourself can implement this functionality in your projects.
    Good luck with your programming!

    Also popular now: