Java – Check passwords in Android

Check passwords in Android… here is a solution to the problem.

Check passwords in Android

I

want to check the password, I have a password field in Android

    package com.example.berk4;

import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Intent;

public class MainActivity extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

EditText et = (EditText)findViewById(R.id.editText1);
    Button buttonEnter = (Button)findViewById(R.id.button1);
    buttonEnter.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    EditText et = (EditText)findViewById(R.id.editText1);
    String password = et.getText().toString();

et.getEditableText().toString();
    if (password.equals("admin")) {
        Intent intent2 = new Intent("com.example.berk4.screen2");
    startActivity(intent2);

}else{

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("you suck");
        builder.setMessage("try again");
        builder.setPositiveButton("ok", null);
        AlertDialog dialog = builder.show();
    }

}

}

When I enter

a random wrong password, it works fine, but when I enter the correct password, the app closes, why doesn’t it work for me? (By the way.) I have another class screen2. )

Solution

Start a new activity as if the password is correct:

 if (password.equals("admin")) {
        Intent intent2 = new Intent(MainActivity.this,screen2.class);
        startActivity(intent2);

}else{
     your code here
  }

And make sure you’ve added Next Activity to AndroidManifest.xml:

.....
<activity android:name=".screen2"></activity>
</application>
.....

Related Problems and Solutions