Question

I'm trying to display a toast when you click the button "boton_continuar" given the following conditions: EditExt vacuum (numero_celular) and ckeckbox (check) check no. This is my code

boton_continuar.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //Verificamos que se halla clikeado y se halla ingresado
        if(check.isChecked() && numero_celular.getText().length() != 0){

            Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
            startActivity(intent);
        }else{
            Context contexto = getApplicationContext();
            if(!check.isChecked()){                 
                Toast.makeText(contexto, "Debe aceptar los términos", 2000);
            }
            if(numero_celular.getText().length() == 0){
                Toast.makeText(contexto, "Debe ingresar su número", 2000);
            }
    }
}});
Was it helpful?

Solution 2

Two things:

  1. Call .show() on the Toast you made.
  2. 2000 is not a valid parameter for the Toast. That will not make it show up for 2000 ms. The only parameter that the Toast accepts for that is Toast.LENGTH_SHORT or Toast.LENGTH_LONG

So:

Change these lines:

Toast.makeText(contexto, "Debe aceptar los términos", 2000);
Toast.makeText(contexto, "Debe ingresar su número", 2000);

to these lines:

Toast.makeText(contexto, "Debe aceptar los términos", Toast.LENGTH_SHORT).show();
Toast.makeText(contexto, "Debe ingresar su número", Toast.LENGTH_SHORT).show();

Toast.LENGTH_SHORT is 2000 ms (2 seconds), and Toast.LENGTH_LONG is 3500 ms (3.5 seconds).

  • Toast.LENGTH_SHORT == 0 so you can pass is 0 instead.
  • Toast.LENGTH_LONG == 1 so you can pass it 1 instead.

Visit Android's documentation on Toast for detailed information.

OTHER TIPS

Make sure to call .show() after making your toast.

boton_continuar.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //Verificamos que se halla clikeado y se halla ingresado
        if(check.isChecked() && numero_celular.getText().length() != 0){
            Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
            startActivity(intent);
        } else {
            Context contexto = getApplicationContext();
            if(!check.isChecked()){                 
                Toast.makeText(contexto, "Debe aceptar los términos", 2000).show();
            }
            if(numero_celular.getText().length() == 0){
                Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
            }
        }
    }});

You have to use SHOW function.

Do:

Toast.makeText(contexto, "Debe ingresar su número", 2000).show();

You need to add ".show()" after your "Toast.makeText()"s.

For example:

if(!check.isChecked()){                 
            Toast.makeText(contexto, "Debe aceptar los términos", 2000).show();
            }
            if(numero_celular.getText().length() == 0){
                Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
            }

Try the following:

 boton_continuar.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //Verificamos que se halla clikeado y se halla ingresado
        if(check.isChecked() && numero_celular.getText().length() != 0){


            Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
            startActivity(intent);
        }else{
            Context contexto = getApplicationContext();
            if(!check.isChecked()){                 
                Toast.makeText(contexto, "Debe aceptar los términos", 2000).show();
            }
            if(numero_celular.getText().length() == 0){
                Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
            }


        }

    }});

You need to call .show() on the Toast for it to appear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top