How do i make a button take text from an edittext and compare it to a random number?

I'm trying to make my button compare the number in the edit text to the random number I'm generating and make a toast if they're the same. the code runs but no matter what i set the bound to the number in the edit text never equals the random number.

Here is my code

Button submit;

EditText etcode;

Random random = new Random();

String generatedPassword = String.format(String.valueOf(random.nextInt(1)));

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_detail);

mFlower = findViewById(R.id.ivImage);

mDescription = findViewById(R.id.tvDescription);

submit = (Button) findViewById(R.id.btn_submit);

etcode = findViewById(R.id.et_code);

Bundle mBundle = getIntent().getExtras();

submit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (etcode.getText().toString().equals(generatedPassword)){

Toast.makeText(getApplicationContext(), "CONGRATS", Toast.LENGTH_SHORT).show();

}

else {

Toast.makeText(getApplicationContext(), "TRY AGAIN",...

Read More »

By: StackOverFlow - 5 days ago

Related Posts