How to pass a variable from AlertDialog to an Activity?

how to pass yes/no value from alertdialog to an Activity in android

  • i've recently started developing in android and am currently stuck at a point i need to receive values from a dialog box. I have a mainActivity which extends fragmentActivity and an AlertDialog Class. 1)i created a static method showDefalutDialog in AlertDialog class and its being called from mainActivity button click listener with parameters being passed to alertDialog. 2)In showDefalutDialog static method i created .setPositivebutton and .setNegativeButton with a Yes/No DialogInterface respectively. now here's what i want to do. 1)When yes button on interface is clicked it should return a value to mainActivity so i can implement it in an if statement to perform a certain function. moving from windows c# programming doing so isn't a problem but i just don't know how to implement that in android below is relevant code snip private void sendSms() { SharedPreferences pref = getApplicationContext().getSharedPreferences("Sms_MyPref", 0); mail = pref.getString("email", null); // getting String tel = pref.getString("receiver_tel", null); // getting String layout = (LinearLayout)findViewById(R.id.linearLayout1); from_dateEdit = (EditText) findViewById(R.id.date_edit); to_dateEdit = (EditText) findViewById(R.id.date_edit_to); snButton = (Button)findViewById(R.id.form_send_button); from = (Button)findViewById(R.id.from); to = (Button)findViewById(R.id.to); spn = (Spinner)findViewById(R.id.form_spinner); spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Object item = parent.getItemAtPosition(pos); spinnerV = (String) item; if(pos == 0) { layout.setVisibility( pos == 0 ? View.VISIBLE : View.VISIBLE); from_dateEdit.setText(DatePickerFragment.getYesteesDate()); from.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDatePicker(); } }); to_dateEdit.setText(DatePickerFragment.getTodaysDate()); to.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDatePicker2(); } }); new1 = null; new2 = null; from_dateEdit.setText(new1); to_dateEdit.setText(new2); } else if(pos == 1) { layout.setVisibility( pos == 1 ? View.GONE : View.VISIBLE); new1 = null; new2 = null; new1 = "a"; new2 = "b"; } else if(pos == 2) { layout.setVisibility( pos == 2 ? View.GONE : View.VISIBLE); new1 = null; new2 = null; new1 = "a"; new2 = "b"; } else if(pos == 3) { layout.setVisibility( pos == 3 ? View.GONE : View.VISIBLE); new1 = null; new2 = null; new1 = "a"; new2 = "b"; } } public void onNothingSelected(AdapterView<?> parent) { } }); snButton.setOnClickListener(new OnClickListener() { public void onClick(View view) { if(new1 == null && new2 == null) { alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date range", false); } else if(new1 != null && new2 == null) { alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date TO", false); } else if(new1 == null && new2 != null) { alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date FROM", false); } else { gen = new1.toString()+","+new2.toString(); alert(); //i want to return a value from dialog yes/no click if(/*dialog yes is clicked*/) { sms(); } else if(/*dialog No is clicked*/) { return; } } } }); } private void alert() { AlertDialogManager.showDefalutDialog(getApplicationContext(), spinnerV, mail, new1,new2); } public void sms() { String both = "{"+ spinnerV.toString() + ","+gen.toString()+","+ mail.toString()+"}"; sendSMS(tel,both); } and showDefaultDialog static method from AlertDialog class @SuppressLint("InflateParams") public static void showDefalutDialog(final Context context, String order, final String mail, String fromD, String toD) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); // set title alertDialogBuilder.setTitle(R.string.finalmsg); LayoutInflater li = LayoutInflater.from(context); View view = li.inflate(R.layout.data_summary_view, null); EditText EMAIL = (EditText)view.findViewById(R.id.Email); EditText Selectedorder = (EditText)view.findViewById(R.id.order); EditText Dfrom = (EditText)view.findViewById(R.id.edit_from); EditText Dto= (EditText)view.findViewById(R.id.edit_to); LinearLayout ll = (LinearLayout) view.findViewById(R.id.datelayout); LinearLayout l2 = (LinearLayout) view.findViewById(R.id.datelayout2); Selectedorder.setText(order); EMAIL.setText(mail); if(fromD.toString() != "a" && toD.toString() != "b") { ll.setVisibility(View.VISIBLE); l2.setVisibility(View.VISIBLE); Dfrom.setText(fromD); Dto.setText(toD); } else if(fromD.toString() == "a" && toD.toString() == "b") { ll.setVisibility(View.GONE); l2.setVisibility(View.GONE); } // set dialog message alertDialogBuilder.setView(view); //int msdt = data.toString().toCharArray().length; //Toast.makeText(context, "MsData char count : " + msdt , Toast.LENGTH_SHORT).show();; alertDialogBuilder .setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { try { Intent main = new Intent(context, MainActivity.class); main.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(main); } catch (Exception e) { Log.d(TAG, "Error while starting Main activity from Dialog ! "); } } }) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { Toast.makeText(context,"Your Order will be sent to "+ mail +" please check your inbox for comfirmation." , Toast.LENGTH_SHORT).show(); dialog.cancel(); } }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.dismiss(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); }

  • Answer:

    You can define you custom interface simmilar to this one: public interface MyDialogClickListener { void onPositiveClicked(String value); } Then you create instance and pass to method, where you create dialog: public static void showDeafultDialog(..., MyDialogClickListener listener) { // ... .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { listener.onPositiveClicked("you can pass yout value here") } }) // ... } Handle result: private void sendSms() { AlertDialogManager.showDeafultDialog(..., new MyDialogClickListener() { @Override public void onPositiveClicked(String value) { // do whatever you want with value } });

heskey at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.