4

Possible Duplicate:
How would one implement a NumberPicker in Android API 7?

I need to pick a small integer in the range 0 to 12. The space in my UI to do this is restricted and must be wider than it is tall. Is there a widget available in API 8 that will allow me to do this?

EDIT: Re possible duplication of previous SO question. I saw a closely related question, but A) it made no "landscape orientation" restriction and B) A suggestion that code could be copied from api 11 was not followed up with an explanation of exactly how to do it.

Community
  • 1
  • 1
Mick
  • 8,284
  • 22
  • 81
  • 173

2 Answers2

9

The easiest (but also most ugly) would be an edittext with inputtype number.

But making a number picker from scratch isnt that hard.

You just need a Textview which takes a variable as text. Add a + and - button which increment/decrement the variable and call Textview.setText(variable)

final int[] counter = {0};
Button add = (Button) findViewById(R.id.bAdd);
Button sub = (Button) findViewById(R.id.bSub);
TextView display = (TextView) findViewById(R.id.tvDisplay);

add.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    counter[0]++;
    display.setText( "" + counter[0]);
    }
});

sub.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    counter[0]--;
    display.setText( "" + counter[0]);
    }
});

in xml just add 2 buttons with id bAdd and bSub and a textview with id tvDisplay and arrange them like you want

codingmechanic
  • 275
  • 4
  • 7
Yalla T.
  • 3,707
  • 3
  • 23
  • 36
1

Try looking at this Custom Number Picker. I would start with that and simply modify the layout of the control to be horizontally oriented.

Another option is to find the source code for the Android 2.3.4 NumberPicker and NumberPickerButton and copy that. This will also require copying the resources required for those controls. Again, you would then need to modify the button layout to be horizontally oriented.

I would not recommend using the NumberPicker from API 11 or greater. You will find that there are numerous depenencies on newer classes that are not available to you with API 8.

jsmith
  • 4,847
  • 2
  • 32
  • 39