Skip to content

Instantly share code, notes, and snippets.

@josemigallas
Created October 5, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josemigallas/1cc03ec52af709f536c5bf4ae39c5597 to your computer and use it in GitHub Desktop.
Save josemigallas/1cc03ec52af709f536c5bf4ae39c5597 to your computer and use it in GitHub Desktop.
Very cool dialog that blocks the screen, to be used during some asynchronous process preventing user from interacting with the screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:indeterminate="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/loading"
android:textColor="@android:color/primary_text_light"
android:textSize="16sp" />
</LinearLayout>
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class LoadingDialog extends DialogFragment {
private static final String ARG_TITLE = "TITLE";
private String title;
public static LoadingDialog newInstance(String title) {
LoadingDialog fragment = new LoadingDialog();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
fragment.setArguments(args);
return fragment;
}
public static LoadingDialog newInstance() {
return new LoadingDialog();
}
public LoadingDialog() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_TITLE);
}
setCancelable(false);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext())
.setView(R.layout.dialog_loading);
if (title != null) {
alertDialog.setTitle(title);
}
return alertDialog.create();
}
}
@josemigallas
Copy link
Author

josemigallas commented Oct 5, 2017

Usage

private LoadingDialog loadingDialog;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loadingDialog = LoadingDialog.newInstance("title");
}

protected void showLoading() {
    loadingDialog.show(getSupportFragmentManager(), "loading");
}

protected void hideLoading() {
    loadingDialog.dismiss();
}

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