Search code examples
javaandroidandroid-edittextgettextandroid-toast

unable to get value from edittext into string


I'm trying to make an android app which will show a Toast on button click. That Toast contains the number entered by the user in edittext field. The problem is that i am entering text to edittext(Numeric) field and on button click, Toast isn't showing the text entered by me. Toast is completely blank. Here is my code:-

Activity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class login extends AppCompatActivity {

    String username, password;
    Button payNGO, payGO;
    EditText usernameField, passwordField;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        setContentView(R.layout.activity_login);

        payNGO = (Button) findViewById(R.id.payngo);
        payGO = (Button) findViewById(R.id.paygo);

        usernameField = (EditText) findViewById(R.id.forno);
        passwordField = (EditText) findViewById(R.id.dob);

        username = usernameField.getText().toString();
        password = passwordField.getText().toString();


        payNGO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
            }
        });



    }
}

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".login"
    android:background="@drawable/back">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/forno"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.438"
        app:srcCompat="@drawable/ico" />

    <EditText
        android:id="@+id/forno"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="30dip"
        android:layout_marginStart="30dip"
        android:background="@drawable/rect_back"
        android:ems="10"
        android:hint="Number"
        android:inputType="number"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        app:layout_constraintBottom_toTopOf="@+id/dob"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.562"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/dob"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:layout_marginBottom="124dp"
        android:layout_marginEnd="30dip"
        android:layout_marginStart="30dip"
        android:background="@drawable/rect_back"
        android:ems="10"
        android:hint="Date of Birth"
        android:inputType="numberPassword"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.562"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="92dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/payngo"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginEnd="20dip"
            android:layout_marginStart="15dip"
            android:layout_weight="1"
            android:background="@drawable/rect_back_button"
            android:text="Pay (NGO)"
            tools:layout_editor_absoluteX="204dp"
            tools:layout_editor_absoluteY="440dp" />

        <Button
            android:id="@+id/paygo"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginEnd="15dip"
            android:layout_marginStart="20dip"
            android:layout_weight="1"
            android:background="@drawable/rect_back_button"
            android:text="Pay (GO)"
            tools:layout_editor_absoluteX="92dp"
            tools:layout_editor_absoluteY="440dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:background="@drawable/rect_back_text"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="50dip"
            android:layout_weight="1"
            android:text="@string/linef"
            android:textAlignment="textEnd"
            android:textColor="#fff"
            android:textSize="20sp"
            tools:layout_editor_absoluteX="30dp"
            tools:layout_editor_absoluteY="505dp" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitCenter"

            app:srcCompat="@drawable/heart"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="50dip"
            android:layout_weight="1"
            android:text="@string/linee"
            android:textColor="#fff"
            android:textSize="20sp"
            tools:layout_editor_absoluteX="171dp"
            tools:layout_editor_absoluteY="505dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Solution

  • Since you are getting the value in onCreate which will be executed even before you enter anything, your Toast shows blank data.

    Move the getText() methods to onClick to achieve expected result as follows:

        payNGO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = usernameField.getText().toString();
                password = passwordField.getText().toString();
                Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
            }
    
        });