1

So I have some textViews that their inputs are numbers, but in order to be less prone to error I want to replace it with numberPickers.

What I have done

  • TextViews work
  • NumberPickers Work
  • Any math and calculation works

What I need:

  • To replace the input method

  • This is what I have for my textViews

    when {
    
            time.text.isEmpty() && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) ->
                calculatePace(null, distance.text.toString().toDouble(), pace.text.toString())
    
            distance.text.isEmpty() && (time.text.isNotEmpty() && pace.text.isNotEmpty()) ->
                calculatePace(time.text.toString(), null, pace.text.toString())
    
            pace.text.isEmpty() && (time.text.isNotEmpty() && distance.text.isNotEmpty()) ->
                calculatePace(time.text.toString(), distance.text.toString().toDouble(), null)
    
    
            else -> {
                Toast.makeText(this, "Please check fields",
                        Toast.LENGTH_SHORT).show()
            }
        }
    

These are my pickers:

    val pickerMinutes = numberPicker as NumberPicker
    val pickerSeconds = numberPickerSeconds as NumberPicker
    pickerMinutes.minValue = 0
    pickerMinutes.maxValue = 60
    pickerMinutes.wrapSelectorWheel = false
    pickerMinutes.isEnabled = true

    pickerSeconds.minValue = 0
    pickerSeconds.maxValue = 60
    pickerSeconds.wrapSelectorWheel = false
    pickerSeconds.isEnabled = true

Any help would be very appreciated

Thanks

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
Joedk
  • 11
  • 3
  • Not quite clear what is it you want ? as far I have been realised , do you want the input field as a number picker not TextView and need same calculation , is that it ? @Joedk – Wubbalubbadubdub Oct 18 '17 at 09:19
  • Hi Sorry, I already have the calculations (function) done. But at the moment the inputs are textViews and I want to replace them for number pickers :) – Joedk Oct 18 '17 at 09:22
  • If you need your input to be only numbers, you can set the EditText inputType property to "number". "android:inputType="number" – Rachit Oct 18 '17 at 09:27
  • I know that, but I need to user numberPicker (because of the reason I mentioned on my question), in order to avoid user input error since this involves time, so I always need the : separator – Joedk Oct 18 '17 at 09:30

1 Answers1

0

If you look at the NumberPicker API , you will see there is a method getValue() which returns selected value , which is int return type . you can use toDouble() as your calculation method is taking double type

N.B as NumberPicker always works with integer I think you can modify your calculation method , as it will never get Double from NumberPicker

please confirm , if I didn't get what you want

Wubbalubbadubdub
  • 2,415
  • 1
  • 24
  • 36
  • this give me error pickerMinutes.value && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) -> I tried it before. Should I show you perhaps the whole class? (I am using Kotlin, thats why I am using .value and not getValue – Joedk Oct 18 '17 at 09:34
  • this would help , give me your class with a https://pastebin.com/ link. this will help me dig deep – Wubbalubbadubdub Oct 18 '17 at 09:36
  • Going to do it. :) – Joedk Oct 18 '17 at 09:38
  • https://pastebin.com/MpfRyrwq ask me any questions. Right now what I have is passing the values from the pickers to the textviews, but thats not optimal. I want the inputs to be only from my pickers Thanks – Joedk Oct 18 '17 at 09:41
  • I start solving this error , just need know which error you get after using pickerMinutes and pickerSeconds – Wubbalubbadubdub Oct 18 '17 at 09:46
  • I don't have any error in the code I sent you. But if I replace time.text.isEmpty() for pickerMinutes.value I get type mismatch required Boolean – Joedk Oct 18 '17 at 09:51
  • Do you have any question @Vutka Bilai ? – Joedk Oct 18 '17 at 09:59
  • Look here why you are getting type mismatch error , you are looking for isEmpty() method which is part of Textview class . If you use NumberPicker you don't need this validation . as you set pickerMinutes.minValue = 0 . so by default it will be set to 0 .(distance.text.isNotEmpty() && pace.text.isNotEmpty()) -> calculatePace(null, distance.text.toString().toDouble(), pace.text.toString()) remove time.text.isEmpty() – Wubbalubbadubdub Oct 18 '17 at 10:44
  • I did not use .isEmpty on my numberPicker. Look what I used :P The thing is I don't want to use my text Views anymore. I know how to pass my Number Picker values to the text views. What I want to do is replace the text views for my number pickers. – Joedk Oct 18 '17 at 10:50
  • I get it :p ; i know what you did pickerMinutes.value && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) -> calculatePace(null, distance.text.toString().toDouble(), pace.text.toString()) what does pickerMinutes.value retruns ?? it returns int , so how "&&" operators works on boolean and int . it is not possible . so when you need number picker value for calculation use pickerMinutes.value . you don't need to validate is it empty or not – Wubbalubbadubdub Oct 18 '17 at 10:51
  • So I just do pickerMinutes.value.(distance.text.isNotEmpty() && pace.text.isNotEmpty()) -> calculatePace(null, distance.text.toString().toDouble(), pace.text.toString()) ? – Joedk Oct 18 '17 at 11:03
  • error as well. I think I am missing something basic here :/ – Joedk Oct 18 '17 at 11:14
  • Any ideas @Vutka? :) – Joedk Oct 18 '17 at 13:48