Reputation: 13
I'm trying to create a new class for a standard alert dialog with 2 buttons (positive and negative), but I do not know how to add parameters to the class.
The parameters are 6 strings, a title, message, a positive button, a positive text for toast, a negative button and a negative text for toast.
private ExampleDialogListener listener;
private String title;
private String positive;
private String message;
private String positivetext;
private String negative;
private String negativetext;
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) //I tried parameters here but it did not work
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//more code below
The paramters I tried adding at the comment was addtext(String title, message, positive, positivetext, negative, negativetext)
I'm new to java and I tried searching this up here, and here, but I do now know what to do due to the onCreateDialog(@Nullable Bundle savedInstanceState)
at line 10. (I got the full code from here, and only roughly knows how it works.)
the full code is
package com.example.productiveapp2;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
public class ExampleDialog extends AppCompatDialogFragment {
private ExampleDialogListener listener;
private String title;
private String positive;
private String message;
private String positivetext;
private String negative;
private String negativetext;
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title)
.setMessage(message)
.setNegativeButton(negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getContext(), negativetext, Toast.LENGTH_SHORT);
toast.show();
}
})
.setPositiveButton(positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getContext(), positivetext, Toast.LENGTH_SHORT);
toast.show();
listener.onYesClicked();
}
});
return builder.create();
}
public interface ExampleDialogListener {
void onYesClicked();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (ExampleDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ "must implement ExampleDialogListener");
}
}
}
Upvotes: 1
Views: 374
Reputation: 14173
You must create a new instance of ExampleDialog, providing title, message, positive, positivetext, negative, negativetext as arguments.
Your code will be like this:
public class ExampleDialog extends AppCompatDialogFragment {
// Create constant values for each key in dialog's arguments.
private static final String ARGUMENT_TITLE = "title";
private static final String ARGUMENT_POSITIVE = "positive";
private static final String ARGUMENT_MESSAGE = "message";
private static final String ARGUMENT_POSITIVE_TEXT = "positive_text";
private static final String ARGUMENT_NEGATIVE = "negative";
private static final String ARGUMENT_NEGATIVE_TEXT = "negative_text";
private ExampleDialogListener listener;
private String title;
private String positive;
private String message;
private String positivetext;
private String negative;
private String negativetext;
/**
* Create a new instance of ExampleDialog, providing
* title, message, positive, positivetext, negative, negativetext as arguments.
*/
public static ExampleDialog newInstance(String title, String positive, String message,
String positivetext, String negative, String negativetext) {
Bundle args = new Bundle();
// Store all arguments into bundle.
args.putString(ARGUMENT_TITLE, title);
args.putString(ARGUMENT_POSITIVE, positive);
args.putString(ARGUMENT_MESSAGE, message);
args.putString(ARGUMENT_POSITIVE_TEXT, positivetext);
args.putString(ARGUMENT_NEGATIVE, negative);
args.putString(ARGUMENT_NEGATIVE_TEXT, negativetext);
ExampleDialog fragment = new ExampleDialog();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
// Retrieve all arguments from bundle here.
title = getArguments().getString(ARGUMENT_TITLE);
positive = getArguments().getString(ARGUMENT_POSITIVE);
message = getArguments().getString(ARGUMENT_MESSAGE);
positivetext = getArguments().getString(ARGUMENT_POSITIVE_TEXT);
negative = getArguments().getString(ARGUMENT_NEGATIVE);
negativetext = getArguments().getString(ARGUMENT_NEGATIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title)
.setMessage(message)
.setNegativeButton(negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getContext(), negativetext, Toast.LENGTH_SHORT);
toast.show();
}
})
.setPositiveButton(positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getContext(), positivetext, Toast.LENGTH_SHORT);
toast.show();
listener.onYesClicked();
}
});
return builder.create();
}
public interface ExampleDialogListener {
void onYesClicked();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (ExampleDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ "must implement ExampleDialogListener");
}
}
}
Whenever you want to create a new instance of ExampleDialog, use this code.
// Replace 6 params/arguments with your own
ExampleDialog exampleDialog = ExampleDialog.newInstance("title", "positive", "message",
"positive", "negative", "negativetext");
You can find more information about how to pass arguments into DialogFragment here.
Upvotes: 3