0

I've implemented this dialog into my android app and it works fine. How to make a "do not ask me again" dialog pop-up box? Android

That is my onResume() method:

@Override
protected void onResume() 
{
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    LayoutInflater adbInflater = LayoutInflater.from(this);
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Info");
    adb.setMessage(Html.fromHtml("Readme"));

    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {
            public void onClick(DialogInterface dialog, int which) 
            {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                editor.commit();
                return;
            }
        });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");

    if (!skipMessage.equals("checked"))
        adb.show();

    super.onResume();
}

But how can I integrate a 10 second countdown into the the OK button. During this countdown the AlertDialog should not be close by clicking the OK button (it should be disabled for the time of the countdown).

Community
  • 1
  • 1

1 Answers1

0

write your code like below

// Create a handler
Handler handler = new Handler();
AlertDialog.Builder adb = new AlertDialog.Builder(this);
    LayoutInflater adbInflater = LayoutInflater.from(this);
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Info");
    adb.setMessage(Html.fromHtml("Readme"));

    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {
            public void onClick(DialogInterface dialog, int which) 
            {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                editor.commit();
                return;
            }
        });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");

    if (!skipMessage.equals("checked"))
        AlertDialog dialog = adb.create();
        dialog.show();

    final Button button;
    button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    if(dialog.isShowing()){

        button.setClickable(false);
    }

    // Post the task to set it clickable in 10000ms
    handler.postDelayed(new Runnable(){
        @Override
        public void run() {
            //if(null !== button || button !== null)
                button.setClickable(true);
        }}, 10000);
Shadik Khan
  • 1,217
  • 1
  • 8
  • 18
  • getButton: The method getButton(int) is undefined for the type AlertDialog.Builder –  Mar 29 '15 at 20:21
  • @AndRoid I updated the code with some check. Please have a look. As per alert dialog documentation you must call show() on your dialog before you can access the buttons. – Shadik Khan Mar 29 '15 at 20:29
  • same problem getButton: The method getButton(int) is undefined for the type AlertDialog.Builder and The method isShowing() is undefined for the type AlertDialog.Builder –  Mar 30 '15 at 08:12
  • @AndRoid I updated the code and tested it as well. Now it's working fine as per your requirement. Lat time I forgot to add the line AlertDialog dialog = adb.create(); and then need to call dialog.show(). Please refer to updated code. Simply copy and replace. I have tested it on my side working like charm. – Shadik Khan Mar 30 '15 at 17:33
  • Thank you for helping me. There is only one last problem. I think the 10 second countdown works. I can't click the button for 10s. But I want to show the countdown inside the button to show the user, that he can't click it until the countdown finished –  Mar 31 '15 at 09:15
  • You can not show the text in alert button like 10,9,8,...1 . It will execute first time correctly like 10 but when it update to 9 it will give you exception. The best solution would be that you show the countdown in your alert/checkbox xml in any textview and hide the button till countdown and show the button after countdown done. – Shadik Khan Mar 31 '15 at 10:35