I'm trying to create two screen UI. The first screen has an editText
and a Submit
button. The second screen has a textView
to display text that would be entered in the first screen.
Here is the code I used, but the second screen does not show inputted text from the first screen.
MainActivity.kt [First Screen]:
package com.rishis.testinginputtext
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.rishis.testinginputtext.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
binding.button.setOnClickListener{
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
setContentView(binding.root)
}
}
SecondActivity.kt [Second Screen]:
package com.rishis.testinginputtext
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.util.AttributeSet
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.rishis.testinginputtext.databinding.ActivityMainBinding
import com.rishis.testinginputtext.databinding.ActivitySecondBinding
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivitySecondBinding.inflate(layoutInflater)
val oneBinding = ActivityMainBinding.inflate(layoutInflater)
//textView refers to textView in secondActivity
val textView = binding.textView
//editText refers to EditText in mainActivity
val editText = oneBinding.EditText
textView.setText(editText.text.toString())
setContentView(binding.root)
}
}
How should I display the text entered in the first screen to be shown in the second screen?