Title: how do i save / use user input in android kotlin
Tags: android;kotlin;input
Question: i'm trying to write my very first android app.  the only programing i have done in the past was some html 4 many years ago (before cms was a thing)

it is essentially a check list but i want to be able to have multiple lists seperated by a list name supplied by the user.  i have an input text box called "island name" but i cant figure out how to capture that text from the user and save it for future use...

``` <?xml version="1.0" encoding="utf-8"?&gt;
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="189dp"
        android:text="temp call islander page"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" /&gt;

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="125dp"
        android:layout_marginBottom="271dp"
        android:onClick="loadHome"
        android:text="home"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" /&gt;

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:ems="10"
        android:hint="island name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" /&gt;

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="7dp"
        android:layout_marginBottom="8dp"
        android:text="north"
        app:layout_constraintBottom_toTopOf="@+id/checkBox2"
        app:layout_constraintEnd_toEndOf="@+id/checkBox2" /&gt;

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginBottom="88dp"
        android:text="south"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/editText" /&gt;


    println("your island name is $name")

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="19dp"

        android:text="you entered $name"
        app:layout_constraintStart_toEndOf="@+id/checkBox2"
        app:layout_constraintTop_toBottomOf="@+id/checkBox2" /&gt;

</androidx.constraintlayout.widget.ConstraintLayout&gt;
```

Comment: You can't write Kotlin code in the middle of your XML. Program logic is not defined by layout files. This XML is merely a text document describing a particular layout of views. Your Kotlin or Java code is used to define an Activity and Fragments, which read the XML file at run-time to create ("inflate") a view hierarchy. You need to go through one of the introductory tutorials before we can really help you with any of your questions in a practical way. https://developer.android.com/training/basics/firstapp


Here is another answer: For those who is experiencing the same issue you can use sharedPreferences. As an example:
```private lateinit var sp: SharedPreferences
private lateinit var editor: SharedPreferences.Editor

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    nameEditText = findViewById(R.id.etName)
    ageEditText = findViewById(R.id.etAge)
    sp = getSharedPreferences(&quot;my_sf&quot;, MODE_PRIVATE)
    editor = sp.edit()
}

override fun onPause() {
    super.onPause()

    val name = nameEditText.text.toString()
    val age = ageEditText.text.toString().toInt()

    editor.apply {
        putString(&quot;my_name&quot;, name)
        putInt(&quot;my_age&quot;, age)
        commit()
    }
}

override fun onResume() {
    super.onResume()

    val name = sp.getString(&quot;my_name&quot;, null)
    val age = sp.getInt(&quot;my_age&quot;, 0)

    nameEditText.setText(name)
    if (age!=0) ageEditText.setText(age.toString())
}
```


Here is another answer: To put it simply, in Android dev (Android Studio) you have your view, which is your .xml file and your program logic is written in your .kt or .java file then you refer to your respective view from the .kt oe ,java file.

e.g

activity_main.xml

```<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"&gt;


    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textView"
        android:layout_height="wrap_content"
        android:layout_width="200dp"/&gt;
    <Button
        android:id="@+id/saveBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" /&gt;

</LinearLayout&gt;
```

MainActivity.kt :

```    import kotlinx.android.synthetic.main.activity_main.* //this will reference all views in activity_main - 
//Android studio usually auto imports this when you simply type control name

    class MainActivity : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            saveBtn.setOnClickListener { 
                saveDetails()
            }
        }

        fun saveDetails(){
            var userText = textView.text.toString()
            textView4.setText(userText)
            //do something with text
        } 
}
```

Comment for this answer: thank you for your response.  i must confess i've been playing with it for a couple days now and i keep getting an unresolved reference error for both "text" (in var userText = textView.text.toString) and  "textView4" (in textView4.setText(userText)    i'm sure i'm missing something very simple here...

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/07/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><kotlin><input>', 'Title': 'how do i save / use user input in android kotlin', 'ViewCount': 1578, 'id': 61666079, 'user_AboutMe': None, 'user_AccountId': 18490867.0, 'user_DisplayName': 'mark sergeant', 'user_Reputation': 33.0, 'user_Views': 8.0, 'user_id': 13470532.0}}