0

I am unable to pass data from (EditText) one activity to another activity.

<EditText
        android:id="@+id/inputText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/eg_raaz"
        android:inputType="textPersonName"
        android:text="@string/name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.059"
        android:autofillHints="" />

ActivityMain.kt

package com.example.bday

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View


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

    fun createCard(view: View) {
        var name=inputText.editableText.toString(); //here error occurs
    }
}

I'd used EditText Id as "inputText" but I am unable to access it from ActivityMain.kt .

jaggu
  • 1
  • 3

1 Answers1

0

First, you need to correctly reference your inputText layout.
You can do it by adding a synthetic import on top of your MainActivity class:

import kotlinx.android.synthetic.main.activity_main.*

Anyway, since the synthetic import method is deprecated you may wanna use one of two methods:

  1. The findViewById method:
fun createCard(view: View) {
    var name=findViewById<EditText>(R.id.inputText).editableText.toString();
    ...
}
  1. The view binding method from the Android Jetpack (newest and suggested):
private lateinit var binding: ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
    }

    fun createCard(view: View) {
        var name=binding.inputText.editableText.toString();
        ...
    }
}

For more information about view binding and how to set it up correctly see here

Once you have a valid reference to the inputText field you can pass its data by using the putExtra method:

fun createCard(view: View) {
    var name=binding.inputText.editableText.toString();

    val intent = Intent(this, NextActivity::class.java)
    intent.putExtra("name", name)

    startActivity(intent)
}

You can then retrieve the name retrieve it in NextActivity with the getStringExtra method:

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

    val name = intent.getStringExtra("name") ?: ""
}
lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
  • @user:13211263 @luca_999 unresolved reference kotlinx is showing when I am importing `import kotlinx.android.synthetic.main.activity_main.*` – jaggu May 14 '21 at 16:40
  • kotlinx synthetic is deprecated, use view binding or findViewById (easiest) and it will work – Merthan Erdem May 14 '21 at 18:37
  • @MerthanErdem I was adapting to the user's approach. Anyway, I've extended my answer with a longer explanation. Thank you for the tip. – lpizzinidev May 15 '21 at 10:46