Created
June 6, 2020 05:41
-
-
Save seanghay/7e5dfab6bdf1f23d7cf394cfa7d067be to your computer and use it in GitHub Desktop.
An alert dialog fragment that supports new material background.
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
/** | |
* Designed and developed by Seanghay Yath (@seanghay) | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.seanghay.materialdialog | |
import android.annotation.SuppressLint | |
import android.app.Activity | |
import android.app.Dialog | |
import android.os.Bundle | |
import androidx.annotation.NonNull | |
import androidx.appcompat.app.AlertDialog | |
import androidx.appcompat.app.AppCompatDialogFragment | |
import androidx.fragment.app.DialogFragment | |
import com.google.android.material.dialog.MaterialAlertDialogBuilder | |
/** | |
* An alert dialog fragment that supports new material background. | |
* It uses {@link MaterialAlertDialogBuilder}. | |
*/ | |
open class MaterialDialogFragment: AppCompatDialogFragment() { | |
/** | |
* Create a MaterialAlertDialog | |
* @return AlertDialog | |
*/ | |
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog { | |
val builder = MaterialAlertDialogBuilder(context, theme) | |
return builder.create() | |
} | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
// We don't have to do anything if it doesn't show | |
if (!showsDialog) { | |
return | |
} | |
// Disable showsDialog so that the code of super class is not executed | |
showsDialog = false | |
// We have to call super | |
super.onActivityCreated(savedInstanceState) | |
val v = view | |
if (v != null) { | |
if (v.parent != null) { | |
throw IllegalStateException("DialogFragment can not be attached to a container view") | |
} | |
if (dialog is AlertDialog) { | |
// If it's an alert dialog set view instead of contentView | |
(dialog as AlertDialog).setView(v) | |
} else { | |
// Fallback to the old one | |
dialog?.setContentView(v) | |
} | |
} | |
val activity = activity | |
if (activity != null) { | |
dialog?.setOwnerActivity(activity) | |
} | |
dialog?.setCancelable(isCancelable) | |
dialog?.setOnCancelListener(this) | |
dialog?.setOnDismissListener(this) | |
if (savedInstanceState != null) { | |
// Restore states of the dialog | |
val dialogState = savedInstanceState.getBundle("android:savedDialogState") | |
if (dialogState != null) { | |
dialog?.onRestoreInstanceState(dialogState) | |
} | |
} | |
showsDialog = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment