Get Value from Dialogfragment

Get value from DialogFragment

Assuming that you want to foward result to the calling Activity:) try this code snippet:

public class QuantityDialogFragment extends DialogFragment implements OnClickListener {

private EditText editQuantity;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
editQuantity = new EditText(getActivity());
editQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);

return new AlertDialog.Builder(getActivity()).setTitle(R.string.app_name).setMessage("Please Enter Quantity")
.setPositiveButton("OK", this).setNegativeButton("CANCEL", null).setView(editQuantity).create();

}

@Override
public void onClick(DialogInterface dialog, int position) {
String value = editQuantity.getText().toString();
Log.d("Quantity: ", value);
MainActivity callingActivity = (MainActivity) getActivity();
callingActivity.onUserSelectValue(value);
dialog.dismiss();
}
}

and on Your activity add :

public class MainActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuantityDialogFragment dialog = new QuantityDialogFragment();
dialog.show(getSupportFragmentManager(), "Dialog");
}

/**
* callback method from QuantityDialogFragment, returning the value of user
* input.
*
* @param selectedValue
*/
public void onUserSelectValue(String selectedValue) {
// TODO add your implementation.
}
}

How to get the result from the DialogFragment in Android?

Fragment class:

public class MyFragment extends Fragment {
int mStackLevel = 0;
public static final int DIALOG_FRAGMENT = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState != null) {
mStackLevel = savedInstanceState.getInt("level");
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}

void showDialog(int type) {

mStackLevel++;

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
Fragment prev = getActivity().getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);

switch (type) {

case DIALOG_FRAGMENT:

DialogFragment dialogFrag = MyDialogFragment.newInstance(123);
dialogFrag.setTargetFragment(this, DIALOG_FRAGMENT);
dialogFrag.show(getFragmentManager().beginTransaction(), "dialog");

break;
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case DIALOG_FRAGMENT:

if (resultCode == Activity.RESULT_OK) {
// After Ok code.
} else if (resultCode == Activity.RESULT_CANCELED){
// After Cancel code.
}

break;
}
}
}

}

DialogFragment class:

public class MyDialogFragment extends DialogFragment {

public static MyDialogFragment newInstance(int num){

MyDialogFragment dialogFragment = new MyDialogFragment();
Bundle bundle = new Bundle();
bundle.putInt("num", num);
dialogFragment.setArguments(bundle);

return dialogFragment;

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

return new AlertDialog.Builder(getActivity())
.setTitle(R.string.ERROR)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.ok_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent());
}
}
)
.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
}
})
.create();
}
}

I hope it's helpful!

How to get the Data from DialogFragment to MainActivity in Android?

Create an interface like-

CustomDialogInterface.java

public interface CustomDialogInterface {

// This is just a regular method so it can return something or
// take arguments if you like.
public void okButtonClicked(String value);


}

and modify your MyAlert.java by-

public class MyAlert extends DialogFragment implements OnClickListener {

private EditText getEditText;
MainActivity callBackActivity;
CustomDialogInterface customDI;

public MyAlert(CustomDialogInterface customDI)
{
this.customDI = customDI;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

callBackActivity = new MainActivity();
getEditText = new EditText(getActivity());
getEditText.setInputType(InputType.TYPE_CLASS_TEXT);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Get UserName :");
builder.setMessage("Enter Your Name :");
builder.setPositiveButton("Ok", this);
builder.setNegativeButton("Cancel", null);
builder.setView(getEditText);
return builder.create();
}

@Override
public void onClick(DialogInterface dialog, int which) {
String value = getEditText.getText().toString();
Log.d("Name : ", value);
dialog.dismiss();
customDI.okButtonClicked(value);

}

void setCustomDialogInterface(CustomDialogInterface customDialogInterface){
this. customDI = customDialogInterface;
c}

}

And implement CustomDialogInterface in your MainActivity and overide method okButtonClicked()
When onClick will be called then your MainActivity's onButtonClicked will be called .

and change showAlert to -

class MainActivity..... implements CustomDialogInterface { 

public void showMyAlert(View view) {
MyAlert myAlert = new MyAlert(this);
myAlert.show(getFragmentManager(), "My New Alert");
}

@Overide
public void okButtonClicked(String value){
// handle
}
}

or use following code :

  public void showMyAlert(View view) {

MyAlert myAlert = new MyAlert(this);
myAlert.setCustomDialogInterface(new CustomDialogInterface() {
@Override
public void okButtonClicked(String value) {
//handle click
}
});
myAlert.show(getFragmentManager(), "My New Alert");

}

How can i get data back from this dialog fragment in this code?

Create a callback interface, implement it on your code (where you set up the listeners), then pass it as a parameter in the dialog.

Example:

public interface Callback {
void onResult(Object result);
}
private Callback callback;
public ListTipo(Callback callback) {
this.callback=callback;
}

// where you want to pass the result
callback.onResult(YOUR_RESULT);

Where you create the dialog:

xTipo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new ListTipo(result -> {
// run your code with result
}).show(getSupportFragmentManager(),"ListTipo");
}
});

How to get data from DialogFragment to a Fragment?

The Fragment.onActivityResult() method is useful in this situation. It takes getTargetRequestCode(), which is a code you set up between fragments so they can be identified. In addition, it takes a request code, normally just 0 if the code worked well, and then an Intent, which you can attach a string too, like so

Intent intent = new Intent();
intent.putExtra("STRING_RESULT", str);

Also, the setTargetFragment(Fragment, requestCode) should be used in the fragment that the result is being sent from to identify it. Overall, you would have code in the requesting fragment that looks like this:

FragmentManager fm = getActivity().getSupportFragmentManager();
DialogFragment dialogFragment = new DialogFragment();
dialogFragment.setTargetFragment(this, REQUEST_CODE);
dialogFragment.show();

The class to send data (the DialogFragment) would use this Fragment we just defined to send the data:

private void sendResult(int REQUEST_CODE) {
Intent intent = new Intent();
intent.putStringExtra(EDIT_TEXT_BUNDLE_KEY, editTextString);
getTargetFragment().onActivityResult(
getTargetRequestCode(), REQUEST_CODE, intent);
}

To receive the data, we use this type of class in the Fragment which initially started the DialogFragment:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Make sure fragment codes match up
if (requestCode == DialogFragment.REQUEST_CODE) {
String editTextString = data.getStringExtra(
DialogFragment.EDIT_TEXT_BUNDLE_KEY);

At this point, you have the string from your EditText from the DialogFragment in the parent fragment. Just use the sendResult(int) method in your TextChangeListener() anonymous class so that the text is sent when you need it.

Receive result from DialogFragment

Use myDialogFragment.setTargetFragment(this, MY_REQUEST_CODE) from the place where you show the dialog, and then when your dialog is finished, from it you can call getTargetFragment().onActivityResult(getTargetRequestCode(), ...), and implement onActivityResult() in the containing fragment.

It seems like an abuse of onActivityResult(), especially as it doesn't involve activities at all. But I've seen it recommended by official google people, and maybe even in the api demos. I think it's what g/setTargetFragment() were added for.

How to return dialog fragment values from dialog to main activity?

Getting stuff from other Activities/Fragments? startActivityForResult
More generally you should take a look at MVVM and create a ViewModel that holds all the Information you need.
Then you can react to that Infomation (LiveData Observer maybe?) in you Activity with the WebView.

Set fragment edittext value from dialogfragment

You need to pass EditText data one Fragment to DialogFragment

First remove this start.setOnClickListener() you can use these

1 -on Text Change Listener

2 - FocusChangeListener

3 -Search and handle its click

on Action pass EditText data

Bundle args = new Bundle();
args.putString("key", "value");
DialogFragment newFragment = new YourDialogFragment();
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "TAG");

and get value in DialogFragment

Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");


Related Topics



Leave a reply



Submit