PositiveButton and other alertDialog buttons on android are disabled, how to solve?

1

I have in my little project a alertDialog , which is called to give some options to the user. Everything was working, until the buttons ( positiveButton , negativeButton and neutralButton ) disabled in all my AlertDialog of the project. The alertDialog is still running, but the buttons are in a weaker color (typical of when it is disabled) and do not call the methods that are within their onClick(); .

Follow my code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

switch (id) {

    case R.id.generate_pdf_2:
    //Aqui está meu código original, que estava funcionando alguns dias atras.

        /*new AlertDialog.Builder(getActivity()).setTitle("Criando Recibo")
                .setMessage("Tem certeza que deseja criar o recibo?")
                .setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((AlertDialog)dialog).getButton(which).setVisibility(View.VISIBLE);
                        GeradorRecibo geradorRecibo = new GeradorRecibo(getActivity(), cliente);
                        Toast.makeText(getActivity(), "Recibo gerado com sucesso!", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("Não", null)
                .show();*/

 //Aqui está uma tentativa que foi o mais parecido com a possivel solução do erro que encontrei, ou seja, segue esta logica porem, obviamente nao é esta a solução pois continua sem funcionar exatamente do mesmo jeito.


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Criando Recibo");
        builder.setMessage("Deseja criar o recibo?");
        builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                GeradorRecibo geradorRecibo = new GeradorRecibo(getActivity(), cliente);
                Toast.makeText(getActivity(), "Recibo gerado com sucesso!", Toast.LENGTH_LONG).show();
            }

        });

        AlertDialog dialog = builder.create();
        final Button btn = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
        btn.setEnabled(true);
        dialog.show();
        return true;
    
asked by anonymous 14.01.2016 / 12:32

1 answer

1

I tried the commented code, it is correct, but the code not commented was breaking, so it worked well too:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Criando Recibo");
    builder.setMessage("Deseja criar o recibo?");
    builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            GeradorRecibo geradorRecibo = new GeradorRecibo(getActivity(), cliente);
            Toast.makeText(getActivity(), "Recibo gerado com sucesso!", Toast.LENGTH_LONG).show();
        }

    });

    AlertDialog dialog = builder.create();
    dialog.show();
    final Button btn = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
    btn.setEnabled(true);
    return true;

Add more information, such as the Android version and more code details, this will help with the answers.

    
15.01.2016 / 14:25