Skip to content

An Android library that provides a simple and customizable ValuePicker.

License

Notifications You must be signed in to change notification settings

mars885/value-picker

Repository files navigation

ValuePicker

An Android library that provides a simple and customizable ValuePicker.

Platform Download Build Android Arsenal License

Contents

Demo (YouTube)

Installation

  1. Make sure that you've added the mavenCentral() repository to your top-level build.gradle file.
buildscript {
    //...
    repositories {
        //...
        mavenCentral()
    }
    //...
}
  1. Add the library dependency to your module-level build.gradle file.
dependencies {
    //...
    implementation "com.paulrybitskyi.valuepicker:valuepicker:1.0.3"
    //...
}

Usage

Basic usage of the ValuePickerView involves two steps - declaring a widget inside the XML file of your choice and configuring it in one of the Kotlin/Java classes.

Let's see how we can do that by following the steps listed above:

  1. Declaring a widget inside the XML file.

    XML (click to expand)

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
    
        <!-- Other widgets here -->
    
        <com.paulrybitskyi.valuepicker.ValuePickerView
            android:id="@+id/valuePickerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:vpv_areDividersEnabled="true"
            app:vpv_isInfiniteScrollEnabled="true"
            app:vpv_maxVisibleItems="5"
            app:vpv_textColor="@color/colorAccent"
            app:vpv_dividerColor="@color/colorAccent"
            app:vpv_flingSpeedFactor="0.3"
            app:vpv_textSize="@dimen/date_picker_text_size"
            app:vpv_textTypeface="@font/ubuntu_mono_bold"
            app:vpv_divider="@drawable/custom_divider"
            app:vpv_orientation="vertical"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>

  2. Configuring the widget in one of the Kotlin/Java classes.

    Kotlin (click to expand)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        //...
    
        with(valuePickerView) {
            onItemSelectedListener = ValuePickerView.OnItemSelectedListener { item ->
                // Do something with item
            }
    
            val pickerItems = getPickerItems()
    
            items = pickerItems
            setSelectedItem(pickerItems[2])
        }
    }
    
    
    private fun getPickerItems(): List<Item> {
        return buildList {
            for(number in 1..100) {
                add(
                    PickerItem(
                        id = number,
                        title = number.toString()
                    )
                )
            }
        }
    }

    Java (click to expand)

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        ValuePickerView valuePickerView = view.findViewById(R.id.valuePickerView);
        valuePickerView.setOnItemSelectedListener((item) -> {
            // Do something with item
        });
    
        final ArrayList<Item> pickerItems = getPickerItems();
    
        valuePickerView.setItems(getPickerItems());
        valuePickerView.setSelectedItem(pickerItems.get(2));
    }
    
    
    private ArrayList<Item> getPickerItems() {
        final ArrayList<Item> pickerItems = new ArrayList<>(100);
    
        for(int i = 1; i <= 100; i++) {
            pickerItems.add(
                new PickerItem(
                    i,
                    String.valueOf(i)
                )
            );
        }
    
        return pickerItems;
    }

Advanced Usage

See the Sample app.

License

ValuePicker is licensed under the Apache 2.0 License.