Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create wheel picker for my custom object?

I want to let the user choice an element from ArrayList<MyObjectType> via a wheel picker that looks similar to the wheel picker that gets used to pick the day/month/year in the data picker. While the date picker obviously has three variables, I only care about having one.

                                                  Month picker

What's the most straightforward way to implement such a picker?

like image 581
Christian Avatar asked Sep 11 '18 15:09

Christian


Video Answer


1 Answers

NumberPicker is provided in Android Widgets. That is what you are looking for.

Solution

<NumberPicker
    android:id="@+id/numberPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

and in Java

NumberPicker np = findViewById(R.id.numberPicker);
String[] months3char = get3CharMonths();
np.setMaxValue(months3char.length - 1); // important
np.setDisplayedValues(months3char); // custom values

private String[] get3CharMonths() {
    String[] months = new DateFormatSymbols().getMonths();
    String[] months3char = new String[months.length];
    for (int i = 0; i < months.length; i++) {
        String month = months[i];
        months3char[i] = month.length() < 3 ? month : month.substring(0, 3);
    }
    return months3char;
}

Output

like image 190
Khemraj Sharma Avatar answered Oct 24 '22 02:10

Khemraj Sharma