I have an Activity
with 3 Fragments
, now each fragment has 2 ToggleButtons
(total six buttons).
An AlertDialog
is shown to the user on each button click.
Each button does some different action and so the layouts/views of Alert Dialog differ from each other, and so the positive button click for dialogs too differ, while the negative button click are almost same.
I have implemented the logic in an onButtonPressed method in the Activity as
onButtonPressed(View v){
switch(v.getId()){
case R.id.button1:
// create and show an AlertDialog
break;
}
case R.id.button2:
// create and show an AlertDialog
break;
}
case R.id.button3:
// create and show an AlertDialog
break;
}
.
.
.
}
This leads to lots of repetitive lines of code, which isn't the best thing AFAIK. I was willing to know if I should keep current implemention, or create a wrapper class something for creating and showing AlertDialogs.
Dialogs in Android are used to shows alerts for making decisions or to edit a single value. But there are some differences between an AlertDialog and a Dialog. In an AlertDialog you always want to show a message and at least one Button for user interaction.
Action Button - This displays action buttons for user interaction. There should not be more than three action buttons in AlertDialog, and they are positive, negative, and neutral. Positive is used to accept and continue with the action. Negative to cancel the action.
Showing a Dialog You should normally create dialogs from within your Activity's onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the “owner” of each dialog.
You can create a class and then extend DialogFragment
class and then override its OnCreateDialog()
method, then you create a static method to create an instance and pass arguments.
The OnCreateDialog
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
//set title,message or view ....
// when you done
return dialog.create();
}
The static method to put arguments ( Static Factory Pattern Design )
public static CustomAlertDialog newInstance(String title,String message) {
CustomAlertDialog customAD = new CustomAlertDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
customAD.setArguments(args);
return customAD;
}
Then you could use it like this
DialogFragment dialog = CustomAlertDialog.newInstance("titulo","message");
dialog.show(/* etc.. */ );
Modern approach to creating AlertDialog
s is actually to wrap them inside DialogFragment
s, so you need to create a class extending DialogFragment
, override its onCreateDialog()
method and return an instance of AlertDialog
from it. For the purpose of customizing the dialogs you can add a newInstance()
method to your DialogFragment
as described in this discussion and pass custom parameters to your class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With