Home > Back-end >  Value in editText passed in Toast message is not showing
Value in editText passed in Toast message is not showing

Time:12-24

I want to pass the value of edit text in the toast but it is not showing the same.What is the mistake in my code ?

This is showing:"Username: is wrong" instead of "Username : nandini is wrong".This is the image. https://drive.google.com/file/d/1EcWfQa3xV3j607VO0yrp_jaUBaVNQeuF/view?usp=sharing

// this is my xml code :

<?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"
    android:background="@drawable/uu"
    tools:context=".MainActivity">


    <LinearLayout
        android:id="@ id/linearLayout"
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="200dp"
        android:layout_marginEnd="16dp"
        android:alpha="0.2"
        android:background="#BDBDBD"
        android:elevation="89dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.482"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </LinearLayout>

    ***<EditText
        android:id="@ id/editLogin"
        android:layout_width="327dp"
        android:layout_height="66dp"
        android:layout_margin="15dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:alpha="0.3"
        android:background="#000000"
        android:drawableLeft="@drawable/account1"
        android:drawablePadding="10dp"
        android:drawableTint="@color/white"
        android:ems="10"
        android:hint="Username"
        android:inputType="textPersonName"
        android:padding="20dp"
        android:textColor="#FFFFFF"
        android:textColorHint="@color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@ id/linearLayout"
        app:layout_constraintStart_toStartOf="@ id/linearLayout"
        app:layout_constraintTop_toTopOf="@ id/linearLayout" />***

    <EditText
        android:id="@ id/editPassword"
        android:layout_width="327dp"
        android:layout_height="66dp"
        android:layout_marginTop="10dp"
        android:alpha="0.3"
        android:background="#000000"
        android:drawableLeft="@drawable/unlockk1"
        android:drawablePadding="10dp"
        android:drawableTint="@color/white"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        android:padding="20dp"
        android:textColor="#FFFFFF"
        android:textColorHint="@color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@ id/linearLayout"
        app:layout_constraintStart_toStartOf="@ id/linearLayout"
        app:layout_constraintTop_toBottomOf="@ id/editLogin" />

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="128dp"
        android:layout_marginEnd="148dp"
        android:fontFamily="sans-serif-black"
        android:text="My Account"
        android:textColor="#FFFFFF"
        android:textSize="34sp"
        app:flow_horizontalAlign="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/passwordConstraint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="13dp"
        android:layout_marginTop="8dp"
        android:text="Password must be atleast of 4 digits."
        android:textColor="#400909"
        android:textSize="16sp"
        app:layout_constraintStart_toStartOf="@ id/linearLayout"
        app:layout_constraintTop_toBottomOf="@ id/editPassword" />

    <TextView
        android:id="@ id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="32dp"
        android:text="@string/forgot_password"
        android:textColor="#FFFCFC"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@ id/buttonSignup"
        app:layout_constraintEnd_toEndOf="@ id/linearLayout"
        app:layout_constraintTop_toBottomOf="@ id/passwordConstraint" />


    ***<Button
        android:id="@ id/buttonSignup"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:layout_marginBottom="16dp"
        android:alpha="0.2"
        android:backgroundTint="#000000"
        android:padding="20dp"
        android:text="Login"
        app:layout_constraintBottom_toBottomOf="@ id/linearLayout"
        app:layout_constraintEnd_toEndOf="@ id/linearLayout"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@ id/linearLayout" />***


</androidx.constraintlayout.widget.ConstraintLayout>

// this is my MainActivity.kt code :

package com.nandini.android.loginpage

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

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

        val editLogin=findViewById<EditText>(R.id.editLogin)
        val name=editLogin.editableText.toString()
        val btn: Button = findViewById(R.id.buttonSignup)
        btn.setOnClickListener {
            Toast.makeText(
                this,
                "Username: $name is wrong",
                Toast.LENGTH_SHORT
            ).show()
        }
}}

CodePudding user response:

You're reading the value of the edit text too early.

Move the val name=editLogin.editableText.toString() inside the onclick listener so you're reading the value on click, not when the activity is set up.

CodePudding user response:

The name variable is not being set in the onClickListener, instead it gets set once and the new value is not being used when the button gets clicked.

val editLogin=findViewById<EditText>(R.id.editLogin)
        
val btn: Button = findViewById(R.id.buttonSignup)
btn.setOnClickListener {
    val name=editLogin.editableText.toString()
    Toast.makeText(
        this,
        "Username: $name is wrong",
        Toast.LENGTH_SHORT
    ).show()
}
  • Related