app/src/main/java/com/adroitdevs/belanja/TaskDialogFragment.java
@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
        final int taskPosition = getArguments().getInt("taskPosition");

        View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_new_task, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setTitle(title);
        final EditText description = (EditText) v.findViewById(R.id.new_task_desc);

        if (taskPosition >= 0) {
            description.setHint(((Task) ((MainActivity) getActivity()).mTaskAdapter.getItem(taskPosition)).getDescription());

            // Check description is present, if so add a task otherwise show an error.
            DialogInterface.OnClickListener positiveClick = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    if (description.getText().length() > 0) {
                        ((MainActivity) getActivity()).updateTaskAt(taskPosition, description.getText().toString());
                        description.getText().clear();
                    } else {
                        Toast.makeText(getActivity(),
                                R.string.task_not_updated,
                                Toast.LENGTH_LONG).show();
                    }
                }
            };

            builder.setView(v).setPositiveButton(R.string.update, positiveClick);
        } else {
            DialogInterface.OnClickListener positiveClick = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    if (description.getText().length() > 0) {
                        ((MainActivity) getActivity()).createNewTask(description.getText().toString());
                        description.getText().clear();
                    } else {
                        Toast.makeText(getActivity(),
                                R.string.task_not_created,
                                Toast.LENGTH_LONG).show();
                    }
                }
            };

            builder.setView(v).setPositiveButton(R.string.create, positiveClick);
        }

        // Create onClick listener for cancel button.
        DialogInterface.OnClickListener negativeClick = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        };
        builder.setNegativeButton(R.string.cancel, negativeClick);

        final AlertDialog taskDialog = builder.create();

        // Enable "Create"/"Update" button when the description has some characters.
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                Button b = taskDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                b.setEnabled(description.getText().length() > 0);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        // Set listener for create button configured above.
        taskDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button b = taskDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                b.setEnabled(description.getText().length() > 0);
                description.addTextChangedListener(textWatcher);
            }
        });

        return taskDialog;
    }
// Create Task dialog with appropriate properties and interaction; Update vs Create.
Create Task dialog with appropriate properties and interaction; Update vs Create.
Create Task dialog with appropriate properties and interaction ; Update vs Create .
@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
        final int taskPosition = getArguments().getInt("taskPosition");
        View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_new_task, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setTitle(title);
        final EditText description = (EditText) v.findViewById(R.id.new_task_desc);
        if (taskPosition >= 0) {
            description.setHint(((Task) ((MainActivity) getActivity()).mTaskAdapter.getItem(taskPosition)).getDescription());
            DialogInterface.OnClickListener positiveClick = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    if (description.getText().length() > 0) {
                        ((MainActivity) getActivity()).updateTaskAt(taskPosition, description.getText().toString());
                        description.getText().clear();
                    } else {
                        Toast.makeText(getActivity(),
                                R.string.task_not_updated,
                                Toast.LENGTH_LONG).show();
                    }
                }
            };
            builder.setView(v).setPositiveButton(R.string.update, positiveClick);
        } else {
            DialogInterface.OnClickListener positiveClick = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    if (description.getText().length() > 0) {
                        ((MainActivity) getActivity()).createNewTask(description.getText().toString());
                        description.getText().clear();
                    } else {
                        Toast.makeText(getActivity(),
                                R.string.task_not_created,
                                Toast.LENGTH_LONG).show();
                    }
                }
            };
            builder.setView(v).setPositiveButton(R.string.create, positiveClick);
        }
        DialogInterface.OnClickListener negativeClick = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        };
        builder.setNegativeButton(R.string.cancel, negativeClick);
        final AlertDialog taskDialog = builder.create();
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                Button b = taskDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                b.setEnabled(description.getText().length() > 0);
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        };
        taskDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button b = taskDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                b.setEnabled(description.getText().length() > 0);
                description.addTextChangedListener(textWatcher);
            }
        });
        return taskDialog;
    }
@ Override public Dialog onCreateDialog ( Bundle savedInstanceState ) { int title = getArguments ( ) . getInt ( "title" ) ; final int taskPosition = getArguments ( ) . getInt ( "taskPosition" ) ; View v = getActivity ( ) . getLayoutInflater ( ) . inflate ( R . layout . dialog_new_task , null ) ; AlertDialog . Builder builder = new AlertDialog . Builder ( getActivity ( ) ) . setTitle ( title ) ; final EditText description = ( EditText ) v . findViewById ( R . id . new_task_desc ) ; if ( taskPosition >= 0 ) { description . setHint ( ( ( Task ) ( ( MainActivity ) getActivity ( ) ) . mTaskAdapter . getItem ( taskPosition ) ) . getDescription ( ) ) ; DialogInterface . OnClickListener positiveClick = new DialogInterface . OnClickListener ( ) { @ Override public void onClick ( DialogInterface dialog , int id ) { if ( description . getText ( ) . length ( ) > 0 ) { ( ( MainActivity ) getActivity ( ) ) . updateTaskAt ( taskPosition , description . getText ( ) . toString ( ) ) ; description . getText ( ) . clear ( ) ; } else { Toast . makeText ( getActivity ( ) , R . string . task_not_updated , Toast . LENGTH_LONG ) . show ( ) ; } } } ; builder . setView ( v ) . setPositiveButton ( R . string . update , positiveClick ) ; } else { DialogInterface . OnClickListener positiveClick = new DialogInterface . OnClickListener ( ) { @ Override public void onClick ( DialogInterface dialog , int id ) { if ( description . getText ( ) . length ( ) > 0 ) { ( ( MainActivity ) getActivity ( ) ) . createNewTask ( description . getText ( ) . toString ( ) ) ; description . getText ( ) . clear ( ) ; } else { Toast . makeText ( getActivity ( ) , R . string . task_not_created , Toast . LENGTH_LONG ) . show ( ) ; } } } ; builder . setView ( v ) . setPositiveButton ( R . string . create , positiveClick ) ; } DialogInterface . OnClickListener negativeClick = new DialogInterface . OnClickListener ( ) { public void onClick ( DialogInterface dialog , int id ) { dialog . dismiss ( ) ; } } ; builder . setNegativeButton ( R . string . cancel , negativeClick ) ; final AlertDialog taskDialog = builder . create ( ) ; final TextWatcher textWatcher = new TextWatcher ( ) { @ Override public void onTextChanged ( CharSequence s , int start , int before , int count ) { Button b = taskDialog . getButton ( DialogInterface . BUTTON_POSITIVE ) ; b . setEnabled ( description . getText ( ) . length ( ) > 0 ) ; } @ Override public void beforeTextChanged ( CharSequence s , int start , int count , int after ) { } @ Override public void afterTextChanged ( Editable s ) { } } ; taskDialog . setOnShowListener ( new DialogInterface . OnShowListener ( ) { @ Override public void onShow ( DialogInterface dialog ) { Button b = taskDialog . getButton ( DialogInterface . BUTTON_POSITIVE ) ; b . setEnabled ( description . getText ( ) . length ( ) > 0 ) ; description . addTextChangedListener ( textWatcher ) ; } } ) ; return taskDialog ; }
Create Task dialog with appropriate properties and interaction; Update vs Create.
Create Task dialog with appropriate properties and interaction ; Update vs Create .
// Check description is present, if so add a task otherwise show an error. // Create onClick listener for cancel button. // Enable "Create"/"Update" button when the description has some characters. // Set listener for create button configured above.