Home > front end >  button doesn't work on a condition android
button doesn't work on a condition android

Time:05-20

i am working on an app which have a login page ,i am trying to login to another activity but the button doesn't work on condition which has admin.i tried multiple solution but the problem is the same ,the condition doesn't work at all.

Login.java:

private ImageView back;
private String Name,Pass;
private EditText name,password;
private static final String url="http://192.168.0.147:8080/login_android.php";
private static final String url2="http://192.168.0.147:8080/login_android2.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    back=(ImageView)findViewById(R.id.imageView4);
    Name = Pass="";
    name=(EditText)findViewById(R.id.editText3);
    password=(EditText)findViewById(R.id.editText4);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent autreActivite= new Intent(getApplicationContext(),acceuil.class);
            startActivity(autreActivite);
            finish();
        }
    });




}

public void button(View view){
    Intent autreActivite= new Intent(getApplicationContext(),patient.class);
    startActivity(autreActivite);
    finish();
}

public void login(View view) {
    Name = name.getText().toString().trim();
    Pass = password.getText().toString().trim();

    if (!name.equals("") && !Pass.equals("")) {

        /*StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.contains("0")) {

                } else {
                    Toast.makeText(getApplicationContext(), "nom ou mot de passe non valide", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void one rrorResponse(VolleyError error) {
                Toast.makeText(Login.this, error.toString().trim(), Toast.LENGTH_SHORT).show();
            }
        }) {*//*
            @Override
            /*C'est une hashmap clé valeur*//*
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> data = new HashMap<>();
                data.put("name", Name);
                data.put("password", Pass);
                return data;
            }
        };

        StringRequest stringRequest2 = new StringRequest(Request.Method.POST, url2, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.contains("0")) {
                    Intent autreActivite = new Intent(getApplicationContext(), patient.class);
                    startActivity(autreActivite);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "nom ou mot de passe non valide", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void one rrorResponse(VolleyError error) {
                Toast.makeText(Login.this, error.toString().trim(), Toast.LENGTH_SHORT).show();
            }
        }) {*//*
            @Override
            /*C'est une hashmap clé valeur*//*
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> data = new HashMap<>();
                data.put("name", Name);
                data.put("password", Pass);
                return data;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
        requestQueue.add(stringRequest2);*/
    } else if(name.equals("admin") && password.equals("admin")){
        /*Intent autreActivite = new Intent(getApplicationContext(), medecin.class);
        startActivity(autreActivite);
        finish();*/
        Toast.makeText(this, "cc", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "Champs vides", Toast.LENGTH_SHORT).show();
    }


}




}

activity_login.xml:

                                                                                                                                   <?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=".Login">

<EditText
    android:id="@ id/editText3"
    android:layout_width="252dp"
    android:layout_height="41dp"
    android:drawableStart="@drawable/ic_user"
    android:ems="10"
    android:fontFamily="@font/pangolinregular"
    android:hint=" Nom"
    android:inputType="textPersonName"
    android:textColorHint="@color/textColor"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.506"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/imageView3"
    app:layout_constraintVertical_bias="0.17" />

<EditText
    android:id="@ id/editText4"
    android:layout_width="260dp"
    android:layout_height="39dp"
    android:drawableStart="@drawable/ic_unlock"
    android:ems="10"
    android:fontFamily="@font/pangolinregular"
    android:hint=" Mot de passe"
    android:inputType="textPersonName"
    android:textColorHint="@color/textColor"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.492"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/editText3" />

<ImageView
    android:id="@ id/imageView3"
    android:layout_width="299dp"
    android:layout_height="452dp"
    android:layout_marginTop="28dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.513"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/photo1" />

<ImageView
    android:id="@ id/imageView4"
    android:layout_width="22dp"
    android:layout_height="19dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.041"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.039"
    app:srcCompat="@drawable/leftarrow" />

<ImageView
    android:id="@ id/imageView5"
    android:layout_width="23dp"
    android:layout_height="29dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.958"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.038"
    app:srcCompat="@drawable/setting" />

<Button
    android:id="@ id/button"
    android:layout_width="304dp"
    android:layout_height="42dp"
    android:layout_marginTop="48dp"
    android:background="@drawable/round_btn"

    android:text="Valider"
    android:textColor="@color/ButtonTextColor"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@ id/editText4"
    app:layout_constraintHorizontal_bias="0.592"
    app:layout_constraintStart_toEndOf="@ id/editText4"
    app:layout_constraintStart_toStartOf="@ id/editText4"
    app:layout_constraintTop_toBottomOf="@ id/editText4"
    app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

else if(name.equals("admin") && password.equals("admin")

I think here is the problem. You are not using the variables you assigned for the inputs.

Name = name.getText().toString().trim();
Pass = password.getText().toString().trim();

if (!Name.equals("") && !Pass.equals("")) // changed to Name variable 

...

else if(Name.equals("admin") && Pass.equals("admin") // changed to variables

maybe change the name of EditText and String to be more distinct to avoid misuses.

CodePudding user response:

i think it maybe because your first expresion in "if". that will make what ever you input password or name the result statment is true and doesnt enter to "else" statement and the other is variabel name case sensitif in if expresion name lowercase but before it declare with uppercase in first word.

Name = name.getText().toString().trim();
Pass = password.getText().toString().trim();

if (!name.equals("") && !Pass.equals("")) // changed to if (Name.equals("") && Pass.equals("")) 
  • Related