Last active
April 10, 2020 14:17
-
-
Save diegohkd/3b9fa7a92e9e1b665d3421c074fcc76b to your computer and use it in GitHub Desktop.
DialogFragment with toolbar and a menu in it
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 MyDialogFragment : DialogFragment() { | |
... | |
private fun setupToolbar() { | |
setupToolbarCloseButton(toolbar, R.string.my_dialog_title) | |
setupToolbarMenu() | |
} | |
private fun setupToolbarMenu() { | |
toolbar.inflateMenu(R.menu.menu_my_dialog) | |
toolbar.setOnMenuItemClickListener { menuItem -> | |
if (menuItem.itemId == R.id.share) { | |
Toast.makeText(requireContext(), "Not implemented", Toast.LENGTH_LONG).show() | |
} | |
true | |
} | |
} | |
... | |
} |
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
fun DialogFragment.setupToolbarCloseButton(toolbar: Toolbar, @StringRes titleRes: Int?) { | |
titleRes?.let(toolbar::setTitle) | |
toolbar.navigationIcon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_close) | |
toolbar.setNavigationOnClickListener { | |
dismiss() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment