skip to Main Content

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?

2

Answers


  1. You can use the "putExtra" method to send the data to next activity,
    and "getExtra" to get the data in next activity.

    Please Refer the article-

    https://medium.com/@haxzie/using-intents-and-extras-to-pass-data-between-activities-android-beginners-guide-565239407ba0

    Login or Signup to reply.
  2. it’s very simple, you just need to use putExtras and getExtra in your activities

    your first activity is looks like this:

     val textToPass = editText.text.toString()
     
     binding.button.setOnClickListener{
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra("SOME_KEY",textToPass)
            startActivity(intent)
        }
    

    and in a second activity you can get the text by using a getStringExtra and which look like this

     val textToDislplay = intent.getStringExtra("SOME_KEY")
     textView.text = textToDisplay
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search