Search code examples
javaandroidgettext

the output of getText method cant be stored in the String variable, what to do?


i am having trouble with storing the data in variable from getText method it's returning the data but variable is not storing the data if i put the et1.getText.toString in the toast in stead of variable its working i dont know why this happening please help out

    package com.example.mad_lab_one_addition_app;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    //import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText et1,et2;
        Button btn;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            et1 = findViewById(R.id.edittextone);
            et2 = findViewById(R.id.edittexttwo);
            btn = findViewById(R.id.button1);
    
            add();
        }
    
        public void add(){
    
    
            String string = et1.getText().toString();
            // String str2 = et2.getText().toString();
    
    //        int a = Integer.parseInt(str);
    //        int b = Integer.parseInt(str2);
    //        int c = a+b;
    
    
    
            btn.setOnClickListener(v -> Toast.makeText(MainActivity.this,string,Toast.LENGTH_LONG).show());
    
        }
    }
    </code>
    </pre>


it is java file !
<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=".MainActivity">

    <EditText
        android:id="@+id/edittextone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="44dp"
        android:ems="10"
        android:hint="@string/enter_first_number"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/edittexttwo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:autofillHints=""
        android:importantForAutofill="no" />

    <EditText
        android:id="@+id/edittexttwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="218dp"
        android:ems="10"
        android:hint="@string/enter_second_number"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="61dp"
        android:text="@string/add"
        app:backgroundTint="@null"
        android:background="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edittexttwo" />
</androidx.constraintlayout.widget.ConstraintLayout>

its XMl ! i am trying to print a toast but its printing empty toast. can anyone help?


Solution

  • your EditText is empty at the first. you should enter the text in the EditText then click the button and store the data into the variable. please follow this approach:

    public class MainActivity extends AppCompatActivity {
    
        EditText et1,et2;
        Button btn;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            et1 = findViewById(R.id.edittextone);
            et2 = findViewById(R.id.edittexttwo);
            btn = findViewById(R.id.button1);
    
            btn.setOnClickListener(v ->
                    String temp = et1.getText().toString();
                    Toast.makeText(MainActivity.this,temp,Toast.LENGTH_LONG).show());
    
                    // Do whatever you want with the temp.
        }
    
    }