Skip to content

Instantly share code, notes, and snippets.

@rocboronat
Last active August 29, 2015 14:20
Show Gist options
  • Save rocboronat/c5674f342bfd1b01c361 to your computer and use it in GitHub Desktop.
Save rocboronat/c5674f342bfd1b01c361 to your computer and use it in GitHub Desktop.
A common DialogFragment with a confirm and a cancel button
package com.infojobs.app.base.view.fragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import at.rocboron.R;
/**
* A simple Accept/Cancel Dialog, to be used as a common component on the whole app.
* The caller can implement all, one or none of this DialogFragment interfaces.
* Why three interfaces instead of one? Good question! It's 'cause the I of SOLID.
* <p/>
* Created by Roc Boronat (roc@fewlaps.com) on 29/04/15 - It's Eskerda's birthday!
* And greatly improved by Romina Liuzzi on 06/05/15 - It's Oriol Fernandez birthday!
*/
public class ConfirmCancelDialogFragment extends DialogFragment {
public static final String ARG_ACCEPT_BUTTON = "argButtonAccept";
public static final String ARG_CANCEL_BUTTON = "argButtonCancel";
public static final String ARG_TITLE = "argTitle";
public static final String ARG_BODY = "argBody";
public static final String ARG_EXTRA = "argExtra";
public static final String ARG_REQUEST_CODE = "argRequestCode";
public static final String ARG_HIDE_LEFT_BUTTON = "argHideLeftButton";
public static final String ARG_HIDE_RIGHT_BUTTON = "argHideRightButton";
public OnDialogAcceptedListener acceptedListener = null;
public OnDialogDeclinedListener declinedListener = null;
public OnDialogDismissedListener dismissedListener = null;
protected Integer requestCode = null;
public static void open(FragmentActivity fragmentActivity) {
DialogFragment newFragment = new ConfirmCancelDialogFragment();
newFragment.show(fragmentActivity.getSupportFragmentManager(), "CONFIRM_LOGOUT_FORM_DIALOG_FRAGMENT");
}
public static void open(FragmentActivity fragmentActivity, String title) {
DialogFragment newFragment = new ConfirmCancelDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
newFragment.setArguments(args);
newFragment.show(fragmentActivity.getSupportFragmentManager(), "CONFIRM_LOGOUT_FORM_DIALOG_FRAGMENT");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_fragment_confirm_cancel, null);
if (getArguments() != null && getArguments().containsKey(ARG_TITLE)) {
TextView titleTV = (TextView) v.findViewById(R.id.tv_title);
titleTV.setText(getArguments().getString(ARG_TITLE));
LinearLayout titleLL = (LinearLayout) v.findViewById(R.id.ll_title);
titleLL.setVisibility(View.VISIBLE);
}
if (getArguments() != null && getArguments().containsKey(ARG_BODY)) {
TextView titleTV = (TextView) v.findViewById(R.id.tv_body);
titleTV.setText(getArguments().getString(ARG_BODY));
}
if (getArguments() != null && getArguments().containsKey(ARG_ACCEPT_BUTTON)) {
TextView titleTV = (TextView) v.findViewById(R.id.bt_accept);
titleTV.setText(getArguments().getString(ARG_ACCEPT_BUTTON));
}
if (getArguments() != null && getArguments().containsKey(ARG_CANCEL_BUTTON)) {
TextView titleTV = (TextView) v.findViewById(R.id.bt_cancel);
titleTV.setText(getArguments().getString(ARG_CANCEL_BUTTON));
}
builder.setView(v);
if (getArguments() != null && getArguments().containsKey(ARG_REQUEST_CODE)) {
requestCode = getArguments().getInt(ARG_REQUEST_CODE);
}
if (getArguments() != null && getArguments().getBoolean(ARG_HIDE_LEFT_BUTTON)) {
v.findViewById(R.id.bt_cancel).setVisibility(View.GONE);
v.findViewById(R.id.v_divider).setVisibility(View.GONE);
} else {
v.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Xiti.tagNavigation("Alert-cancel", getActivity());
if (declinedListener != null) {
declinedListener.onDialogDeclined(requestCode);
}
dismiss();
}
});
}
if (getArguments() != null && getArguments().getBoolean(ARG_HIDE_RIGHT_BUTTON)) {
v.findViewById(R.id.bt_accept).setVisibility(View.GONE);
v.findViewById(R.id.v_divider).setVisibility(View.GONE);
} else {
v.findViewById(R.id.bt_accept).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Xiti.tagNavigation("Alert-confirm", getActivity());
if (acceptedListener != null) {
acceptedListener.onDialogAccepted(requestCode);
}
dismiss();
}
});
}
return builder.create();
}
@Override
public void onResume() {
super.onResume();
Xiti.tagPage("Applications::Alert::Alert::Confirm-cancel", getActivity());
}
@Override
public void onCancel(DialogInterface dialog) {
if (dismissedListener != null) {
dismissedListener.onDialogDismissed(requestCode);
}
super.onCancel(dialog);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnDialogAcceptedListener) {
acceptedListener = (OnDialogAcceptedListener) activity;
}
if (activity instanceof OnDialogDeclinedListener) {
declinedListener = (OnDialogDeclinedListener) activity;
}
if (activity instanceof OnDialogDismissedListener) {
dismissedListener = (OnDialogDismissedListener) activity;
}
}
public interface OnDialogAcceptedListener {
public void onDialogAccepted(Integer requestCode);
}
public interface OnDialogDeclinedListener {
public void onDialogDeclined(Integer requestCode);
}
public interface OnDialogDismissedListener {
public void onDialogDismissed(Integer requestCode);
}
/**
* Fluent API for creating {@link ConfirmCancelDialogFragment} instances.
*/
@SuppressWarnings("UnusedDeclaration") // Public API.
public static class Builder {
private FragmentActivity fragmentActivity;
private Bundle args;
public Builder(FragmentActivity fragmentActivity) {
if (fragmentActivity == null) {
throw new IllegalArgumentException("fragmentActivity cannot be null");
}
this.args = new Bundle();
this.fragmentActivity = fragmentActivity;
}
public Builder title(String title) {
if (title != null) {
args.putString(ARG_TITLE, title);
}
return this;
}
public Builder body(String body) {
if (body != null) {
args.putString(ARG_BODY, body);
}
return this;
}
public Builder acceptButtonText(String acceptButtonText) {
if (acceptButtonText != null) {
args.putString(ARG_ACCEPT_BUTTON, acceptButtonText);
}
return this;
}
public Builder cancelButtonText(String cancelButtonText) {
if (cancelButtonText != null) {
args.putString(ARG_CANCEL_BUTTON, cancelButtonText);
}
return this;
}
public Builder extra(String extra) {
if (extra != null) {
args.putString(ARG_EXTRA, extra);
}
return this;
}
public Builder requestCode(Integer requestCode) {
if (requestCode != null) {
args.putInt(ARG_REQUEST_CODE, requestCode);
}
return this;
}
public Builder hideLeftButton() {
args.putBoolean(ARG_HIDE_LEFT_BUTTON, true);
return this;
}
public Builder hideRightButton() {
args.putBoolean(ARG_HIDE_RIGHT_BUTTON, true);
return this;
}
public Bundle getArgs() {
return args;
}
public FragmentActivity getFragmentActivity() {
return fragmentActivity;
}
public void build() {
ConfirmCancelDialogFragment confirmCancelDialogFragment = new ConfirmCancelDialogFragment();
confirmCancelDialogFragment.setArguments(args);
confirmCancelDialogFragment.show(fragmentActivity.getSupportFragmentManager(), "ConfirmCancelDialogFragment");
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">
<com.infojobs.app.base.view.widget.CustomFontTextView
android:id="@+id/tv_title"
style="@style/text_normal_medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/gap_high"
tools:text="Just another great title" />
<View style="@style/divider" />
</LinearLayout>
<com.infojobs.app.base.view.widget.CustomFontTextView
android:id="@+id/tv_body"
style="@style/text_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/gap_high"
android:text="@string/global_are_you_sure_question" />
<View
android:id="@+id/divider"
style="@style/divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.infojobs.app.base.view.widget.CustomFontTextView
android:id="@+id/bt_cancel"
style="@style/button_darkest_silk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/gap_high"
android:text="@string/global_cancel"
android:textAllCaps="true" />
<View
android:id="@+id/v_divider"
style="@style/divider"
android:layout_width="@dimen/divider"
android:layout_height="match_parent" />
<com.infojobs.app.base.view.widget.CustomFontTextView
android:id="@+id/bt_accept"
style="@style/button_white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/gap_high"
android:text="@string/global_accept"
android:textAllCaps="true" />
</LinearLayout>
</LinearLayout>
@rocboronat
Copy link
Author

The revision #3 has been pulled from @rliuzzi 's fork. Thanks Romina!

https://gist.github.com/rliuzzi/d94af3fdfae60c71ee56

@rocboronat
Copy link
Author

Revision #4! Now you can hide the left or right button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment