xXJohnRamboXx
xXJohnRamboXx

Reputation: 739

android repeat dialog after clicked on it

I have an app let me show at start a dialog where input username and password for logging in. If your username and password are correct my program change activity, if incorrect it should repeat me dialog for input again username and password. My problem is how to repeat dialog when input are incorrect

Here the code:

public Dialog onCreateDialog(Bundle savedInstanceState) {

    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.my_dialog_layout,null);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            username = (EditText) v.findViewById(R.id.username);
            password = (EditText) v.findViewById(R.id.password);
            String user = "";
            String pswd = "";

            try {
                File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/"+"user.txt"); 

                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();
                user = bufferedReader.readLine().toString();
                pswd = bufferedReader.readLine();

            }catch (FileNotFoundException e) {
                Log.e("login activity", "File not found: " + e.toString());
            }catch (IOException e) {
                Log.e("login activity", "Can not read file: " + e.toString());
            }

            if (username.getText().toString().equals(user) && password.getText().toString().equals(pswd)) {

                Intent i = new Intent("user_activity");
                startActivity(i);
                Toast.makeText(getActivity(), "Welcome: " + username.getText().toString(), Toast.LENGTH_LONG).show();

            } else {
                //here it should me repeat this dialog
                Toast.makeText(getActivity(), "Username invalid", Toast.LENGTH_LONG).show();
            }
        }

        }).setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ((MainActivity)getActivity()).finish();
        }
    });

    return builder.create();
}

Thanks everybody

Upvotes: 1

Views: 779

Answers (1)

T D Nguyen
T D Nguyen

Reputation: 7603

Try this simple approach: keep dialog showing and clear field username and password:

if (username.getText().toString().equals(user) && password.getText().toString().equals(pswd)) {

            Intent i = new Intent("user_activity");
            startActivity(i);
            Toast.makeText(getActivity(), "Welcome: " + username.getText().toString(),    Toast.LENGTH_LONG).show();
            dialog.dismiss();
        } else {
            //here it should me repeat this dialog

            username.setText("", TextView.BufferType.EDITABLE);;
            password.setText("", TextView.BufferType.EDITABLE);
            Toast.makeText(getActivity(), "Username invalid", Toast.LENGTH_LONG).show();
        }

Upvotes: 1

Related Questions