Android Create Number Picker Widget Example Tutorial

How to use number picker inside android application to show numbers between a define range.

In this tutorial we are creating an Number picker widget with minimum range starts from 0 and maximum range to 100. That means app user can only able to select between 0 to 100 from number picker. So here is the complete step by step tutorial for Android Create Number Picker Widget Example Tutorial.

android-project-download-code-button

Android Create Number Picker Widget Example Tutorial.

Code for MainActivity.java file.

 package com.numberpicker_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.NumberPicker;
import android.widget.TextView;


public class MainActivity extends Activity {

 NumberPicker numberpicker;
 TextView textview;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 numberpicker = (NumberPicker)findViewById(R.id.numberPicker1);

 textview = (TextView)findViewById(R.id.textView1);
 
 numberpicker.setMinValue(0);
 
 numberpicker.setMaxValue(100);
 
 numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
 @Override
 public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
 
 
 textview.setText("Selected Value is : " + newVal);
 }
 });
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.numberpicker_android_examples.com.MainActivity" >

 <NumberPicker
 android:id="@+id/numberPicker1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/numberPicker1"
 android:layout_centerHorizontal="true"
 android:text="Selected Number Display here"
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:gravity="center"/>
 
</RelativeLayout>

Screenshot:

Android Create Number Picker Widget Example Tutorial

Click here to download Android Create Number Picker Widget Example Tutorial project with source code.