Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Created March 30, 2017 08:36
Show Gist options
  • Save Jeevuz/566532672386786d481f16475b15f108 to your computer and use it in GitHub Desktop.
Save Jeevuz/566532672386786d481f16475b15f108 to your computer and use it in GitHub Desktop.
Progress DialogFragment with only progress view on the dimmed screen
/**
* Dialog fragment showing progress widget on dimmed screen.
* @author Vasili Chyrvon ([email protected])
*/
class ProgressDimDialogFragment : AppCompatDialogFragment() {
companion object {
val TAG = "ProgressDimDialogFragment"
private val ARGS_DISPATCH_BACK_PRESS = "args_dispatches_back_press"
fun newInstance(dispatchBackPress: Boolean = true): ProgressDimDialogFragment {
val args = Bundle()
args.putBoolean(ARGS_DISPATCH_BACK_PRESS, dispatchBackPress)
val fragment = ProgressDimDialogFragment()
fragment.arguments = args
return fragment
}
}
private val dispatchBackPress by lazy { arguments.getBoolean(ARGS_DISPATCH_BACK_PRESS) }
private var listener: BackPressListener? = null
interface BackPressListener {
fun onProgressBackPressed()
}
override fun onAttach(context: Context?) {
super.onAttach(context)
if (parentFragment is BackPressListener) {
listener = parentFragment as BackPressListener
} else if (activity is BackPressListener) {
listener = activity as BackPressListener
} else {
if (dispatchBackPress)
throw IllegalStateException("Activity or parent fragment must implement BackPressListener to dispatch back presses.")
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
with(dialog) {
window.setBackgroundDrawable(ColorDrawable(0)) // Remove the frame
setCanceledOnTouchOutside(false)
}
return MaterialProgressBar(context)
}
override fun onCancel(dialog: DialogInterface?) {
listener?.onProgressBackPressed()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment