OpenEUICC/app/src/main/java/im/angry/openeuicc/util/UiUtils.kt
2022-05-02 21:30:13 -04:00

38 lines
1.3 KiB
Kotlin

package im.angry.openeuicc.util
import android.app.Activity
import android.content.res.Resources
import android.graphics.Rect
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import im.angry.openeuicc.OpenEuiccApplication
val Activity.openEuiccApplication: OpenEuiccApplication
get() = application as OpenEuiccApplication
val Fragment.openEuiccApplication: OpenEuiccApplication
get() = requireActivity().openEuiccApplication
// Source: <https://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height>
/**
* Call this method (in onActivityCreated or later) to set
* the width of the dialog to a percentage of the current
* screen width.
*/
fun DialogFragment.setWidthPercent(percentage: Int) {
val percent = percentage.toFloat() / 100
val dm = Resources.getSystem().displayMetrics
val rect = dm.run { Rect(0, 0, widthPixels, heightPixels) }
val percentWidth = rect.width() * percent
dialog?.window?.setLayout(percentWidth.toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
}
/**
* Call this method (in onActivityCreated or later)
* to make the dialog near-full screen.
*/
fun DialogFragment.setFullScreen() {
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}