title: How To Get LiveData Value From EditText and Display It In A TextView Using Kotlin? MVVM
body: <h1>Overview:</h1>
  <p>Greetings. In my app the user can enter a number in an EditText and that number is then used as the maximum range for generating random numbers from 1 to (the number the user entered), which should then be displayed in a TextView.</p>
  <h1>Problem:</h1>
  <p>When the number is entered by the user and the button is pressed to generate the random number within the TextView, it doesn't work as intended.</p>
  <p><strong>For instance:</strong></p>
  <p>Initially, the value the LiveData holds for <code>bonusNumber</code> is <strong>1</strong> as it should be (based on the <code>init</code> block), but when the button is pressed to display a random number <strong>0</strong> is displayed after the button is pressed to generate the random number, instead of an actual random number between 1 and whatever the user entered in the EditText.</p>
  <h1>Code:</h1>
  <p><strong>ViewModel:</strong></p>
  <pre><code>class QuickPickViewModel : ViewModel() {
  
      private val repository = Repository()
  
      // LiveData for the number entered by the user
      private var _userBonusNumber = MutableLiveData&lt;Int&gt;()
      val userBonusNumber: LiveData&lt;Int&gt;
          get() = _userBonusNumber
  
      // LiveData for the actual result of the randomly generated bonus number
      private var _bonusNumber = MutableLiveData&lt;Int&gt;()
      val bonusNumber: LiveData&lt;Int&gt;
          get() = _bonusNumber
  
      init {
          _userBonusNumber.value = 1
          _bonusNumber.value = 1
      }
  
      fun getRandomBonusNumber() {
          // Set the MutableLiveData to be displayed in the TextView, to the number the user entered
          _bonusNumber.value = repository.generateBonusRandomNumber(_userBonusNumber.value!!.toInt())
      }
  }
  </code></pre>
  <p><strong>Fragment:</strong></p>
  <pre><code>class QuickPickFragment : Fragment() {
  
      private lateinit var binding: FragmentQuickPickBinding
      private lateinit var viewModel: QuickPickViewModel
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment using data binding
          binding = DataBindingUtil.inflate(
              inflater,
              R.layout.fragment_quick_pick,
              container,
              false
          )
  
          viewModel = ViewModelProvider(this).get(QuickPickViewModel::class.java)
          
          binding.quickPickViewModel = viewModel
          binding.lifecycleOwner = this
  
          return binding.root
      }
  }
  </code></pre>
  <p><strong>XML Views(The Relevant Ones):</strong></p>
  <pre><code> &lt;TextView
                      android:id=&quot;@+id/bonus_result_text_view&quot;
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_marginTop=&quot;16dp&quot;
                      android:text=&quot;@{String.valueOf(quickPickViewModel.bonusNumber)}&quot;
                      android:textAllCaps=&quot;true&quot;
                      android:textStyle=&quot;bold&quot;
                      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                      app:layout_constraintHorizontal_bias=&quot;0.498&quot;
                      app:layout_constraintStart_toStartOf=&quot;parent&quot;
                      app:layout_constraintTop_toBottomOf=&quot;@+id/main_result_text_view&quot; /&gt;
  
      &lt;EditText
                      android:id=&quot;@+id/bonus_set_edit_text&quot;
                      android:layout_width=&quot;120dp&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_margin=&quot;16dp&quot;
                      android:layout_marginStart=&quot;8dp&quot;
                      android:layout_marginEnd=&quot;16dp&quot;
                      android:ems=&quot;10&quot;
                      android:hint=&quot;@string/enter_number_quick_pick_bonus_edit_text_hint&quot;
                      android:importantForAutofill=&quot;no&quot;
                      android:inputType=&quot;number&quot;
                      android:text=&quot;@{String.valueOf(quickPickViewModel.userBonusNumber)}&quot;
                      app:layout_constraintBaseline_toBaselineOf=&quot;@+id/bonus_set_title_text_view&quot;
                      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                      app:layout_constraintStart_toEndOf=&quot;@+id/bonus_set_title_text_view&quot; /&gt;
  
       &lt;Button
                      android:id=&quot;@+id/button&quot;
                      android:layout_width=&quot;0dp&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_marginStart=&quot;16dp&quot;
                      android:layout_marginEnd=&quot;16dp&quot;
                      android:layout_marginBottom=&quot;16dp&quot;
                      android:onClick=&quot;@{() -&gt; quickPickViewModel.getRandomBonusNumber()}&quot;
                      android:text=&quot;Generate&quot;
                      android:textAllCaps=&quot;true&quot;
                      android:textStyle=&quot;bold&quot;
                      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                      app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
  </code></pre>
  <p><strong>Repository:</strong></p>
  <pre><code>class Repository {
      /**
       * Generate random number for the bonus number within a particular range set by user
       *
       * [maxNum]: the number that the user entered which acts as the max range for the random numbers
       */
      fun generateBonusRandomNumber(maxNum: Int): Int {
          val minNum = 1
          // Adding 1[minNum] to the bound makes sure the number the person entered is inclusive
          return Random().nextInt(((maxNum - minNum)) + minNum)
      }
  }
  </code></pre>