Search code examples
androidstringandroid-edittextgettext

Why Edit Text returning empty string on a button click in android?


Making a simple login screen where i am using two edit texts for username and password. i set a listener on a button click and used getText() function to store the values in variables but i am getting empty strings. I tried to toast the variables but i am getting an empty toast while debugging.

 EditText ed1,ed2;
    Button b1;
    DatabaseReference dr;
    Query q;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ed1=findViewById(R.id.editText10);
        ed2=findViewById(R.id.editText11);
        b1=findViewById(R.id.button);
        final String role=getIntent().getStringExtra("role");
        b1.setOnClickListener(new View.OnClickListener() {
            String username=ed1.getText().toString();
            String input_password=ed2.getText().toString();
            @Override
            public void onClick(View view) {

                if(role.equals("teacher"))
                {
                    dr= FirebaseDatabase.getInstance().getReference();
                    Toast.makeText(LoginActivity.this, username+" "+input_password, Toast.LENGTH_SHORT).show();
                    Query q=dr.child("Teachers").orderByChild("username").equalTo(username);
                    q.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if(!dataSnapshot.exists())
                            {
                                Toast.makeText(LoginActivity.this, "Invalid Combination no data", Toast.LENGTH_SHORT).show();
                            }
                            for (DataSnapshot ds: dataSnapshot.getChildren())
                            {
                                Map mp=(Map) ds.getValue();
                                String password=mp.get("password").toString();
                                if(input_password.equals(password))
                                {
                                    MyData.teacher=username;
                                    startActivity(new Intent(LoginActivity.this,SelectClass.class));
                                }
                                else
                                {
                                    Toast.makeText(LoginActivity.this, "Invalid Combination", Toast.LENGTH_SHORT).show();
                                }

                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });

                }
                else
                {
                    startActivity(new Intent(LoginActivity.this,AdminActivity.class));
                }

            }
        });
    }

Here is my Xml file


    <LinearLayout
        android:layout_width="203dp"
        android:layout_height="227dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.427"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.324">

        <EditText
            android:id="@+id/editText10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Username"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/editText11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Password"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login" />
    </LinearLayout>

Solution

  • You have put code in wrong place. You need to move it inside onClick

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        String username=ed1.getText().toString();
        String input_password=ed2.getText().toString();
        //rest of your code as it is