Password Generator Error (Android)

760 Views Asked by At

I dont know why I cant generate a custom password.

public void GeneratePass(View view) {

    EditText TextField = (EditText)findViewById(R.id.DigitsField);
    int DigitsNum = Integer.parseInt(TextField.getText().toString());
    if (DigitsNum != 1){
        Random Pass = new Random();
        int num1 = Pass.nextInt(10);
        TextView PassText = (TextView) findViewById(R.id.PassText);
        PassText.setText(num1);
    }
}

I created this Text Field(EditText) In which you can write how many digits you want the password to be, I tried putting

    EditText TextField = (EditText)findViewById(R.id.DigitsField);
    String Digits = TextField.getText().toString();
    int DigitsNum = Integer.parseInt(Num);

and changing the if(DigitsNum == 1)... But when I write the digits(I just code for 1) I tried writing 1 but it just crash or stop working.

1

There are 1 best solutions below

9
On

Try something like this and modify it to your needs:

Java Code (Add this in your onCreate method):

final TextView passText = (TextView) findViewById(R.id.passText);
final EditText digitsField = (EditText) findViewById(R.id.digitsField);
Button generatePassword = (Button) findViewById(R.id.generatePassword);

generatePassword.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        int passwordLength = Integer.parseInt(digitsField.getText().toString());
        String allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        char[] allowedCharsArray = allowedChars.toCharArray();
        char[] chars = new char[passwordLength];
        Random random = new Random();

        for (int i = 0; i < passwordLength; i++) {
            chars[i] = allowedCharsArray[random.nextInt(allowedChars.length())];
        }

        passText.setText(chars, 0, passwordLength);
    }
});

XML Layout (Edit it with your current layout):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Password"
        android:layout_gravity="center"
        android:id="@+id/passText" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:id="@+id/digitsField" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Generate Password"
        android:layout_gravity="center"
        android:id="@+id/generatePassword" />

</LinearLayout>