A simple customizable NumberPicker plugin for Android

Overview

NumberPicker

Build Status Maven Central Android Arsenal

A simple customizable NumberPicker for Android.

Installation

via Gradle:

compile 'com.github.travijuu:numberpicker:1.0.7'

or Maven:

<dependency>
  <groupId>com.github.travijuu</groupId>
  <artifactId>numberpicker</artifactId>
  <version>1.0.7</version>
  <type>aar</type>
</dependency>

Usage

Add NumberPicker component in your XML layout

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:numberpicker="http://schemas.android.com/apk/res-auto"
    tools:context="com.travijuu.numberpicker.sample.MainActivity">

    <com.travijuu.numberpicker.library.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="130dp"
        android:layout_height="40dp"
        numberpicker:min="0"
        numberpicker:max="10"
        numberpicker:value="-5"
        numberpicker:unit="1"
        numberpicker:focusable="false"
        numberpicker:custom_layout="@layout/number_picker_custom_layout" />

</LinearLayout>

MainActivity.java

import com.travijuu.numberpicker.library.NumberPicker;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NumberPicker numberPicker = (NumberPicker) findViewById(R.id.number_picker);
        numberPicker.setMax(15);
        numberPicker.setMin(5);
        numberPicker.setUnit(2);
        numberPicker.setValue(10);
    }
}

XML Attributes

Name Type Default
min int 0
max int 999999
value int 1
unit int 1
focusable boolean false
custom_layout layout @layout/number_picker_layout

Layout Customization

if you want to customize your NumberPicker layout you can create your own.

IMPORTANT! This layout should contains at least 3 items with given Ids:

  • Button (@+id/increment)
  • Button (@+id/decrement)
  • TextView (@+id/display)

Note: You can see an example layout in both sample and library modules.

Example XML layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="130dp"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:background="@android:color/white">

    <Button
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:textColor="@android:color/black"
        android:background="@null"
        android:id="@+id/decrement"
        android:textStyle="bold"
        android:text=""/>

    <TextView
        android:layout_width="70dp"
        android:background="@android:color/white"
        android:layout_height="match_parent"
        android:text="1"
        android:textColor="@android:color/black"
        android:inputType="number"
        android:id="@+id/display"
        android:gravity="center"
        />
    <Button
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:textSize="25sp"
        android:textColor="@android:color/black"
        android:background="@null"
        android:id="@+id/increment"
        android:text="+"/>
</LinearLayout>

Methods

Here is the list of methods with definitions.

setMin(int value)

Sets minimum value allowed

getMin()

Gets minimum value

setMax(int value)

Sets maximum value allowed

getMax()

Gets maximum value allowed

setUnit(int value)

Sets unit value for increment/decrement operation

getUnit()

Gets unit value

setValue(int value)

Sets NumberPicker current value

getValue()

Gets NumberPicker current value

setActionEnabled(ActionEnum action, boolean enabled)

Enables or disables Increment/Decrement buttons

setDisplayFocusable(boolean focusable)

Enables or disables NumberPicker editable via keyboard

increment()

NumberPicker will be incremented by defined unit value

increment(int unit)

NumberPicker will be incremented by given unit value

decrement()

NumberPicker will be decremented by defined unit vale

decrement(int unit)

NumberPicker will be decremented by given unit value

refresh()

NumberPicker will be refreshed with already defined value

clearFocus()

NumberPicker will lose the focus

valueIsAllowed(int value)

Checks whether given value is acceptable or not

setLimitExceededListener(LimitExceededListener limitExceededListener)

setValueChangedListener(ValueChangedListener valueChangedListener)

setOnEditorActionListener(OnEditorActionListener onEditorActionListener)

setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener)

Listeners

LimitExceededListener

This is triggered when you try to set lower or higher than the given min/max limits

public class DefaultLimitExceededListener implements LimitExceededListener {

    public void limitExceeded(int limit, int exceededValue) {

        String message = String.format("NumberPicker cannot set to %d because the limit is %d.", exceededValue, limit);
        Log.v(this.getClass().getSimpleName(), message);
    }
}

ValueChangedListener

This is triggered when the NumberPicker is incremented or decremented.

Note: setValue method will not trigger this listener.

public class DefaultValueChangedListener implements ValueChangedListener {

    public void valueChanged(int value, ActionEnum action) {

        String actionText = action == ActionEnum.MANUAL ? "manually set" : (action == ActionEnum.INCREMENT ? "incremented" : "decremented");
        String message = String.format("NumberPicker is %s to %d", actionText, value);
        Log.v(this.getClass().getSimpleName(), message);
    }
}

OnEditorActionListener

This is triggered when you click "done" button on keyboard after you edit current value.

Note: "done" button can be changed on xml so this listener should be overrided according to new IME option.

OnFocusChangeListener

This is triggered when clearFocus() is called which helps to set new value when the focus lost

Comments
  • error classes could not be instantiated

    error classes could not be instantiated

    i got error message like this when try your lib, how to fix it? thanks

    java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText at com.travijuu.numberpicker.library.NumberPicker.initialize(NumberPicker.java:90) at com.travijuu.numberpicker.library.NumberPicker.(NumberPicker.java:59) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:475) at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:262) at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:220) at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) at android.view.LayoutInflater.rInflate(LayoutInflater.java:834) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) at android.view.LayoutInflater.rInflate(LayoutInflater.java:834) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) at android.view.LayoutInflater.rInflate(LayoutInflater.java:834) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.inflate(LayoutInflater.java:518) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429) at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:368) at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:567) at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:549) at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:863) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:549) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$1(RenderTask.java:680) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

    opened by aryayudha 4
  • Can't able to listen to value changed listener from outside the library

    Can't able to listen to value changed listener from outside the library

    i have used this library for number selection from one range to another range.I want to listen to value changed listener in my fragment,so that i can enable and disable proceed button.

    opened by nathansdev 1
  • INFLATE EXCEPTION

    INFLATE EXCEPTION

    Hours I'm searching for solutions.. please where is the problem ? android.view.InflateException: Binary XML file line #93: Binary XML file line #93: Error inflating class com.travijuu.numberpicker.library.NumberPicker Caused by: android.view.InflateException: Binary XML file line #93: Error inflating class com.travijuu.numberpicker.library.NumberPicker Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at android.view.LayoutInflater.createView(LayoutInflater.java:647) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423)

    opened by Dave181295 0
  • Unable to get click listener of number picker

    Unable to get click listener of number picker

    Hello Sir, How to get click event of - button and +button. Unable to get clickListener of that button. Thanks in advance.

    Thanks & Regards, Santosh Pardeshi

    opened by rockstarsantosh1994 0
  • Erro on use programmatically

    Erro on use programmatically

    code: NumberPicker numberPicker = new NumberPicker(opcionais.getContext()); numberPicker.setValue(itens.get(i).quantidade);

    Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at com.travijuu.numberpicker.library.NumberPicker.refresh(NumberPicker.java:113) at com.travijuu.numberpicker.library.NumberPicker.setValue(NumberPicker.java:155)

    opened by d3roch4 0
  • any view from inc/dec button

    any view from inc/dec button

    I wanted to use the AppCompatImageButton for inc/dec buttons. This caused the

    java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
    
    opened by akaGelo 2
  • NumberPicker fills black color to whole layout

    NumberPicker fills black color to whole layout

    on a few devices, if we add number picker to a layout. the whole screen background becomes black. even if I used android:background to the main layout still, it's not working. you can check this on nexus4 device

    opened by zeus512 0
Releases(v1.0.7)
Owner
Erkin Çakar
travego
Erkin Çakar
Now deprecated. A small Android library allowing you to have a smooth and customizable horizontal indeterminate ProgressBar

Description Small library allowing you to make a smooth indeterminate progress bar. You can either user your progress bars and set this drawable or us

Antoine Merle 4.4k Dec 30, 2022
Android library for showing progress in a highly customizable pie.

ProgressPieView Android library for showing progress in a highly customizable pie. Choose from the broad spectre of styleable elements: ppvStrokeWidth

Filip Puđak 399 Dec 29, 2022
PowerSpinner - 🌀 A lightweight dropdown popup spinner, fully customizable with an arrow and animations for Android.

PowerSpinner - ?? A lightweight dropdown popup spinner, fully customizable with an arrow and animations for Android.

Jaewoong Eum 881 Dec 30, 2022
Customizable bouncing dots for smooth loading effect. Mostly used in chat bubbles to indicate the other person is typing.

LoadingDots for Android Customizable bouncing dots view for smooth loading effect. Mostly used in chat bubbles to indicate the other person is typing.

Eyal Biran 162 Dec 2, 2022
Completely customizable progress based loaders drawn using custom CGPaths written in Swift

FillableLoaders Completely customizable progress based loaders drawn using custom CGPaths written in Swift Waves Plain Spike Rounded Demo: Changelog:

Pol Quintana 2.1k Dec 31, 2022
A customizable indeterminate progress bar

DilatingDotsProgressBar Installation compile 'com.github.justzak:dilatingdotsprogressbar:1.0.1' Usage <com.zl.reik.dilatingdotsprogressbar.DilatingDo

Zachary Reik 628 Sep 24, 2022
Completely customizable progress based loaders drawn using custom CGPaths written in Swift

FillableLoaders Completely customizable progress based loaders drawn using custom CGPaths written in Swift Waves Plain Spike Rounded Demo: Changelog:

Pol Quintana 2.1k Dec 31, 2022
Beautiful progress bar with segments. Highly customizable. Fully written with Jetpack Compose

?? SegmentedProgressBar ?? Beautiful progress bar split into several segments. Highly customizable. Fully written with Jetpack Compose. Why this libra

Stephen Vinouze 90 Dec 15, 2022
a simple loadingview for android with animation

LoadingView a simple loadingview for android with animation 简单的带有动画效果的加载控件 #Preview #Gradle compile 'com.ldoublem.loadingview:loadingviewlib:1.0' Id P

ldoublem 2.7k Jan 2, 2023
A simple library for creating circular progressbars for Android

CircleProgressBar A simple library for creating circular progressbars for Android. Examples Installation Get it via gradle: implementation 'com.emreda

Emre 96 Nov 1, 2022
A simple progress loader inspired by Can you Code this UI? Volume 6! - https://stories.uplabs.com/can-you-code-this-ui-volume-6-7bd09fa6dd92#.nyh2zhpvb

SlidingSquaresLoader Sliding Square Loader - A simple progress loader inspired by Can you Code this UI? Volume 6! Gradle Step 1. Add the JitPack repos

Hamza Fetuga 151 Jul 26, 2022
Arc pointer - simple customized progress bar in the form of an arch

ArcPointer Simple customized progress bar in the form of an arch Demo Quick start Step 1 Gradle: compile 'io.github.dvegasa:arcpointer:1.0.2' Maven:

Ed Khalturin 79 Nov 22, 2022
A simple lib to create a ring-like progress view with corner edges

ProgressRingView Installation Gradle: dependencies { compile 'com.github.flepsik:progress-ring-view:1.2.1' } Maven: <dependency> <groupId>com.g

null 71 Dec 5, 2021
A simple and flexible Fillable Progress Layout written in Kotlin

FillProgressLayout ?? A simple and flexible Fill Progress Layout written in Kotlin ?? Netflix button animation using FillProgressLayout Support Librar

null 78 Sep 20, 2022
Simple Progress View that you can compare things, like statistics of a Football match

Simple Progress View that you can compare things, like statistics of a Football match

Kostas Antoniou 29 Jun 8, 2022
[Android] Round Corner Progress Bar Library for Android

RoundCornerProgressBar Round corner is cool. Let's make your progress bar to round corner Colorful progress bar with round corner on progress which yo

Akexorcist 2.3k Dec 31, 2022
[Android] Round Corner Progress Bar Library for Android

RoundCornerProgressBar Round corner is cool. Let's make your progress bar to round corner Colorful progress bar with round corner on progress which yo

Akexorcist 2.3k Jan 7, 2023
A beautiful, slim Android ProgressBar.

Android NumberProgressBar The NumberProgressBar is a bar, slim and sexy (every man wants! ). I decided to do this because I was really tired of androi

代码家 6k Jan 7, 2023
An android library to display a progressbar that goes around an image.

android-square-progressbar First things first This library is setup to work with the Android Studio and Gradle. If you're using the Eclipse environmen

Yannick Signer 1.3k Nov 15, 2022