Probably many of you have used widgets such as DatePicker and TimePicker. But the functionality of the widget is limited ability to choose the date and time, but I wanted more- to use this feature to select any number in a specified range (as inkontroleNumericUpDown in C #). Such a possibility exists. DatePicker widget andsystem-based TimePicker widget NumberPicker, you can not use the box. To use it you need to write a little code.
First, add NumberPicker in our main layout, xml-code is as follows:
We have 3 NumberPicker method that we need setRange, getCurrent and setCurrent. I guess by the names of the methods, it is clear that they do. The difficulty is that these methods are hidden for the developer and they can only call using reflection. For ease of use, I wrote a helper-class that allows you to work with these methods. Class code is shown below:
In konstrukture class passed a real object NumberPicker (which LinearLayout), obtained by findViewById of our main layout. Consider how to use freshly NumberPicker in practice.Basically all quite simple when using the helper-class. The screen looks as follows:We now proceed to the second part of the article - full change in the appearance of the widget. For this purpose, a set of prepared me pictures (from my program MyTasks) different states of the buttons ('+' and '-') and a text field widget, it is from them is NumberPicker. Get them mozhnootsyuda.. The images presented in a format Nine-Patch (9-Patch), a type of image can be stretched, depending, for example, of their content, they are often used to set the background of various widgets, including in my case. More information on the 9-Patch you can read here. Images need to put it in the drawable, or a folder drawable-mdpi (-ldpi,-hdpi), as in my case.
In order to fit these images have earned us a way (that is responsive to the different states of widgets) necessary to form three xml-file-selector in the format (a file on each widget - two buttons and a text box). The selector is a set of elements with attributes, which represent different states of the widget (selected, pressed, inactive, etc.). These files can also be placed under the drawable, or drawable-mdpi (-ldpi,-hdpi). Below are the three files.
Button for '+' (timepicker_up_btn.xml)For a text box (ed_picker_background.xml)For button '-' (timepicker_down_btn.xml)Now we need to slightly modify the constructor helper-class that was created forNumberPicker at the outset. This will allow us to create an object NumberPicker change the appearance of its components. The code of the modified class is shown below.The result is shown in the figure below.In this article of mine is complete, I hope I helped you learn something new. Waiting forcomments and amendments, if there is someone to that offer.
First, add NumberPicker in our main layout, xml-code is as follows:
We have 3 NumberPicker method that we need setRange, getCurrent and setCurrent. I guess by the names of the methods, it is clear that they do. The difficulty is that these methods are hidden for the developer and they can only call using reflection. For ease of use, I wrote a helper-class that allows you to work with these methods. Class code is shown below:
In konstrukture class passed a real object NumberPicker (which LinearLayout), obtained by findViewById of our main layout. Consider how to use freshly NumberPicker in practice.Basically all quite simple when using the helper-class. The screen looks as follows:We now proceed to the second part of the article - full change in the appearance of the widget. For this purpose, a set of prepared me pictures (from my program MyTasks) different states of the buttons ('+' and '-') and a text field widget, it is from them is NumberPicker. Get them mozhnootsyuda.. The images presented in a format Nine-Patch (9-Patch), a type of image can be stretched, depending, for example, of their content, they are often used to set the background of various widgets, including in my case. More information on the 9-Patch you can read here. Images need to put it in the drawable, or a folder drawable-mdpi (-ldpi,-hdpi), as in my case.
In order to fit these images have earned us a way (that is responsive to the different states of widgets) necessary to form three xml-file-selector in the format (a file on each widget - two buttons and a text box). The selector is a set of elements with attributes, which represent different states of the widget (selected, pressed, inactive, etc.). These files can also be placed under the drawable, or drawable-mdpi (-ldpi,-hdpi). Below are the three files.
Button for '+' (timepicker_up_btn.xml)For a text box (ed_picker_background.xml)For button '-' (timepicker_down_btn.xml)Now we need to slightly modify the constructor helper-class that was created forNumberPicker at the outset. This will allow us to create an object NumberPicker change the appearance of its components. The code of the modified class is shown below.The result is shown in the figure below.In this article of mine is complete, I hope I helped you learn something new. Waiting forcomments and amendments, if there is someone to that offer.