Last active
January 26, 2017 19:35
-
-
Save ersin-ertan/45297b15acb36fefa0bbb24b3351abce to your computer and use it in GitHub Desktop.
Generic dialogframent to take in arguments, size itself, and set anything as its listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override protected void onViewBound(@NonNull View view) { | |
super.onViewBound(view); | |
TestDialog.reAttachListener(getActivity(), TestDialog.class, this); | |
btnShowDialog.setOnClickListener(v -> { | |
TestDialog gd = new TestDialogBuilder(R.dimen.dialog_add_inspection_item_height, | |
R.dimen.dialog_add_inspection_item_width, R.string.camera, R.string.title).build(); | |
TestDialog.show(getActivity(), gd, this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/wrap_content" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:descendantFocusability="beforeDescendants" | |
android:focusableInTouchMode="true" | |
> | |
<android.support.constraint.Guideline | |
android:id="@+id/guide_start" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
app:layout_constraintGuide_begin="@dimen/dialog_guide_begin" | |
tools:layout_editor_absoluteY="0dp" | |
tools:layout_editor_absoluteX="40dp" | |
/> | |
<android.support.constraint.Guideline | |
android:id="@+id/guide_end" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
app:layout_constraintGuide_end="@dimen/dialog_guide_end" | |
tools:layout_editor_absoluteY="0dp" | |
tools:layout_editor_absoluteX="760dp" | |
/> | |
<android.support.constraint.Guideline | |
android:id="@+id/guide_mid" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
app:layout_constraintGuide_percent="0.5" | |
tools:layout_editor_absoluteY="0dp" | |
tools:layout_editor_absoluteX="400dp" | |
/> | |
<TextView | |
android:id="@+id/txt_title_dialog_generic" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="12dp" | |
android:textAppearance="@style/TextAppearance.FontPath.Medium" | |
android:textColor="@color/grey_dark" | |
app:layout_constraintLeft_toLeftOf="@+id/guide_start" | |
app:layout_constraintTop_toTopOf="parent" | |
android:text="@string/title" | |
app:layout_constraintRight_toLeftOf="@+id/guide_end" | |
/> | |
<LinearLayout | |
android:id="@+id/layout_linear_dialog_generic" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="32dp" | |
android:background="@color/pink" | |
android:divider="@drawable/div_horizontal" | |
android:orientation="vertical" | |
android:showDividers="beginning|middle|end" | |
app:layout_constraintBottom_toTopOf="@+id/div_buttons_top" | |
app:layout_constraintLeft_toLeftOf="@+id/guide_start" | |
app:layout_constraintTop_toBottomOf="@+id/txt_title_dialog_generic" | |
app:layout_constraintVertical_bias="0.0" | |
tools:layout_height="400dp" | |
app:layout_constraintRight_toLeftOf="@+id/guide_end" | |
/> | |
<View | |
android:id="@+id/div_buttons_top" | |
android:layout_width="0dp" | |
android:layout_height="1dp" | |
android:background="@drawable/div_horizontal" | |
app:layout_constraintBottom_toTopOf="@+id/btn_dialog_generic_negative" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
/> | |
<View | |
android:id="@+id/div_buttons_mid" | |
android:layout_width="1dp" | |
android:layout_height="0dp" | |
android:background="@drawable/div_vertical" | |
app:layout_constraintBottom_toBottomOf="@+id/btn_dialog_generic_negative" | |
app:layout_constraintLeft_toRightOf="@+id/btn_dialog_generic_negative" | |
app:layout_constraintRight_toLeftOf="@+id/btn_dialog_generic_positive" | |
app:layout_constraintTop_toTopOf="@+id/btn_dialog_generic_negative" | |
/> | |
<Button | |
app:layout_constraintTop_toBottomOf="@id/layout_linear_dialog_generic" | |
android:id="@+id/btn_dialog_generic_negative" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:background="@null" | |
android:gravity="center" | |
android:textAllCaps="true" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toLeftOf="@+id/guide_mid" | |
android:text="@string/cancel" | |
app:layout_constraintVertical_bias="1.0" | |
android:layout_marginTop="34dp" | |
/> | |
<Button | |
android:id="@+id/btn_dialog_generic_positive" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="1dp" | |
android:background="@null" | |
android:textAllCaps="true" | |
app:layout_constraintBottom_toBottomOf="@+id/btn_dialog_generic_negative" | |
app:layout_constraintLeft_toLeftOf="@+id/guide_mid" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="@+id/btn_dialog_generic_negative" | |
android:text="@string/ok" | |
/> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FragmentWithArgs public abstract class GenericDialog<DataType> extends DialogFragment { | |
@Arg @StringRes public int strTitle; | |
@Arg(required = false) public @StringRes int strBtnPositive; | |
@Arg(required = false) public @StringRes int strBtnNegative; | |
@Arg @DimenRes public int dimenWidth; | |
@Arg @DimenRes public int dimenHeight; | |
@BindView(R.id.txt_title_dialog_generic) TextView tvTitle; | |
@BindView(R.id.btn_dialog_generic_positive) Button btnPositive; | |
@BindView(R.id.btn_dialog_generic_negative) Button btnNegative; | |
@BindView(R.id.layout_linear_dialog_generic) LinearLayout linearLayout; | |
protected int[] layouts; | |
protected OnPositiveClickListener onClickListener; | |
public interface OnPositiveClickListener { | |
void onPositiveClick(Object data); | |
} | |
@Override public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
FragmentArgs.inject(this); | |
} | |
@Override public void onStart() { | |
super.onStart(); | |
getDialog().getWindow() | |
.setLayout(getResources().getDimensionPixelSize(dimenWidth), | |
getResources().getDimensionPixelSize(dimenHeight)); | |
} | |
@Nullable @Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, | |
@Nullable Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.dialog_generic, container); | |
if (layouts.length > 0) { | |
addViews(getActivity(), rootView, layouts); | |
} | |
return rootView; | |
} | |
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
ButterKnife.bind(this, view); | |
tvTitle.setText(strTitle); | |
btnPositive.setText(strBtnPositive != 0 ? strBtnPositive : R.string.ok); | |
btnNegative.setText(strBtnNegative != 0 ? strBtnNegative : R.string.cancel); | |
} | |
@OnClick(R.id.btn_dialog_generic_positive) public abstract void onPositiveClick(); | |
@OnClick(R.id.btn_dialog_generic_negative) void onNegativeClick() { | |
dismiss(); | |
} | |
private void setOnClickListener(@NonNull OnPositiveClickListener onClickListener) { | |
this.onClickListener = onClickListener; | |
} | |
protected void onPositiveClickImp(DataType data) { | |
if (data != null && onClickListener != null) { | |
onClickListener.onPositiveClick(data); | |
} | |
} | |
@Override public void onDetach() { | |
onClickListener = null; | |
super.onDetach(); | |
} | |
public static void addViews(Activity activity, View rootView, int[] layouts) { | |
LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.layout_linear_dialog_generic); | |
for (int i = 0; i < layouts.length; i++) { | |
LayoutInflater.from(activity).inflate(layouts[0], ll, true); | |
} | |
} | |
public static void show(Activity activity, GenericDialog genericDialog, | |
OnPositiveClickListener onPositiveClickListener) { | |
genericDialog.setOnClickListener(onPositiveClickListener); | |
genericDialog.show(activity.getFragmentManager(), genericDialog.getClass().getSimpleName()); | |
} | |
public static void reAttachListener(@NonNull Activity activity, Class fragment, | |
@NonNull OnPositiveClickListener onPositiveClickListener) { | |
GenericDialog gd = | |
(GenericDialog) activity.getFragmentManager().findFragmentByTag(fragment.getSimpleName()); | |
if (gd != null) gd.setOnClickListener(onPositiveClickListener); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FragmentWithArgs public class TestDialog extends GenericDialog<Integer> { | |
@Arg @StringRes public int strText1; | |
@BindView(android.R.id.text1) TextView text1; | |
int i = 0; | |
{ | |
layouts = new int[] { android.R.layout.simple_list_item_1 }; | |
} | |
@Override public void onPositiveClick() { | |
onPositiveClickImp(i++); | |
if(i == 2) dismiss(); // validation | |
} | |
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
text1.setText(strText1 != 0 ? strText1 : R.string.ok); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment