Here we are going to see how to use NumberPicker.
NumberPicker is a widget that enables the user to select a number from a predefined range.
We can see the NumberPicker in Old Style of TimePicker
How it works -
You can select the values on scroll up or down
NumberPicker is a widget that enables the user to select a number from a predefined range.
We can see the NumberPicker in Old Style of TimePicker
How it works -
You can select the values on scroll up or down
You can also edit the value that on view
Create and XML
<LinearLayout
android:id="@+id/id_picker_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<NumberPicker
android:id="@+id/year_pick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"/>
<NumberPicker
android:id="@+id/month_pick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="0.5dp"
android:layout_marginLeft="0.5dp"
android:layout_weight="1"/>
<NumberPicker
android:id="@+id/date_pick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"/>
</LinearLayout>
In Your class
if (pickYear != null) {
pickYear.setMinValue(1950);
pickYear.setMaxValue(2019);
pickYear.setWrapSelectorWheel(true);
pickYear.setValue(2019);
pickYear.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
String text = "YEAR from " + oldVal + " to " + newVal;
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
strYear = ""+newVal;
}
});
}
Text in Number Picker
if (pickMonth != null) {
final String[] values = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
pickMonth.setMinValue(0);
pickMonth.setMaxValue(values.length - 1);
pickMonth.setDisplayedValues(values);
pickMonth.setWrapSelectorWheel(true);
pickMonth.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
String text = "MONTH from " + oldVal + " to " + newVal;
month =newVal;
setDay();
strMonth = values[newVal];
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
if (pickDay != null) {
int maxValue;
if(month==0||month==2||month==4||month==6||month==7||month==9||month==11){
maxValue = 31;
}else if(month==3||month==5||month==8||month==10||month==12){
maxValue = 30;
}else{
maxValue = 29;
}
pickDay.setMinValue(1);
pickDay.setMaxValue(maxValue);
pickDay.setWrapSelectorWheel(true);
pickDay.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
String text = "DAY from " + oldVal + " to " + newVal;
strDat = ""+newVal;
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
___________________________________________________________________________
Happy Coding...
Comments
Post a Comment