Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a number picker dialog?

I want to be able to create a Dialog that allows the user to pick a number from a specified range.

I know that there are existing widgets(like those from quietlycoding and the one by SimonVT) that already does this but I'm having hard time integrating those properly into my application. Also, those are primarily widgets. I want something that is very similar to the one in the android developers page tutorials.

I also checked the documentation for the NumberPicker and it said to go check the TimePicker and DatePicker for examples but they only show how to use Time and Date pickers and I'm having a hard time feeling my way around the code and trying to convert the Time Picker to just a normal Number Picker. Does anyone have any idea where to start? I've been looking for solutions for the last 3 hours to no avail.

like image 863
Razgriz Avatar asked Jul 23 '13 08:07

Razgriz


People also ask

How do you make a number picker horizontal?

In fact, you can. Add android:orientation="horizontal" in the XML and it is horizontal.

What is number picker in Android Studio?

↳ android.widget.NumberPicker. A widget that enables the user to select a number from a predefined range. There are two flavors of this widget and which one is presented to the user depends on the current theme.

How to use numberpicker in word?

First we can use numberpicker just within our page directly. The user can scroll through the numbers and pick. As the user scrolls through, the current scroll value is automatically set to a button. Then the second way is to use a number picker dialog. We have two types of dialogs: integer number picker dialog and decimal number picker dialog.

Should I use a number picker or spinner in a dialog?

Consider using a Spinner instead of a Number Picker in a Dialog. It's not exactly what was asked for, but it's much easier to implement, more contextual UI design, and should fulfill most use cases. The equivalent code for a Spinner is:

How do I display the date picker in a dialogfragment?

Once you've defined a DialogFragment like the one shown above, you can display the date picker by creating an instance of the DialogFragment and calling show (). For example, here's a button that, when clicked, calls a method to show the dialog: When the user clicks this button, the system calls the following method:

Which dialogfragment should I use to host the pickers?

We recommend that you use DialogFragment to host each time or date picker. The DialogFragment manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large screens.


1 Answers

I have made a small demo of NumberPicker. This may not be perfect but you can use and modify the same.

public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener {     private static TextView tv;     static Dialog d ;     @Override     public void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         tv = (TextView) findViewById(R.id.textView1);         Button b = (Button) findViewById(R.id.button11);          b.setOnClickListener(new OnClickListener()          {              @Override             public void onClick(View v) {                  show();             }             });            }      @Override     public void onValueChange(NumberPicker picker, int oldVal, int newVal) {           Log.i("value is",""+newVal);       }      public void show()     {           final Dialog d = new Dialog(MainActivity.this);          d.setTitle("NumberPicker");          d.setContentView(R.layout.dialog);          Button b1 = (Button) d.findViewById(R.id.button1);          Button b2 = (Button) d.findViewById(R.id.button2);          final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);          np.setMaxValue(100);          np.setMinValue(0);          np.setWrapSelectorWheel(false);          np.setOnValueChangedListener(this);          b1.setOnClickListener(new OnClickListener()          {           @Override           public void onClick(View v) {               tv.setText(String.valueOf(np.getValue()));               d.dismiss();            }               });          b2.setOnClickListener(new OnClickListener()          {           @Override           public void onClick(View v) {               d.dismiss();            }               });        d.show();       } } 

activity_main.xml

<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=".MainActivity" >      <TextView         android:id="@+id/textView1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/hello_world" />      <Button         android:id="@+id/button11"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_centerHorizontal="true"         android:text="Open" />  </RelativeLayout> 

dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >      <NumberPicker         android:id="@+id/numberPicker1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentTop="true"         android:layout_centerHorizontal="true"         android:layout_marginTop="64dp" />      <Button         android:id="@+id/button2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/numberPicker1"         android:layout_marginLeft="20dp"         android:layout_marginTop="98dp"         android:layout_toRightOf="@+id/numberPicker1"         android:text="Cancel" />      <Button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button2"         android:layout_alignBottom="@+id/button2"         android:layout_marginRight="16dp"         android:layout_toLeftOf="@+id/numberPicker1"         android:text="Set" />  </RelativeLayout> 

Edit:

under res/values/dimens.xml

<resources>      <!-- Default screen margins, per the Android Design guidelines. -->     <dimen name="activity_horizontal_margin">16dp</dimen>     <dimen name="activity_vertical_margin">16dp</dimen>  </resources> 
like image 136
Raghunandan Avatar answered Sep 23 '22 10:09

Raghunandan