Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to NumberPicker for API 8 [duplicate]

Tags:

android

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.

like image 716
Mick Avatar asked Dec 27 '22 18:12

Mick


1 Answers

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

like image 192
Yalla T. Avatar answered Jan 09 '23 12:01

Yalla T.