Skip to content

Instantly share code, notes, and snippets.

@pksokolowski
Created October 29, 2019 07:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pksokolowski/f31eb38fe230f0bb62c17e235de8bd1b to your computer and use it in GitHub Desktop.
Save pksokolowski/f31eb38fe230f0bb62c17e235de8bd1b to your computer and use it in GitHub Desktop.
Android: fix for DialogFragment's size issues when a recyclerView lives inside it. This extension method allows you to hardcode in the desired dimensions of the dialog fragment as percentages of the available screen real estate.
/**
* For dialogFragments with size issues, say due to a recyclerView inside them,
* use this extension function in their onResume() callback method.
* Provide the desired percentages of with and height of the available screen space
* which you desire the dialog to occupy, and enjoy your day.
*/
fun DialogFragment.fixDialogSize(widthPercent: Double, heightPercent: Double){
val size = Point()
dialog?.window?.apply {
windowManager.defaultDisplay.getSize(size)
setLayout(widthPercent of size.x, heightPercent of size.y)
setGravity(Gravity.CENTER)
}
}
private infix fun Double.of(value: Int) = (this * value).toInt()
class HelpDialogFragment : androidx.fragment.app.DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_help, null)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recycler.adapter = HelpAdapter().apply {
setItems(getRules())
}
recycler.layoutManager = LinearLayoutManager(requireActivity())
}
override fun onResume() {
super.onResume()
fixDialogSize(0.98, 0.9)
}
}
@SerggioC
Copy link

Still useful.
Thumbs up for solution.
Thanks.

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