I am Making a Basic form to take user input send it to different Activity and display it there ,When I use edittext to take a input store it in String variable and then send it A blank string gets send,There seems to be no error or warnig in code can someone help,Also while testing the string using a toast if I pass fullname(see code) as Editable and then convert is to String using toString() it works,but if I declare it as a string beforehand and then pass to toast again a blank output This is my FormActivity
package com.example.formapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.textservice.TextInfo
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.google.android.material.textfield.TextInputEditText
import java.util.regex.Pattern
class FormActivity : AppCompatActivity() {
private lateinit var named:EditText
private lateinit var emailadd : EditText
private lateinit var branch : EditText
private lateinit var desc : TextInputEditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_form)
named =findViewById(R.id.name)
emailadd =findViewById(R.id.editTextTextEmailAddress)
branch=findViewById(R.id.branch)
desc =findViewById(R.id.bio)
val fullName = named.text.toString()
val email= emailadd.text
val branchName = branch.text
val biot= desc.text.toString()
val bundle = Bundle()
bundle.putString("key1", named.text.toString())
bundle.putString("email", email.toString())
bundle.putString("branch",branchName.toString())
val intent = Intent(this,DataPoint::class.java)
intent.putExtras(bundle)
val bSumit : Button =findViewById(R.id.submit)
bSumit.setOnClickListener {
val isAllinfoCorrect = Checkinfo();
if(isAllinfoCorrect)
{
startActivity(intent)
Toast.makeText(this,fullName,Toast.LENGTH_LONG).show()
}
else
{
Toast.makeText(applicationContext,"Please Enter the required fields",Toast.LENGTH_LONG).show()
}
}
}
private fun Checkinfo(): Boolean {
var text = named.text.toString()
if(text.isEmpty())
{
named.setError("Field Required")
return false
}
else
{
named.setError(null)
}
text = emailadd.text.toString()
val chekmail = "[a-zA-Z0-9._-] @[a-z] \\.[a-z] "
val expression = "^[\\w.-] @([\\w\\-] \\.) [A-Z]{2,4}$"
val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(text)
if(!matcher.matches() || text.isEmpty())
{
emailadd.setError("Please Enter a Valid Email")
return false
}
else
{
emailadd.setError(null)
}
text=branch.text.toString()
if(text.isEmpty())
{
branch.setError(" Field Required")
return false
}
else
{
branch.setError(null)
}
return true
}
}
This Is my second activity where I am using bundle
package com.example.formapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class DataPoint : AppCompatActivity() {
private lateinit var name : TextView
private lateinit var email : TextView
private lateinit var Branch : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_data_point)
val bundle = intent.extras
val nmaseString= "TEST STRING"
name=findViewById(R.id.username)
email=findViewById(R.id.emailAddress)
Branch = findViewById(R.id.branchname)
name.setText(bundle?.getString("key1","THIS IS A DEFAULT VALUE"))
if (bundle != null) {
email.setText(bundle.getString("email","Default"))
Branch.setText(bundle.getString("branch","Default"))
}
}
}
xml for datapoint
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".DataPoint">
<androidx.cardview.widget.CardView
android:id="@ id/card_view"
android:layout_width="350dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:elevation="5dp"
android:outlineAmbientShadowColor="@color/black"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.262"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436">
<TextView
android:id="@ id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:text="NAME"
android:textAlignment="center"
android:textSize="25sp"
android:translationY="50dp"
tools:visibility="visible"></TextView>
<TextView
android:id="@ id/emailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:textAlignment="center"
android:translationY="100dp" />
<TextView
android:id="@ id/branchname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:textAlignment="center"
android:translationY="150dp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
It appears you are creating the Bundle and Intent ahead of time. In onCreate the EditText values are still empty.
Instead, create the Intent & populate the Bundle just before calling the startActivity method.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_form)
named =findViewById(R.id.name)
emailadd =findViewById(R.id.editTextTextEmailAddress)
branch=findViewById(R.id.branch)
desc =findViewById(R.id.bio)
val bSumit : Button =findViewById(R.id.submit)
bSumit.setOnClickListener {
val isAllinfoCorrect = Checkinfo();
if(isAllinfoCorrect)
{
val email= emailadd.text
val branchName = branch.text
val biot= desc.text.toString()
val fullName = named.text.toString()
val bundle = Bundle()
bundle.putString("key1", named.text.toString())
bundle.putString("email", email.toString())
bundle.putString("branch",branchName.toString())
val intent = Intent(this,DataPoint::class.java)
intent.putExtras(bundle)
startActivity(intent)
Toast.makeText(this,fullName,Toast.LENGTH_LONG).show()
}
else
{
Toast.makeText(applicationContext,"Please Enter the required fields",Toast.LENGTH_LONG).show()
}
}
}
CodePudding user response:
Move this part of code...
val bundle = Bundle()
bundle.putString("key1", named.text.toString())
bundle.putString("email", email.toString())
bundle.putString("branch",branchName.toString())
val intent = Intent(this,DataPoint::class.java)
intent.putExtras(bundle)
Inside the listener part like this:
if(isAllinfoCorrect)
{
val bundle = Bundle()
bundle.putString("key1", named.text.toString())
bundle.putString("email", email.toString())
bundle.putString("branch",branchName.toString())
val intent = Intent(this,DataPoint::class.java)
intent.putExtras(bundle)
startActivity(intent)
Toast.makeText(this,fullName,Toast.LENGTH_LONG).show()
}

