title: How do I call a function from a file in an activity with no class Android Studio?
body: <p>I want to be able to have neater code and have a Utility file for all sorts of functions and things. I want to know how to call functions IN an activity FROM a .Kt file not a class.</p>
  <p>I haven't been successful in this direction. Any examples I've found were very complex and/or unsuccessful. Additionally, when I throw some functions into a file or even a Class folder I get errors because it looses references that the activities gives automatically, such as referencing &quot;this&quot;.</p>
  <p>Example, how do I call one of the following in main.Kt?</p>
  <p><strong>Utilities.Kt</strong> File:</p>
  <pre><code>package ...
  
  imports ...    
  
      // Hide keyboard utilities
  private fun Activity.hideKeyboard() {
      hideKeyboard(currentFocus ?: View(this))
  }
  
  @SuppressLint(&quot;ServiceCast&quot;)
  fun Context.hideKeyboard(view: View) {
      val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as 
      InputMethodManager
      inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
  }
  
      // Alert dialogue for null/empty field
  private fun nullEmptyAlert() {
      val box = AlertDialog.Builder(ContextThemeWrapper(this, R.style.AlertDialogCustom))
  
      with(box) {
          box.setTitle(&quot;Empty Field!&quot;)
          box.setMessage(&quot;Calculator cannot compute values with empty field. Please enter unit dimension in mm.&quot;)
          box.setPositiveButton(&quot;OK&quot;) { _, _ -&gt; }
          box.show()
      }
  }
  </code></pre>