Is it possible to make a horizontal NumberPicker?

Android’s NumberPicker widget is vertical, with buttons to increase and decrease above and below the number.

Is it possible to make it horizontal? Using the buttons to the left and right of the number?

+15
source share
6 answers

You cannot change the NumberPicker AFAIK, but writing your own should be pretty simple.

+11
source

One RelativeLayout 2 Buttons one EditText/TextView with a fancy background between them and there you go. Setting MIN and MAX , overriding onClickListener() s. Some tests. What is it.

+10
source

Nope. In fact, you cannot even use it, even if it appears in the graphic layout builder. He is still protected.

This question has some related information: Android Number Selection Dialog Box

+2
source

I could not find a reliable solution, so in the end I made my own, but I need it to look like a circular screw, so it adapted for this use case.

You can get the idea from the source though .. Here it is:

https://github.com/milosmns/actual-number-picker

+1
source

android: rotation = "from 90" to 90 degrees, or, as you like, the value may be negative, it will rotate the view, but.

0
source

I was looking for a similar solution and created an implementation based on the screenshot in this question . Keep in mind that this is quite simple and will allow for integers to be taken into account and set the minimum and maximum value. (Any additional features you will need to add yourself.)

Here's how it would look:

horizontal number picker screenshot

You need a layout file and a class file for the custom component, and then you can use it in other layout files (and the class).

Layout File : numberpicker_horizont.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/btn_less" android:layout_width="0sp" android:layout_height="match_parent" android:layout_weight="1" android:text="-" /> <EditText android:id="@+id/et_number" android:layout_width="0sp" android:layout_height="match_parent" android:layout_weight="1" android:textAlignment="center" android:inputType="number" android:text="0" /> <Button android:id="@+id/btn_more" android:layout_width="0sp" android:layout_height="match_parent" android:layout_weight="1" android:text="+" /> </LinearLayout> 

Class File : HorizontalNumberPicker.java

 public class HorizontalNumberPicker extends LinearLayout { private EditText et_number; private int min, max; public HorizontalNumberPicker(Context context, @Nullable AttributeSet attrs) { super(context, attrs); inflate(context, R.layout.numberpicker_horizontal, this); et_number = findViewById(R.id.et_number); final Button btn_less = findViewById(R.id.btn_less); btn_less.setOnClickListener(new AddHandler(-1)); final Button btn_more = findViewById(R.id.btn_more); btn_more.setOnClickListener(new AddHandler(1)); } /*** * HANDLERS **/ private class AddHandler implements OnClickListener { final int diff; public AddHandler(int diff) { this.diff = diff; } @Override public void onClick(View v) { int newValue = getValue() + diff; if (newValue < min) { newValue = min; } else if (newValue > max) { newValue = max; } et_number.setText(String.valueOf(newValue)); } } /*** * GETTERS & SETTERS */ public int getValue() { if (et_number != null) { try { final String value = et_number.getText().toString(); return Integer.parseInt(value); } catch (NumberFormatException ex) { Log.e("HorizontalNumberPicker", ex.toString()); } } return 0; } public void setValue(final int value) { if (et_number != null) { et_number.setText(String.valueOf(value)); } } public int getMin() { return min; } public void setMin(int min) { this.min = min; } public int getMax() { return max; } public void setMax(int max) { this.max = max; } } 

Then include it in other layout files as follows (replacing com.yourpackage with your own package name):

 <com.yourpackage.HorizontalNumberPicker android:id="@+id/np_channel_nr" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

Then you can use the component, like any other component, and use the getValue () method to get the current value:

 // get a reference to the component final HorizontalNumberPicker np_channel_nr = dialogView.findViewById(R.id.np_channel_nr); // use value in your code final int nr = np_channel_nr.getValue(); 
0
source

All Articles