Android NumberPicker Example

activity_main.xml
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     tools:context=".MainActivity"     android:background="#eff4f5"     >     <NumberPicker         android:id="@+id/np"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         />     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_toRightOf="@id/np"         android:padding="15dp"         android:layout_marginLeft="15dp"         android:textSize="20dp"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.graphics.Color; import android.os.Bundle; import android.app.Activity; import android.widget.NumberPicker; import android.widget.RelativeLayout; import android.widget.TextView;   public class MainActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the widgets reference from XML layout         simpulan RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);         NumberPicker np = (NumberPicker) findViewById(R.id.np);         simpulan TextView tv = (TextView) findViewById(R.id.tv);          // Sets the minimum value of the picker.         np.setMinValue(0);         // Sets the maximum value of the picker.         np.setMaxValue(10);          // Set a value change listener for number picker         np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {             @Override             public void onValueChange(NumberPicker picker, int oldVal, int newVal) {                 // Display the old and new value on TextView                 tv.setText("VALUE CHANGED \nOLD VALUE : "                         + oldVal + "\nNEW VALUE : " +newVal);             }         });    } } 
More android examples

Komentar