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)));


protected void onCreate(Bundle savedInstanceState) {



mFlower = findViewById(;

mDescription = findViewById(;

submit = (Button) findViewById(;

etcode = findViewById(;

Bundle mBundle = getIntent().getExtras();

submit.setOnClickListener(new View.OnClickListener() {


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 - Friday, 9 November

Related Posts