title: How to get variable's value in a lambda expression from outside the function in Kotlin
body: <p>I'm trying to get the value of the variable "finalvalue" in order to use it in another function, but it's not visible/accessible from outside the "edittext.onClickListener()" function.</p>
  
  <p>I tried to declare it as a companion object but still I couldn't make it.
  Also, declaring it in main class makes it visible, but I want the updated value of this variable which is set in the "edittext.onClickListener()" function.</p>
  
  <p>What am I missing? Thanks.</p>
  
  <p>UPDATE: Some words about the program. It's just a simple Countdown timer. edittext.getText() takes the input (how many seconds), then convert it to Integer. In the next function named "startbutton.onClickListener" I multiply it by 1000 to convert it to seconds and start the countdown.
  the problem is that I can't get the updated value of "finalvalue". I only have access to the initial value of the variable.</p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
  
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      var finalvalue = 30
  
      val day_night=findViewById&lt;Button&gt;(R.id.day_night)
      val setbutton=findViewById&lt;Button&gt;(R.id.setbutton)
      val stopbutton=findViewById&lt;Button&gt;(R.id.stopbutton)
      val startbutton=findViewById&lt;Button&gt;(R.id.startbutton)
      val antistrofimetrisi=findViewById&lt;TextView&gt;(R.id.antistrofimetrisi)
      val edittext=findViewById&lt;EditText&gt;(R.id.edittext)
  
  
      var edittextIsVisible = false
      antistrofimetrisi.setText("0")
  
      setbutton.setOnClickListener() {
          if(edittextIsVisible == false) {
              edittext.setVisibility(View.VISIBLE)
              edittextIsVisible = true }
  
          else {
              edittext.setVisibility(View.GONE)
              edittextIsVisible = false }
  
      }
  
  
      edittext.setOnEditorActionListener() { v, actionId, event -&gt;
          if (actionId == EditorInfo.IME_ACTION_DONE &amp;&amp; edittext.getText().toString() == "")
          {
              Toast.makeText(this, "Εισάγετε έγκυρο αριθμό δευτερολέπτων", Toast.LENGTH_LONG).show()
              true
          }
  
          else
  
          {
              var value: String = edittext.getText().toString()
              var finalvalue = Integer.parseInt(value)
              antistrofimetrisi.setText(finalvalue.toString())
              Toast.makeText(
                  this,
                  "Πιέστε START για αντίστροφη μέτρηση από τα " + finalvalue + " δευτερόλεπτα",
                  Toast.LENGTH_SHORT
              ).show()
  
  
  
              false
          }
      }
  
  
  
      startbutton.setOnClickListener()
      {
          var secondsLeft: Int = 0
          var finalseconds = finalvalue*1000
          val timer = object: CountDownTimer(finalseconds.toLong(), 100) {
              override fun onTick(millisUntilFinished: Long) {
  
                  if (Math.round(millisUntilFinished.toFloat() / 1000.0f) != secondsLeft)
                  {
                      secondsLeft = Math.round(millisUntilFinished.toFloat() / 1000.0f)
                      antistrofimetrisi.setText(secondsLeft.toString())
                  }
  
  
  
                  var timeleft = millisUntilFinished / 1000
                  antistrofimetrisi.setText(timeleft.toString()) }
  
  
  
              override fun onFinish() {
                  antistrofimetrisi.setText("0")
                  start()
              }
          }.start()
  
  
          stopbutton.setOnClickListener()
          {
              timer.cancel()
              antistrofimetrisi.setText("0")
          }
      }
  </code></pre>