you can implement a generic solution for decisions and use in another case not just for yes/no and custom the alert with animations or layout:
Something like this; first create your class for transfer datas:
public class AlertDecision {
private String question = "";
private String strNegative = "";
private String strPositive = "";
public AlertDecision question(@NonNull String question) {
this.question = question;
return this;
}
public AlertDecision ansPositive(@NonNull String strPositive) {
this.strPositive = strPositive;
return this;
}
public AlertDecision ansNegative(@NonNull String strNegative) {
this.strNegative = strNegative;
return this;
}
public String getQuestion() {
return question;
}
public String getAnswerNegative() {
return strNegative;
}
public String getAnswerPositive() {
return strPositive;
}
}
after a interface for return the result
public interface OnAlertDecisionClickListener {
/**
* Interface definition for a callback to be invoked when a view is clicked.
*
* @param dialog the dialog that was clicked
* @param object The object in the position of the view
*/
void onPositiveDecisionClick(DialogInterface dialog, Object object);
void onNegativeDecisionClick(DialogInterface dialog, Object object);
}
Now you can create an utils for access easily (in this class you can implement different animation or custom layout for the alert):
public class AlertViewUtils {
public static void showAlertDecision(Context context,
@NonNull AlertDecision decision,
final OnAlertDecisionClickListener listener,
final Object object) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(decision.getQuestion());
builder.setPositiveButton(decision.getAnswerPositive(),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.onPositiveDecisionClick(dialog, object);
}
});
builder.setNegativeButton(decision.getAnswerNegative(),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.onNegativeDecisionClick(dialog, object);
}
});
android.support.v7.app.AlertDialog dialog = builder.create();
dialog.show();
}
}
and the last call in activity or fragment; you can use this in you case or for other task:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
initResources();
}
public void initResources() {
Button doSomething = (Button) findViewById(R.id.btn);
doSomething.setOnClickListener(getDecisionListener());
}
private View.OnClickListener getDecisionListener() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDecision decision = new AlertDecision()
.question("question ...")
.ansNegative("negative action...")
.ansPositive("positive action... ");
AlertViewUtils.showAlertDecision(MainActivity.this,
decision, getOnDecisionListener(), v);
}
};
}
private OnAlertDecisionClickListener getOnDecisionListener() {
return new OnAlertDecisionClickListener() {
@Override
public void onPositiveDecisionClick(DialogInterface dialog, Object object) {
//do something like create, show views, etc...
}
@Override
public void onNegativeDecisionClick(DialogInterface dialog, Object object) {
//do something like delete, close session, etc ...
}
};
}
}