Skip to content

Instantly share code, notes, and snippets.

@leinardi
Created October 25, 2016 12:01
Show Gist options
  • Save leinardi/8cc87f52e7db96aca6b59bbeac2282a8 to your computer and use it in GitHub Desktop.
Save leinardi/8cc87f52e7db96aca6b59bbeac2282a8 to your computer and use it in GitHub Desktop.

Revisions

  1. leinardi created this gist Oct 25, 2016.
    76 changes: 76 additions & 0 deletions ProgressDialogFragment.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    public class ProgressDialogFragment extends DialogFragment {
    private static final String TAG = ProgressDialogFragment.class.getName();
    private static final String KEY_TITLE = TAG + "_message";
    private static final String KEY_MESSAGE = TAG + "_title";

    /**
    * Create and show a ProgressDialogFragment with the given message.
    *
    * @param fragmentManager The FragmentManager this fragment will be added to
    * @param message displayed message
    */
    public static void showProgressDialog(FragmentManager fragmentManager, CharSequence message) {
    showProgressDialog(fragmentManager, message, null);
    }

    /**
    * Create and show a ProgressDialogFragment with the given message.
    *
    * @param fragmentManager The FragmentManager this fragment will be added to
    * @param message displayed message
    * @param title displayed title
    */
    public static void showProgressDialog(FragmentManager fragmentManager, @NonNull CharSequence message, CharSequence title) {
    ProgressDialogFragment dialogFragment = (ProgressDialogFragment) fragmentManager.findFragmentByTag(TAG);
    if (dialogFragment == null) {
    dialogFragment = new ProgressDialogFragment();
    final Bundle args = new Bundle();
    args.putCharSequence(KEY_MESSAGE, message);
    if (title != null) {
    args.putCharSequence(KEY_TITLE, title);
    }
    dialogFragment.setArguments(args);
    dialogFragment.show(fragmentManager, TAG);
    } else {
    updateDialog(dialogFragment, message, title);
    }
    }

    private static void updateDialog(ProgressDialogFragment dialogFragment, CharSequence message, CharSequence title) {
    Dialog dialog = dialogFragment.getDialog();
    AppCompatTextView textView = (AppCompatTextView) dialog.findViewById(R.id.message);
    textView.setText(message);
    if (title != null) {
    dialog.setTitle(title);
    }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final LayoutInflater inflater = LayoutInflater.from(builder.getContext());
    final FrameLayout root = (FrameLayout) inflater.inflate(R.layout.progress_dialog, null /* root */);
    final AppCompatTextView textView = (AppCompatTextView) root.findViewById(R.id.message);
    textView.setText(args.getCharSequence(KEY_MESSAGE));
    final CharSequence titleLabel = args.getCharSequence(KEY_TITLE);
    if (titleLabel != null) {
    builder.setTitle(titleLabel);
    }
    builder.setView(root);
    setCancelable(false);
    return builder.create();
    }

    /**
    * Dismiss an already shown ProgressDialogFragment
    *
    * @param fragmentManager The FragmentManager this fragment will be added to
    */
    public static void dismissProgressDialog(FragmentManager fragmentManager) {
    ProgressDialogFragment dialogFragment = (ProgressDialogFragment) fragmentManager.findFragmentByTag(TAG);
    if (dialogFragment != null) {
    dialogFragment.dismiss();
    }
    }
    }
    31 changes: 31 additions & 0 deletions progress_dialog.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
    android:id="@+id/body"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:paddingBottom="10dip"
    android:paddingEnd="8dip"
    android:paddingStart="8dip"
    android:paddingTop="10dip">

    <ProgressBar
    android:id="@android:id/progress"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="12dip"
    android:max="10000"/>

    <android.support.v7.widget.AppCompatTextView
    android:id="@+id/message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>
    </LinearLayout>
    </FrameLayout>