Kardo Kamal Kardo Kamal - 1 month ago 23
Java Question

Trying to setup a NumberPicker to show value in Toast

I'm using the NumberPicker tool in Android and I changed the value of the integer to String to be able to show it in the Toast I used

Integer.toString
method but I don't think the problem is that.

The thing is when I tap my button no matter what value I pick in the NumberPicker it shows (0). I'm using the
.getValue();
as shown below.

How do I make it get the value that I set on NumberPicker?

numPickerMin = (NumberPicker) findViewById(R.id.numberPickerMinute);

//Min
numPickerMin.setMaxValue(60);
numPickerMin.setMinValue(0);
numPickerMin.setWrapSelectorWheel(false);

int getvalueminute = numPickerMin.getValue();
final String getValMin= Integer.toString(getvalueminute);

silentButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Toast.makeText(MainActivity.this,getValMin,Toast.LENGTH_LONG).show();
}
}
);

Answer

You are getting Picker's value after you initialize it so it will always be 0. As I understand your code you want to show correct value in Toast after pressing button? You should move those lines inside OnClickListener:

int getvalueminute = numPickerMin.getValue(); String getValMin= Integer.toString(getvalueminute);