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

Asked

Viewed 732 times

1

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

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

1 answer

1

I tested the commented code, that’s correct, but the unclaimed code was breaking, so it also worked well:

 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 in the answers.

  • But this code is identical to the OP. This does not seem to be an attempt to solve the problem.

  • It is not identical, dialog.getButton() should be after the dialog call.show(), tested in api 23.

  • 1

    So guys, I appreciate the comments. I ended up discovering what it was. They were just problems with the permissions, which now changed on android. Above api 23 the app should present to the user a request for permissions while running the application, as the functions of my alertDialog require access to internal storage, have been disabled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.