Created
October 29, 2019 07:02
-
-
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.
This file contains hidden or 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
/** | |
* 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() |
This file contains hidden or 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
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still useful.
Thumbs up for solution.
Thanks.