How to Pass Values Between a Dialog and an Activity

How can I pass values between a Dialog and an Activity?

You can do that in different ways... actually, if your dialog has only an "OK" button to dismiss, why don't you just create a custom dialog using the AlertDialog.Builder class instead of subclassing Dialog?

Anyway... let's suppose you have good reasons to do it the way you did it. In that case, I'd use the ObserverPattern. Something like this:

public class CustomDialog extends Dialog  {

private String name;
public static EditText etName;
public String zip;
OnMyDialogResult mDialogResult; // the callback

public CustomDialog(Context context, String name) {
super(context);
this.name = name;
}

@Override
public void onCreate(Bundle savedInstanceState) {
// same you have
}

private class OKListener implements android.view.View.OnClickListener {
@Override
public void onClick(View v) {
if( mDialogResult != null ){
mDialogResult.finish(String.valueOf(etName.getText()));
}
CustomDialog.this.dismiss();
}
}

public void setDialogResult(OnMyDialogResult dialogResult){
mDialogResult = dialogResult;
}

public interface OnMyDialogResult{
void finish(String result);
}
}

On your activity:

CustomDialog dialog;
// initialization stuff, blah blah
dialog.setDialogResult(new OnMyDialogResult(){
public void finish(String result){
// now you can use the 'result' on your activity
}
});

Reading your code it seems you already tried something similar.

Edit: doing it the easy way

You can still use your mycustomdialog layout. And this is how you would use the AlertDialog.Builder:

LayoutInflater inflater = LayoutInflater.from(YourActivity.this);
final View yourCustomView = inflater.inflate(R.layout.mycustomdialog, null);

final TextView etName = (EditText) yourCustomView.findViewById(R.id.EditZip);
AlertDialog dialog = new AlertDialog.Builder(YourActivity.this)
.setTitle("Enter the Zip Code")
.setView(yourCustomView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mSomeVariableYouHaveOnYourActivity = etName.getText().toString();
}
})
.setNegativeButton("Cancel", null).create();
dialog.show();

Robust way to pass value back from Dialog to Activity on Android?

You are on the right track, I follow the method recommended by the Android Developers - Using DialogFragments article.

You create your DialogFragment and define an interface that the Activity will implement, like you have done above with this:

public interface MyDialogFragmentListener {
public void onReturnValue(String foo);
}

Then in the DialogFragment when you want to return the result to the Activity you cast the activity to the interface:

@Override
public void onClick(DialogInterface dialog, int id) {
MyDialogFragmentListener activity = (MyDialogFragmentListener) getActivity();
activity.onReturnValue("some value");
}

Then in the Activity you implement that interface and grab the value:

public class MyActivity implements MyDialogFragmentListener {
...
@Override
public void onReturnValue(String foo) {
Log.i("onReturnValue", "Got value " + foo + " back from Dialog!");
}
}

How to Pass Value In Activity to Dialog and Display it in Android Studio

Make a static method on you dialog like this :

public static MyDialog myDialog(Value what u need){
MyDialog myDialog = new MyDialog();
return myDialog;
}

After that you can call this method on your activity. It will return with your dialog, and you can use .show on it. On the static method you can save your value.

Passing data from an adapter over a dialog to a activity

Instead of having the dialog in a separate file I just moved it into the adapter class

Passing Data from Dialog box to activity

Instead of calling method from class which extend Activity by creating object in non-activity class you should create custom event listener using interface which trigger event when onClick happen in Dialog.

Android Custom Event Listener

How to pass Values from a dialog event to parent activity?

Solution:

Add the below code for a Custom Dialog:

    final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.your_dialog_layout);

EditText text1 = (EditText) dialog.findViewById(R.id.your_edittext_Id1);
EditText text2 = (EditText) dialog.findViewById(R.id.your_edittext_Id2);

Button dialogButton = (Button) dialog.findViewById(R.id.your_button_Id);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String text1 = text1.getText.toString().trim();
String text2 = text2.getText.toString().trim();

.... (Write Whatever You Want The Button To Do)

}
});

dialog.show();

According to the above code, when you click the button, it takes the data from both the EditTexts and stores in those two strings. You may use those datas' wherever you require.

Any doubts, please comment.

Hope it helps.

pass data between a activity to dialogfragment

When preparing Intent here:

intent.putExtra(estate, "estate");
intent.putExtra(block, "block");
intent.putExtra(ha, "ha");

using keys as values and values as keys causing issue and in MainActivity getting all values null.

Change it as:

intent.putExtra("estate",estate);
intent.putExtra("block",block);
intent.putExtra("ha",ha);

How pass data from Activity to Dialog in Android Studio

You are implementing AppCompatDialogFragment, you can pass data to it by setting its arguments

public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
Bundle args = new Bundle();
args.putString("title", title);
configWIFIDialog.setArguments(args);
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}

you can learn more from codepath



Related Topics



Leave a reply



Submit