From 6bd53aed79920bb5aa2fddd90fb4619f3ee60d21 Mon Sep 17 00:00:00 2001 From: sarac cristian Date: Thu, 6 Mar 2014 20:02:11 +0100 Subject: [PATCH] Fixed bug 44 --- src/org/sagemath/droid/NewCellDialog.java | 111 ++++++++++++++-------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/src/org/sagemath/droid/NewCellDialog.java b/src/org/sagemath/droid/NewCellDialog.java index b6970b1..034a76a 100644 --- a/src/org/sagemath/droid/NewCellDialog.java +++ b/src/org/sagemath/droid/NewCellDialog.java @@ -5,13 +5,16 @@ import java.util.Date; import java.util.Locale; +import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.View; +import android.widget.Button; import android.widget.EditText; import android.widget.Toast; @@ -26,10 +29,13 @@ public class NewCellDialog extends DialogFragment { private EditText group; private EditText input; + + @SuppressLint("NewApi") @Override public Dialog onCreateDialog(Bundle savedInstanceState) { View dialogView = getActivity().getLayoutInflater() .inflate(R.layout.dialog_new, null); + final Context mContext = dialogView.getContext(); title = (EditText)dialogView.findViewById(R.id.insert_cell_title); group = (EditText)dialogView.findViewById(R.id.insert_cell_group); @@ -40,48 +46,69 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { g = ""; group.setText(g); - return new AlertDialog.Builder(getActivity()) - .setView(dialogView) - .setTitle(R.string.add_new_title) - .setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - CellData newCell = new CellData(); - - if (title.getText().toString().equals("")) { - Date date = new Date(); - DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm aaa",Locale.US); - newCell.title = dateFormat.format(date); - } else { - newCell.title = title.getText().toString(); - } - if (group.getText().toString().equals("")) { - newCell.group = "My Worksheets"; - } else { - newCell.group = group.getText().toString(); - } - if (input.getText().toString().equals("")) { - Toast.makeText(getActivity(), "Enter an input to calculate!", Toast.LENGTH_SHORT).show(); - return; - } else { - newCell.input = input.getText().toString(); - } - - newCell.rank = 0; - CellCollection.getInstance().addCell(newCell); - CellCollection.getInstance().setCurrentCell(newCell); + final AlertDialog d = new AlertDialog.Builder(getActivity()) + .setView(dialogView) + .setTitle(R.string.add_new_title) + .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick + .setNegativeButton(android.R.string.cancel, null) + .create(); + + d.setOnShowListener(new DialogInterface.OnShowListener() { + + @Override + public void onShow(DialogInterface dialog) { + + final Button b = d.getButton(AlertDialog.BUTTON_POSITIVE); + b.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View view) { + + CellData newCell = new CellData(); + + if (title.getText().toString().equals("")) { + Date date = new Date(); + DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm aaa",Locale.US); + newCell.title = dateFormat.format(date); + } else { + newCell.title = title.getText().toString(); + } + if (group.getText().toString().equals("")) { + newCell.group = "My Worksheets"; + } else { + newCell.group = group.getText().toString(); + } + + if (input.getText().toString().equals("")) { - - Intent i = new Intent(getActivity().getApplicationContext(), - SageActivity.class); - i.putExtra("NEWCELL", true); - i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivity(i); - - } - }) - .setNegativeButton(android.R.string.cancel, null) - .create(); + Toast.makeText(mContext, "Enter an input to calculate!", Toast.LENGTH_SHORT).show(); + return; + } else { + newCell.input = input.getText().toString(); + + } + + newCell.rank = 0; + CellCollection.getInstance().addCell(newCell); + CellCollection.getInstance().setCurrentCell(newCell); + + + Intent i = new Intent(getActivity().getApplicationContext(), + SageActivity.class); + i.putExtra("NEWCELL", true); + i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(i); + + + + //Dismiss once everything is OK. + d.dismiss(); + } + }); + } +}); + + +return d ; } }