├── .gitignore ├── LICENSE ├── README.md ├── art ├── ss1.png ├── ss2.png └── ss3.png ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── kennyc │ │ └── bottomsheet │ │ ├── BottomSheetListener.kt │ │ ├── BottomSheetMenuDialogFragment.kt │ │ ├── adapters │ │ ├── GridAdapter.kt │ │ └── ViewHolder.kt │ │ └── menu │ │ ├── BottomSheetMenu.kt │ │ ├── BottomSheetMenuItem.kt │ │ └── BottomSheetViewModel.kt │ └── res │ ├── drawable │ └── ic_baseline_close_24.xml │ ├── layout │ ├── bottom_sheet_grid_item.xml │ ├── bottom_sheet_list_item.xml │ └── bottom_sheet_menu.xml │ ├── values-sw600dp │ └── bools.xml │ └── values │ ├── attrs.xml │ ├── bools.xml │ ├── dimens.xml │ └── styles.xml ├── sample ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── kennyc │ │ └── bottomsheetsample │ │ └── MainActivity.kt │ └── res │ ├── drawable-hdpi │ ├── ic_close_grey_600_48dp.png │ ├── ic_cloud_upload_grey_600_24dp.png │ ├── ic_content_copy_grey_600_24dp.png │ ├── ic_content_copy_grey_600_48dp.png │ ├── ic_delete_grey_600_48dp.png │ ├── ic_edit_grey_600_48dp.png │ ├── ic_mail_grey_600_48dp.png │ ├── ic_print_grey_600_24dp.png │ ├── ic_save_grey_600_48dp.png │ └── ic_share_grey_600_24dp.png │ ├── drawable-mdpi │ ├── ic_close_grey_600_48dp.png │ ├── ic_cloud_upload_grey_600_24dp.png │ ├── ic_content_copy_grey_600_24dp.png │ ├── ic_content_copy_grey_600_48dp.png │ ├── ic_delete_grey_600_48dp.png │ ├── ic_edit_grey_600_48dp.png │ ├── ic_mail_grey_600_48dp.png │ ├── ic_print_grey_600_24dp.png │ ├── ic_save_grey_600_48dp.png │ └── ic_share_grey_600_24dp.png │ ├── drawable-xhdpi │ ├── ic_close_grey_600_48dp.png │ ├── ic_cloud_upload_grey_600_24dp.png │ ├── ic_content_copy_grey_600_24dp.png │ ├── ic_content_copy_grey_600_48dp.png │ ├── ic_delete_grey_600_48dp.png │ ├── ic_edit_grey_600_48dp.png │ ├── ic_mail_grey_600_48dp.png │ ├── ic_print_grey_600_24dp.png │ ├── ic_save_grey_600_48dp.png │ └── ic_share_grey_600_24dp.png │ ├── drawable-xxhdpi │ ├── ic_close_grey_600_48dp.png │ ├── ic_cloud_upload_grey_600_24dp.png │ ├── ic_content_copy_grey_600_24dp.png │ ├── ic_content_copy_grey_600_48dp.png │ ├── ic_delete_grey_600_48dp.png │ ├── ic_edit_grey_600_48dp.png │ ├── ic_mail_grey_600_48dp.png │ ├── ic_print_grey_600_24dp.png │ ├── ic_save_grey_600_48dp.png │ └── ic_share_grey_600_24dp.png │ ├── drawable-xxxhdpi │ ├── ic_close_grey_600_48dp.png │ ├── ic_cloud_upload_grey_600_24dp.png │ ├── ic_content_copy_grey_600_24dp.png │ ├── ic_content_copy_grey_600_48dp.png │ ├── ic_delete_grey_600_48dp.png │ ├── ic_edit_grey_600_48dp.png │ ├── ic_mail_grey_600_48dp.png │ ├── ic_print_grey_600_24dp.png │ ├── ic_save_grey_600_48dp.png │ └── ic_share_grey_600_24dp.png │ ├── layout │ └── activity_main.xml │ ├── menu │ ├── grid_sheet.xml │ ├── list_sheet.xml │ └── main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### Android ### 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | 29 | ### Intellij ### 30 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 31 | 32 | *.iml 33 | 34 | ## Directory-based project format: 35 | .idea/ 36 | # if you remove the above rule, at least ignore the following: 37 | 38 | # User-specific stuff: 39 | # .idea/workspace.xml 40 | # .idea/tasks.xml 41 | # .idea/dictionaries 42 | 43 | # Sensitive or high-churn files: 44 | # .idea/dataSources.ids 45 | # .idea/dataSources.xml 46 | # .idea/sqlDataSources.xml 47 | # .idea/dynamic.xml 48 | # .idea/uiDesigner.xml 49 | 50 | # Gradle: 51 | # .idea/gradle.xml 52 | # .idea/libraries 53 | 54 | # Mongo Explorer plugin: 55 | # .idea/mongoSettings.xml 56 | 57 | ## File-based project format: 58 | *.ipr 59 | *.iws 60 | 61 | ## Plugin-specific files: 62 | 63 | # IntelliJ 64 | out/ 65 | 66 | # mpeltonen/sbt-idea plugin 67 | .idea_modules/ 68 | 69 | # JIRA plugin 70 | atlassian-ide-plugin.xml 71 | 72 | # Crashlytics plugin (for Android Studio and IntelliJ) 73 | com_crashlytics_export_strings.xml 74 | 75 | # Ignore Gradle GUI config 76 | gradle-app.setting 77 | 78 | # Mobile Tools for Java (J2ME) 79 | .mtj.tmp/ 80 | 81 | # Package Files # 82 | *.war 83 | *.ear 84 | 85 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 86 | hs_err_pid* 87 | 88 | *.DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BottomSheetMenu 2 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-BottomSheet-green.svg?style=flat)](https://android-arsenal.com/details/1/2315) 3 | 4 | 5 | 6 | 7 | 8 | # Features 9 | - Both list and grid style 10 | - Light, Dark, and DayNight theme as well as custom themeing options 11 | - Material3 Theme support 12 | - XML style support 13 | - Tablet support 14 | - API 21+ 15 | - Kotlin support 16 | 17 | 18 | # Using BottomSheetMenu 19 | To get started using BottomSheetMenu, first you'll need to create a menu resource file with the defined actions. 20 | ```xml 21 | 22 | 23 | 24 | 28 | 29 | 33 | 34 | 38 | 39 | 43 | 44 | 45 | ``` 46 | 47 | Then create a BottomSheetMenuDialogFragment via the Builder class using either the Builder method calls for java 48 | or named arguments for Kotlin 49 | ```java 50 | new BottomSheetMenuDialogFragment.Builder(getActivity()) 51 | .setSheet(R.menu.bottom_sheet) 52 | .setTitle(R.string.options) 53 | .setListener(myListener) 54 | .setObject(myObject) 55 | .show(getSupportFragmentManager()); 56 | ``` 57 | 58 | ```kotilin 59 | BottomSheetMenuDialogFragment.Builder(context = this, 60 | sheet = R.menu.bottom_sheet, 61 | listener = myListener, 62 | title = R.string.options, 63 | `object` = myObject) 64 | .show(supportFragmentManager) 65 | ``` 66 | # Styling 67 | BottomSheetMenu comes with both a Light and Dark theme to accommodate most scenarios. However, if you want to customize itr more, you can create your own style and supply it to the builder. 68 |
Customizable attributes are: 69 | ```xml 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ``` 88 | 89 | Then create a style 90 | ```xml 91 | 96 | 97 | 100 | 101 | 105 | 106 | 110 | ``` 111 | Also note that each of these pre-defined styles also have a light and DayNight theme. They are named similary with a `.Light` or `DayNight` added to the end of the style name
112 | `@style/Theme.BottomSheetMenuDialog.Light` `@style/BottomSheetMenu.Title.TextAppearance.Light` etc... 113 | 114 | 115 | Then finally pass the style into the `Builder` object. 116 | ```java 117 | new BottomSheetMenuDialogFragment.Builder(getActivity(), R.style.MyBottomSheetStyle) 118 | .setSheet(R.menu.bottom_sheet) 119 | .setTitle(R.string.options) 120 | .setListener(myListener) 121 | .show(); 122 | ``` 123 | 124 | ```kotlin 125 | BottomSheetMenuDialogFragment.Builder(context = this, 126 | sheet = R.menu.bottom_sheet, 127 | title = R.string.options, 128 | listener = myListener, 129 | style = R.style.MyBottomSheetStyle) 130 | .show(supportFragmentManager) 131 | ``` 132 | 133 | # Callbacks 134 | BottomSheetMenu uses the [BottomSheetListener](https://github.com/Kennyc1012/BottomSheetMenu/blob/master/library/src/main/java/com/kennyc/bottomsheet/BottomSheetListener.kt) for callbacks 135 | ```kotlin 136 | /** 137 | * Called when the [BottomSheetMenuDialogFragment] is first displayed 138 | * 139 | * @param bottomSheet The [BottomSheetMenuDialogFragment] that was shown 140 | * @param object Optional [Object] to pass to the [BottomSheetMenuDialogFragment] 141 | */ 142 | fun onSheetShown(bottomSheet: BottomSheetMenuDialogFragment, `object`: Any?) 143 | 144 | /** 145 | * Called when an item is selected from the list/grid of the [BottomSheetMenuDialogFragment] 146 | * 147 | * @param bottomSheet The [BottomSheetMenuDialogFragment] that had an item selected 148 | * @param item The item that was selected 149 | * @param object Optional [Object] to pass to the [BottomSheetMenuDialogFragment] 150 | */ 151 | fun onSheetItemSelected(bottomSheet: BottomSheetMenuDialogFragment, item: MenuItem, `object`: Any?) 152 | 153 | /** 154 | * Called when the [BottomSheetMenuDialogFragment] has been dismissed 155 | * 156 | * @param bottomSheet The [BottomSheetMenuDialogFragment] that was dismissed 157 | * @param object Optional [Object] to pass to the [BottomSheetMenuDialogFragment] 158 | * @param dismissEvent How the [BottomSheetMenuDialogFragment] was dismissed. Possible values are:

159 | * * [.DISMISS_EVENT_SWIPE] 160 | * * [.DISMISS_EVENT_MANUAL] 161 | * * [.DISMISS_EVENT_ITEM_SELECTED] 162 | */ 163 | fun onSheetDismissed(bottomSheet: BottomSheetMenuDialogFragment, `object`: Any?, @DismissEvent dismissEvent: Int) 164 | ``` 165 | 166 | # Upgrading to 5.X 167 | - Removed various `createShareBottomSheet` methods 168 | - Targeting Android SDK 34 169 | - Targeting Kotlin 1.8.22 170 | 171 | # Upgrading to 4.X 172 | - Styles now extend Theme.Material3.* themes 173 | - An app's style should inherit from a MaterialComponent theme. Material3 themes are preferred but not required. 174 | - Removed `bottom_sheet_menu_selector` attribute 175 | - Removed various resources 176 | - Java 11 is now required to compile project 177 | - MinSdk is now 21, also targeting API 31 178 | 179 | # Upgrading to 3.X 180 | - `BottomSheet` has been renamed to `BottomSheetMenuDialogFragment` 181 | - Custom views and simple messages are no longer supported. Please use a [BottomSheetDialogFragment](https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetDialogFragment) and customize it from there 182 | - Many of the theme attributes have been removed or renamed. See the Styling section above for current values 183 | - CollaspingView has been removed. 184 | - Migration to [AndroidX](https://developer.android.com/jetpack/androidx/) and [Google Material Components](https://github.com/material-components/material-components-android) 185 | - MinSdk is now 19, also targeting API 28 186 | 187 | # Upgrading From 1.x 188 | When upgrading to 2.x from a 1.x release, some changes will have to be made. 189 | - All of the builder methods for settings colors have been removed. All customzing should be done through themes. 190 | - The style attributes have been change to text appearances rather than colors. 191 | - The Builder constructor no longer takes a menu object. You will need to call ```setSheet(...)```. 192 | - The ```onSheetDismissed``` callback now takes an int as an argument for simple message support. 193 | - The gradle dependency has changed and needs to be updated. 194 | 195 | # Including in your project 196 | To include BottomSheet in your project, make the following changes to your build.gradle file 197 | 198 | ## Add repository 199 | ```groovy 200 | allprojects { 201 | repositories { 202 | ... 203 | maven { url "https://jitpack.io" } 204 | } 205 | } 206 | ``` 207 | ## Add dependency 208 | ```groovy 209 | dependencies { 210 | implementation "com.github.Kennyc1012:BottomSheetMenu:5.1.1" 211 | ``` 212 | 213 | # Contribution 214 | Pull requests are welcomed and encouraged. If you experience any bugs, please file an [issue](https://github.com/Kennyc1012/BottomSheet/issues) 215 | 216 | License 217 | ======= 218 | 219 | Copyright 2015 Kenny Campagna 220 | 221 | Licensed under the Apache License, Version 2.0 (the "License"); 222 | you may not use this file except in compliance with the License. 223 | You may obtain a copy of the License at 224 | 225 | http://www.apache.org/licenses/LICENSE-2.0 226 | 227 | Unless required by applicable law or agreed to in writing, software 228 | distributed under the License is distributed on an "AS IS" BASIS, 229 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 230 | See the License for the specific language governing permissions and 231 | limitations under the License. 232 | -------------------------------------------------------------------------------- /art/ss1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kennyc1012/BottomSheetMenu/0a2213a2907d0a89018ee0cb12df1d72840b53bf/art/ss1.png -------------------------------------------------------------------------------- /art/ss2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kennyc1012/BottomSheetMenu/0a2213a2907d0a89018ee0cb12df1d72840b53bf/art/ss2.png -------------------------------------------------------------------------------- /art/ss3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kennyc1012/BottomSheetMenu/0a2213a2907d0a89018ee0cb12df1d72840b53bf/art/ss3.png -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | ext.kotlin_version = '1.9.21' 4 | ext.sdk_version = 34 5 | 6 | repositories { 7 | jcenter() 8 | google() 9 | } 10 | 11 | dependencies { 12 | classpath 'com.android.tools.build:gradle:8.3.2' 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | jcenter() 22 | google() 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | android.useAndroidX=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kennyc1012/BottomSheetMenu/0a2213a2907d0a89018ee0cb12df1d72840b53bf/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Mar 14 07:55:52 EDT 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | yes | $ANDROID_HOME/tools/bin/sdkmanager "build-tools;28.0.3" 3 | 4 | ############################################################################## 5 | ## 6 | ## Gradle start up script for UN*X 7 | ## 8 | ############################################################################## 9 | 10 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 11 | DEFAULT_JVM_OPTS="" 12 | 13 | APP_NAME="Gradle" 14 | APP_BASE_NAME=`basename "$0"` 15 | 16 | # Use the maximum available, or set MAX_FD != -1 to use that value. 17 | MAX_FD="maximum" 18 | 19 | warn ( ) { 20 | echo "$*" 21 | } 22 | 23 | die ( ) { 24 | echo 25 | echo "$*" 26 | echo 27 | exit 1 28 | } 29 | 30 | # OS specific support (must be 'true' or 'false'). 31 | cygwin=false 32 | msys=false 33 | darwin=false 34 | case "`uname`" in 35 | CYGWIN* ) 36 | cygwin=true 37 | ;; 38 | Darwin* ) 39 | darwin=true 40 | ;; 41 | MINGW* ) 42 | msys=true 43 | ;; 44 | esac 45 | 46 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 47 | if $cygwin ; then 48 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 49 | fi 50 | 51 | # Attempt to set APP_HOME 52 | # Resolve links: $0 may be a link 53 | PRG="$0" 54 | # Need this for relative symlinks. 55 | while [ -h "$PRG" ] ; do 56 | ls=`ls -ld "$PRG"` 57 | link=`expr "$ls" : '.*-> \(.*\)$'` 58 | if expr "$link" : '/.*' > /dev/null; then 59 | PRG="$link" 60 | else 61 | PRG=`dirname "$PRG"`"/$link" 62 | fi 63 | done 64 | SAVED="`pwd`" 65 | cd "`dirname \"$PRG\"`/" >&- 66 | APP_HOME="`pwd -P`" 67 | cd "$SAVED" >&- 68 | 69 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 70 | 71 | # Determine the Java command to use to start the JVM. 72 | if [ -n "$JAVA_HOME" ] ; then 73 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 74 | # IBM's JDK on AIX uses strange locations for the executables 75 | JAVACMD="$JAVA_HOME/jre/sh/java" 76 | else 77 | JAVACMD="$JAVA_HOME/bin/java" 78 | fi 79 | if [ ! -x "$JAVACMD" ] ; then 80 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 81 | 82 | Please set the JAVA_HOME variable in your environment to match the 83 | location of your Java installation." 84 | fi 85 | else 86 | JAVACMD="java" 87 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 88 | 89 | Please set the JAVA_HOME variable in your environment to match the 90 | location of your Java installation." 91 | fi 92 | 93 | # Increase the maximum file descriptors if we can. 94 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 95 | MAX_FD_LIMIT=`ulimit -H -n` 96 | if [ $? -eq 0 ] ; then 97 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 98 | MAX_FD="$MAX_FD_LIMIT" 99 | fi 100 | ulimit -n $MAX_FD 101 | if [ $? -ne 0 ] ; then 102 | warn "Could not set maximum file descriptor limit: $MAX_FD" 103 | fi 104 | else 105 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 106 | fi 107 | fi 108 | 109 | # For Darwin, add options to specify how the application appears in the dock 110 | if $darwin; then 111 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 112 | fi 113 | 114 | # For Cygwin, switch paths to Windows format before running java 115 | if $cygwin ; then 116 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 117 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 118 | 119 | # We build the pattern for arguments to be converted via cygpath 120 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 121 | SEP="" 122 | for dir in $ROOTDIRSRAW ; do 123 | ROOTDIRS="$ROOTDIRS$SEP$dir" 124 | SEP="|" 125 | done 126 | OURCYGPATTERN="(^($ROOTDIRS))" 127 | # Add a user-defined pattern to the cygpath arguments 128 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 129 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 130 | fi 131 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 132 | i=0 133 | for arg in "$@" ; do 134 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 135 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 136 | 137 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 138 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 139 | else 140 | eval `echo args$i`="\"$arg\"" 141 | fi 142 | i=$((i+1)) 143 | done 144 | case $i in 145 | (0) set -- ;; 146 | (1) set -- "$args0" ;; 147 | (2) set -- "$args0" "$args1" ;; 148 | (3) set -- "$args0" "$args1" "$args2" ;; 149 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 150 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 151 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 152 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 153 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 154 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 155 | esac 156 | fi 157 | 158 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 159 | function splitJvmOpts() { 160 | JVM_OPTS=("$@") 161 | } 162 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 163 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 164 | 165 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 166 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdk sdk_version 6 | namespace "com.kennyc.bottomsheet" 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion sdk_version 11 | } 12 | 13 | lintOptions { 14 | abortOnError false 15 | } 16 | 17 | compileOptions { 18 | sourceCompatibility JavaVersion.VERSION_17 19 | targetCompatibility JavaVersion.VERSION_17 20 | } 21 | 22 | kotlinOptions { 23 | jvmTarget = "17" 24 | } 25 | 26 | packagingOptions { 27 | exclude 'META-INF/library_release.kotlin_module' 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | implementation 'androidx.annotation:annotation:1.8.0' 34 | implementation 'com.google.android.material:material:1.12.0' 35 | } 36 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/BottomSheetListener.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet 2 | 3 | import android.view.MenuItem 4 | import androidx.annotation.IntDef 5 | 6 | interface BottomSheetListener { 7 | 8 | @IntDef(DISMISS_EVENT_MANUAL, DISMISS_EVENT_SWIPE) 9 | annotation class DismissEvent 10 | 11 | /** 12 | * Called when the [BottomSheetMenuDialogFragment] is first displayed 13 | * 14 | * @param bottomSheet The [BottomSheetMenuDialogFragment] that was shown 15 | * @param object Optional [Object] to pass to the [BottomSheetMenuDialogFragment] 16 | */ 17 | fun onSheetShown(bottomSheet: BottomSheetMenuDialogFragment, `object`: Any?) 18 | 19 | /** 20 | * Called when an item is selected from the list/grid of the [BottomSheetMenuDialogFragment] 21 | * 22 | * @param bottomSheet The [BottomSheetMenuDialogFragment] that had an item selected 23 | * @param item The item that was selected 24 | * @param object Optional [Object] to pass to the [BottomSheetMenuDialogFragment] 25 | */ 26 | fun onSheetItemSelected(bottomSheet: BottomSheetMenuDialogFragment, item: MenuItem, `object`: Any?) 27 | 28 | /** 29 | * Called when the [BottomSheetMenuDialogFragment] has been dismissed 30 | * 31 | * @param bottomSheet The [BottomSheetMenuDialogFragment] that was dismissed 32 | * @param object Optional [Object] to pass to the [BottomSheetMenuDialogFragment] 33 | * @param dismissEvent How the [BottomSheetMenuDialogFragment] was dismissed. Possible values are:

34 | * * [.DISMISS_EVENT_SWIPE] 35 | * * [.DISMISS_EVENT_MANUAL] 36 | * * [.DISMISS_EVENT_ITEM_SELECTED] 37 | */ 38 | fun onSheetDismissed(bottomSheet: BottomSheetMenuDialogFragment, `object`: Any?, @DismissEvent dismissEvent: Int) 39 | 40 | companion object { 41 | const val DISMISS_EVENT_SWIPE = -4 42 | 43 | const val DISMISS_EVENT_MANUAL = -5 44 | 45 | const val DISMISS_EVENT_ITEM_SELECTED = -6 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/BottomSheetMenuDialogFragment.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet 2 | 3 | import android.app.Dialog 4 | import android.content.Context 5 | import android.content.DialogInterface 6 | import android.os.Bundle 7 | import android.text.TextUtils 8 | import android.util.Log 9 | import android.view.* 10 | import android.widget.* 11 | import androidx.annotation.IntegerRes 12 | import androidx.annotation.MenuRes 13 | import androidx.annotation.StringRes 14 | import androidx.annotation.StyleRes 15 | import androidx.coordinatorlayout.widget.CoordinatorLayout 16 | import androidx.fragment.app.FragmentManager 17 | import androidx.lifecycle.ViewModelProvider 18 | import com.google.android.material.bottomsheet.BottomSheetBehavior 19 | import com.google.android.material.bottomsheet.BottomSheetDialog 20 | import com.google.android.material.bottomsheet.BottomSheetDialogFragment 21 | import com.kennyc.bottomsheet.adapters.GridAdapter 22 | import com.kennyc.bottomsheet.menu.BottomSheetMenu 23 | import com.kennyc.bottomsheet.menu.BottomSheetMenuItem 24 | import com.kennyc.bottomsheet.menu.BottomSheetViewModel 25 | import java.util.* 26 | 27 | private const val TAG = "BottomSheetMenu" 28 | 29 | class BottomSheetMenuDialogFragment() : BottomSheetDialogFragment(), 30 | AdapterView.OnItemClickListener { 31 | 32 | private constructor(builder: Builder) : this() { 33 | setTempBuilder(builder) 34 | } 35 | 36 | private var tempBuilder: Builder? = null 37 | 38 | private lateinit var container: LinearLayout 39 | 40 | private lateinit var closeContainer: LinearLayout 41 | 42 | private lateinit var adapter: GridAdapter 43 | 44 | private var dismissEvent = BottomSheetListener.DISMISS_EVENT_MANUAL 45 | 46 | private lateinit var viewModel: BottomSheetViewModel 47 | 48 | override fun onCreate(savedInstanceState: Bundle?) { 49 | super.onCreate(savedInstanceState) 50 | viewModel = ViewModelProvider(this)[BottomSheetViewModel::class.java].apply { 51 | if (builder == null) { 52 | builder = tempBuilder 53 | } 54 | } 55 | 56 | tempBuilder = null 57 | } 58 | 59 | override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 60 | return BottomSheetDialog(requireActivity(), viewModel.style).apply { 61 | setOnShowListener(DialogInterface.OnShowListener { 62 | 63 | if (container.parent == null) return@OnShowListener 64 | val params = 65 | (container.parent as View).layoutParams as CoordinatorLayout.LayoutParams 66 | val behavior = params.behavior 67 | 68 | // Should always be the case 69 | if (behavior is BottomSheetBehavior<*>) { 70 | if (viewModel.autoExpand) behavior.state = BottomSheetBehavior.STATE_EXPANDED 71 | 72 | behavior.addBottomSheetCallback(object : 73 | BottomSheetBehavior.BottomSheetCallback() { 74 | override fun onStateChanged(bottomSheet: View, state: Int) { 75 | if (state == BottomSheetBehavior.STATE_HIDDEN) { 76 | dismissEvent = BottomSheetListener.DISMISS_EVENT_SWIPE 77 | dismiss() 78 | } 79 | } 80 | 81 | override fun onSlide(bottomSheet: View, slideOffSet: Float) { 82 | closeContainer.alpha = if (slideOffSet > 0) slideOffSet else 0.0f 83 | 84 | } 85 | }) 86 | } 87 | }) 88 | } 89 | } 90 | 91 | override fun onCreateView( 92 | inflater: LayoutInflater, 93 | container: ViewGroup?, 94 | savedInstanceState: Bundle? 95 | ): View? { 96 | return inflater.inflate(R.layout.bottom_sheet_menu, container, false) 97 | } 98 | 99 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 100 | super.onViewCreated(view, savedInstanceState) 101 | if (viewModel.builder == null) { 102 | Log.e(TAG, "Builder object is null, dismissing dialog.") 103 | dismiss() 104 | return 105 | } 106 | 107 | container = view.findViewById(R.id.bottom_sheet_container) 108 | val title = container.findViewById(R.id.bottom_sheet_title) 109 | val gridView = container.findViewById(R.id.bottom_sheet_grid) 110 | closeContainer = container.findViewById(R.id.bottom_sheet_close_container) 111 | val closeTitle = closeContainer.findViewById(R.id.bottom_sheet_close_title) 112 | initUi(title, closeTitle, gridView) 113 | 114 | require(viewModel.menuItems.isNotEmpty()) { "No items were passed to the builder" } 115 | 116 | adapter = GridAdapter( 117 | ContextThemeWrapper(requireActivity(), viewModel.style), 118 | viewModel.menuItems, 119 | viewModel.isGrid 120 | ) 121 | 122 | gridView.onItemClickListener = this 123 | gridView.adapter = adapter 124 | viewModel.listener?.onSheetShown(this, viewModel.`object`) 125 | this.isCancelable = viewModel.cancelable 126 | 127 | closeContainer.findViewById(R.id.bottom_sheet_close).setOnClickListener { 128 | dismissEvent = BottomSheetListener.DISMISS_EVENT_MANUAL 129 | dismiss() 130 | } 131 | } 132 | 133 | private fun initUi(title: TextView, closeTitle: TextView, gridView: GridView) { 134 | val hasTitle = !TextUtils.isEmpty(viewModel.title) 135 | 136 | if (hasTitle) { 137 | title.text = viewModel.title 138 | } else { 139 | title.visibility = View.GONE 140 | } 141 | 142 | if (!viewModel.isGrid) { 143 | val padding = resources.getDimensionPixelSize(R.dimen.bottom_sheet_menu_list_padding) 144 | gridView.setPadding(0, if (hasTitle) 0 else padding, 0, padding) 145 | } 146 | 147 | if (!TextUtils.isEmpty(viewModel.closeTitle)) { 148 | closeTitle.text = viewModel.closeTitle 149 | } else { 150 | closeTitle.visibility = View.GONE 151 | } 152 | 153 | gridView.numColumns = getNumberColumns() 154 | } 155 | 156 | private fun getNumberColumns(): Int { 157 | if (viewModel.columnCount > 0) return viewModel.columnCount 158 | val isTablet = resources.getBoolean(R.bool.bottom_sheet_menu_it_tablet) 159 | 160 | val numItems = viewModel.menuItems.size 161 | 162 | if (viewModel.isGrid) { 163 | // Show 4 columns if a tablet and the number of its is 4 or >=7 164 | return if ((numItems >= 7 || numItems == GRID_MAX_COLUMN) && isTablet) { 165 | GRID_MAX_COLUMN 166 | } else { 167 | GRID_MIN_COLUMNS 168 | } 169 | } 170 | 171 | return when (isTablet) { 172 | // If a tablet with more than 6 items are present, split them into 2 columns 173 | true -> if (numItems >= MIN_LIST_TABLET_ITEMS) 2 else 1 174 | // Regular phone, one column 175 | else -> 1 176 | } 177 | } 178 | 179 | private fun setTempBuilder(builder: Builder) { 180 | this.tempBuilder = builder 181 | } 182 | 183 | override fun onDismiss(dialog: DialogInterface) { 184 | viewModel.listener?.onSheetDismissed(this, viewModel.`object`, dismissEvent) 185 | super.onDismiss(dialog) 186 | } 187 | 188 | override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { 189 | dismissEvent = BottomSheetListener.DISMISS_EVENT_ITEM_SELECTED 190 | 191 | viewModel.listener?.let { 192 | val item = adapter.getItem(position) 193 | it.onSheetItemSelected(this, item, viewModel.`object`) 194 | dismiss() 195 | } 196 | } 197 | 198 | /** 199 | * Builder factory used for creating [BottomSheetMenuDialogFragment] 200 | */ 201 | class Builder @JvmOverloads constructor( 202 | private val context: Context, 203 | @StyleRes style: Int = R.style.Theme_BottomSheetMenuDialog_Light, 204 | columnCount: Int = -1, 205 | @MenuRes sheet: Int = -1, 206 | cancelable: Boolean = true, 207 | isGrid: Boolean = false, 208 | autoExpand: Boolean = false, 209 | menuItems: MutableList = mutableListOf(), 210 | title: String? = null, 211 | closeTitle: String? = null, 212 | listener: BottomSheetListener? = null, 213 | `object`: Any? = null, 214 | idsToDisable: Array? = null 215 | ) { 216 | 217 | @StyleRes 218 | var style: Int = style; private set 219 | var columnCount: Int = columnCount; private set 220 | var title: String? = title; private set 221 | var cancelable: Boolean = cancelable; private set 222 | var isGrid: Boolean = isGrid; private set 223 | var autoExpand: Boolean = autoExpand; private set 224 | var menuItems: MutableList = menuItems; private set 225 | var listener: BottomSheetListener? = listener; private set 226 | var `object`: Any? = `object`; private set 227 | var closeTitle: String? = closeTitle; private set 228 | 229 | init { 230 | if (sheet != -1 && menuItems.isEmpty()) setSheet(sheet, idsToDisable) 231 | } 232 | 233 | /** 234 | * Sets the [BottomSheetMenuDialogFragment] to use a dark theme 235 | * 236 | * @return 237 | */ 238 | fun dark(): Builder { 239 | style = R.style.Theme_BottomSheetMenuDialog 240 | return this 241 | } 242 | 243 | /** 244 | * Sets the [BottomSheetMenuDialogFragment] to use the DayNight theme 245 | * 246 | * @return 247 | */ 248 | fun dayNight(): Builder { 249 | style = R.style.Theme_BottomSheetMenuDialog_DayNight 250 | return this 251 | } 252 | 253 | /** 254 | * Sets the style of the [BottomSheetMenuDialogFragment] 255 | * 256 | * @param style 257 | * @return 258 | */ 259 | fun setStyle(@StyleRes style: Int): Builder { 260 | this.style = style 261 | return this 262 | } 263 | 264 | /** 265 | * Sets the title of the [BottomSheetMenuDialogFragment] 266 | * 267 | * @param title String for the title 268 | * @return 269 | */ 270 | fun setTitle(title: String): Builder { 271 | this.title = title 272 | return this 273 | } 274 | 275 | /** 276 | * Sets the title of the [BottomSheetMenuDialogFragment] 277 | * 278 | * @param title String resource for the title 279 | * @return 280 | */ 281 | fun setTitle(@StringRes title: Int): Builder { 282 | return setTitle(context.getString(title)) 283 | } 284 | 285 | /** 286 | * Sets the [BottomSheetMenuDialogFragment] to use a grid for displaying options 287 | * 288 | * @return 289 | */ 290 | fun grid(): Builder { 291 | isGrid = true 292 | return this 293 | } 294 | 295 | /** 296 | * Sets whether the [BottomSheetMenuDialogFragment] is cancelable with the [BACK][KeyEvent.KEYCODE_BACK] key. 297 | * 298 | * @param cancelable If the dialog can be canceled 299 | * @return 300 | */ 301 | fun setCancelable(cancelable: Boolean): Builder { 302 | this.cancelable = cancelable 303 | return this 304 | } 305 | 306 | /** 307 | * Sets the [BottomSheetListener] to receive callbacks 308 | * 309 | * @param listener The [BottomSheetListener] to receive callbacks for 310 | * @return 311 | */ 312 | fun setListener(listener: BottomSheetListener): Builder { 313 | this.listener = listener 314 | return this 315 | } 316 | 317 | /** 318 | * Sets the menu resource to use for the [BottomSheetMenuDialogFragment] 319 | * 320 | * @param sheetItems The [BottomSheetListener] to receive callbacks for 321 | * @param idsToDisable Ids of any MenuItems to set disabled 322 | * @return 323 | */ 324 | fun setSheet(@MenuRes sheetItems: Int, idsToDisable: Array?): Builder { 325 | val menu = BottomSheetMenu(context) 326 | MenuInflater(context).inflate(sheetItems, menu) 327 | 328 | idsToDisable?.let { 329 | for (i in 0 until menu.size()) { 330 | val item = menu.getItem(i) 331 | if (it.contains(item.itemId)) item.isEnabled = false 332 | } 333 | } 334 | return setMenu(menu) 335 | } 336 | 337 | /** 338 | * Sets the menu resource to use for the [BottomSheetMenuDialogFragment] 339 | * 340 | * @param sheetItems The [BottomSheetListener] to receive callbacks for 341 | * @return 342 | */ 343 | fun setSheet(@MenuRes sheetItems: Int): Builder = setSheet(sheetItems, null) 344 | 345 | /** 346 | * Sets the menu to use for the [BottomSheetMenuDialogFragment] 347 | * 348 | * @param menu 349 | * @return 350 | */ 351 | fun setMenu(menu: Menu): Builder { 352 | val items = ArrayList(menu.size()) 353 | 354 | for (i in 0 until menu.size()) { 355 | items.add(menu.getItem(i)) 356 | } 357 | 358 | return setMenuItems(items) 359 | 360 | return this 361 | } 362 | 363 | /** 364 | * Adds the [List] of menu items to use for the [BottomSheetMenuDialogFragment] 365 | * 366 | * @param menuItems 367 | * @return 368 | */ 369 | fun setMenuItems(menuItems: List): Builder { 370 | this.menuItems.addAll(menuItems) 371 | return this 372 | } 373 | 374 | /** 375 | * Adds a [MenuItem] to the [BottomSheetMenuDialogFragment]. For creating a [MenuItem], see [BottomSheetMenuItem] 376 | * 377 | * @param item 378 | * @return 379 | */ 380 | fun addMenuItem(item: MenuItem): Builder { 381 | menuItems.add(item) 382 | return this 383 | } 384 | 385 | /** 386 | * Sets the number of columns that will be shown when set to a grid style 387 | * 388 | * @param columnCount Number of columns to show 389 | * @return 390 | */ 391 | fun setColumnCount(columnCount: Int): Builder { 392 | this.columnCount = columnCount 393 | return this 394 | } 395 | 396 | /** 397 | * Sets the number of columns that will be shown when set to a grid style 398 | * 399 | * @param columnCount Integer resource containing number of columns to show 400 | * @return 401 | */ 402 | fun setColumnCountResource(@IntegerRes columnCount: Int): Builder { 403 | return setColumnCount(context.resources.getInteger(columnCount)) 404 | } 405 | 406 | /** 407 | * Sets the [Object] to be passed with the [BottomSheetMenuDialogFragment] 408 | * 409 | * @param object Optional [Object] 410 | * @return 411 | */ 412 | fun `object`(`object`: Any?): Builder { 413 | this.`object` = `object` 414 | return this 415 | } 416 | 417 | /** 418 | * Sets if the [BottomSheetMenuDialogFragment] should auto expand when opened. Default value is true 419 | */ 420 | fun setAutoExpand(autoExpand: Boolean): Builder { 421 | this.autoExpand = autoExpand 422 | return this 423 | } 424 | 425 | /** 426 | * Sets the close title of the [BottomSheetMenuDialogFragment] 427 | * 428 | * @param closeTitle The text to be used for the close title 429 | */ 430 | fun setCloseTitle(closeTitle: String): Builder { 431 | this.closeTitle = closeTitle 432 | return this 433 | } 434 | 435 | /** 436 | * Sets the close title of the [BottomSheetMenuDialogFragment] 437 | * 438 | * @param closeTitle The text resource to be used for the close title 439 | */ 440 | fun setCloseTitle(@StringRes closeTitle: Int): Builder { 441 | return setCloseTitle(context.getString(closeTitle)) 442 | } 443 | 444 | /** 445 | * Creates the [BottomSheetMenuDialogFragment] but does not show it. 446 | * 447 | * @return 448 | */ 449 | fun create(): BottomSheetMenuDialogFragment { 450 | return BottomSheetMenuDialogFragment(this) 451 | } 452 | 453 | /** 454 | * Creates the [BottomSheetMenuDialogFragment] and shows it. 455 | * 456 | * @param manager @link FragmentManager} the [BottomSheetMenuDialogFragment] will be added to 457 | * @param tag Optional tag for the [BottomSheetDialogFragment] 458 | */ 459 | @JvmOverloads 460 | fun show(manager: FragmentManager, tag: String? = null) { 461 | create().show(manager, tag) 462 | } 463 | } 464 | } 465 | 466 | private const val MIN_LIST_TABLET_ITEMS = 6 467 | 468 | private const val GRID_MIN_COLUMNS = 3 469 | 470 | private const val GRID_MAX_COLUMN = 4 471 | -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/adapters/GridAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet.adapters 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.MenuItem 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import android.widget.BaseAdapter 9 | import androidx.annotation.RestrictTo 10 | import com.kennyc.bottomsheet.R 11 | 12 | @RestrictTo(RestrictTo.Scope.LIBRARY) 13 | class GridAdapter(context: Context, 14 | private val items: List, 15 | private val isGrid: Boolean) : BaseAdapter() { 16 | 17 | private val inflater: LayoutInflater = LayoutInflater.from(context) 18 | 19 | override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { 20 | val item = getItem(position) 21 | val menuIcon = item.icon 22 | 23 | return when (convertView) { 24 | null -> { 25 | val itemView = if (isGrid) R.layout.bottom_sheet_grid_item else R.layout.bottom_sheet_list_item 26 | ViewHolder(inflater.inflate(itemView, parent, false)).apply { 27 | view.tag = this 28 | } 29 | } 30 | else -> { 31 | convertView.tag as ViewHolder 32 | } 33 | }.apply { 34 | icon.setImageDrawable(menuIcon) 35 | icon.visibility = if (menuIcon != null) View.VISIBLE else View.GONE 36 | title.text = item.title 37 | title.isEnabled = item.isEnabled 38 | }.view 39 | } 40 | 41 | override fun getItem(position: Int): MenuItem = items[position] 42 | 43 | override fun getItemId(position: Int): Long = getItem(position).itemId.toLong() 44 | 45 | override fun getCount(): Int = items.size 46 | 47 | override fun areAllItemsEnabled(): Boolean = false 48 | 49 | override fun isEnabled(position: Int): Boolean = getItem(position).isEnabled 50 | } -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/adapters/ViewHolder.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet.adapters 2 | 3 | import android.view.View 4 | import android.widget.ImageView 5 | import android.widget.TextView 6 | import com.kennyc.bottomsheet.R 7 | 8 | internal class ViewHolder(val view: View) { 9 | val title: TextView = view.findViewById(R.id.title) 10 | 11 | val icon: ImageView = view.findViewById(R.id.icon) 12 | } -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/menu/BottomSheetMenu.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet.menu 2 | 3 | import android.content.ComponentName 4 | import android.content.Context 5 | import android.content.Intent 6 | import android.graphics.drawable.Drawable 7 | import android.view.KeyEvent 8 | import android.view.Menu 9 | import android.view.MenuItem 10 | import android.view.SubMenu 11 | import androidx.annotation.DrawableRes 12 | import androidx.annotation.StringRes 13 | import androidx.core.content.res.ResourcesCompat 14 | 15 | /* 16 | * Copyright (C) 2010 The Android Open Source Project 17 | * 18 | * Licensed under the Apache License, Version 2.0 (the "License"); 19 | * you may not use this file except in compliance with the License. 20 | * You may obtain a copy of the License at 21 | * 22 | * http://www.apache.org/licenses/LICENSE-2.0 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, 26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 | * See the License for the specific language governing permissions and 28 | * limitations under the License. 29 | */ 30 | class BottomSheetMenu(private val context: Context) : Menu { 31 | 32 | private var isQwerty: Boolean = false 33 | 34 | private val items: ArrayList = ArrayList() 35 | 36 | override fun add(title: CharSequence): MenuItem { 37 | return add(0, 0, 0, title) 38 | } 39 | 40 | override fun add(titleRes: Int): MenuItem { 41 | return add(0, 0, 0, titleRes) 42 | } 43 | 44 | override fun add(groupId: Int, itemId: Int, order: Int, titleRes: Int): MenuItem { 45 | return add(groupId, itemId, order, context.resources.getString(titleRes)) 46 | } 47 | 48 | override fun add(groupId: Int, itemId: Int, order: Int, title: CharSequence): MenuItem { 49 | val item = BottomSheetMenuItem(context, groupId, itemId, 0, order, title) 50 | // TODO Order is ignored here. 51 | items.add(item) 52 | return item 53 | } 54 | 55 | override fun addIntentOptions( 56 | groupId: Int, itemId: Int, order: Int, 57 | caller: ComponentName, specifics: Array, intent: Intent, flags: Int, 58 | outSpecificItems: Array? 59 | ): Int { 60 | val pm = context.packageManager 61 | val lri = pm.queryIntentActivityOptions(caller, specifics, intent, 0) 62 | val size = lri.size 63 | 64 | if (flags and Menu.FLAG_APPEND_TO_GROUP == 0) { 65 | removeGroup(groupId) 66 | } 67 | 68 | for (i in 0 until size) { 69 | val ri = lri[i] 70 | val rintent = Intent( 71 | if (ri.specificIndex < 0) intent else specifics[ri.specificIndex] 72 | ) 73 | rintent.component = ComponentName( 74 | ri.activityInfo.applicationInfo.packageName, 75 | ri.activityInfo.name 76 | ) 77 | val item = add(groupId, itemId, order, ri.loadLabel(pm)) 78 | .setIcon(ri.loadIcon(pm)) 79 | .setIntent(rintent) 80 | if (outSpecificItems != null && ri.specificIndex >= 0) { 81 | outSpecificItems[ri.specificIndex] = item 82 | } 83 | } 84 | 85 | return size 86 | } 87 | 88 | override fun addSubMenu(title: CharSequence): SubMenu? { 89 | throw UnsupportedOperationException("Not Supported") 90 | } 91 | 92 | override fun addSubMenu(titleRes: Int): SubMenu? { 93 | throw UnsupportedOperationException("Not Supported") 94 | } 95 | 96 | override fun addSubMenu( 97 | groupId: Int, itemId: Int, order: Int, 98 | title: CharSequence 99 | ): SubMenu? { 100 | throw UnsupportedOperationException("Not Supported") 101 | } 102 | 103 | override fun addSubMenu(groupId: Int, itemId: Int, order: Int, titleRes: Int): SubMenu? { 104 | throw UnsupportedOperationException("Not Supported") 105 | } 106 | 107 | override fun clear() { 108 | items.clear() 109 | } 110 | 111 | override fun close() {} 112 | 113 | private fun findItemIndex(id: Int): Int { 114 | val itemCount = items.size 115 | for (i in 0 until itemCount) { 116 | if (items[i].itemId == id) { 117 | return i 118 | } 119 | } 120 | 121 | return -1 122 | } 123 | 124 | override fun findItem(id: Int): MenuItem { 125 | return items[findItemIndex(id)] 126 | } 127 | 128 | override fun getItem(index: Int): MenuItem { 129 | return items[index] 130 | } 131 | 132 | override fun hasVisibleItems(): Boolean { 133 | val itemCount = items.size 134 | 135 | for (i in 0 until itemCount) { 136 | if (items[i].isVisible) { 137 | return true 138 | } 139 | } 140 | 141 | return false 142 | } 143 | 144 | private fun findItemWithShortcut(keyCode: Int, event: KeyEvent): BottomSheetMenuItem? { 145 | // TODO Make this smarter. 146 | val qwerty = isQwerty 147 | val itemCount = items.size 148 | 149 | for (i in 0 until itemCount) { 150 | val item = items[i] 151 | val shortcut = if (qwerty) 152 | item.alphabeticShortcut 153 | else 154 | item.numericShortcut 155 | if (keyCode == shortcut.toInt()) { 156 | return item 157 | } 158 | } 159 | return null 160 | } 161 | 162 | override fun isShortcutKey(keyCode: Int, event: KeyEvent): Boolean { 163 | return findItemWithShortcut(keyCode, event) != null 164 | } 165 | 166 | override fun performIdentifierAction(id: Int, flags: Int): Boolean { 167 | val index = findItemIndex(id) 168 | return if (index < 0) { 169 | false 170 | } else items[index].invoke() 171 | 172 | } 173 | 174 | override fun performShortcut(keyCode: Int, event: KeyEvent, flags: Int): Boolean { 175 | val item = findItemWithShortcut(keyCode, event) ?: return false 176 | 177 | return item.invoke() 178 | } 179 | 180 | override fun removeGroup(groupId: Int) { 181 | var itemCount = items.size 182 | var i = 0 183 | while (i < itemCount) { 184 | if (items[i].groupId == groupId) { 185 | items.removeAt(i) 186 | itemCount-- 187 | } else { 188 | i++ 189 | } 190 | } 191 | } 192 | 193 | override fun removeItem(id: Int) { 194 | items.removeAt(findItemIndex(id)) 195 | } 196 | 197 | override fun setGroupCheckable( 198 | group: Int, checkable: Boolean, 199 | exclusive: Boolean 200 | ) { 201 | val itemCount = items.size 202 | 203 | for (i in 0 until itemCount) { 204 | val item = items[i] 205 | if (item.groupId == group) { 206 | item.isCheckable = checkable 207 | item.setExclusiveCheckable(exclusive) 208 | } 209 | } 210 | } 211 | 212 | override fun setGroupEnabled(group: Int, enabled: Boolean) { 213 | val itemCount = items.size 214 | 215 | for (i in 0 until itemCount) { 216 | val item = items[i] 217 | if (item.groupId == group) { 218 | item.isEnabled = enabled 219 | } 220 | } 221 | } 222 | 223 | override fun setGroupVisible(group: Int, visible: Boolean) { 224 | val itemCount = items.size 225 | 226 | for (i in 0 until itemCount) { 227 | val item = items[i] 228 | if (item.groupId == group) { 229 | item.isVisible = visible 230 | } 231 | } 232 | } 233 | 234 | override fun setQwertyMode(isQwerty: Boolean) { 235 | this.isQwerty = isQwerty 236 | } 237 | 238 | override fun size(): Int { 239 | return items.size 240 | } 241 | 242 | class MenuItemBuilder( 243 | private val context: Context, 244 | id: Int, 245 | title: String = "NULL", 246 | icon: Drawable? = null, 247 | enabled: Boolean = true 248 | ) { 249 | 250 | var title: String = title; private set 251 | var icon: Drawable? = icon; private set 252 | var id: Int = id; private set 253 | var enabled: Boolean = enabled; private set 254 | 255 | fun setTitle(@StringRes title: Int): MenuItemBuilder = setTitle(context.getString(title)) 256 | 257 | fun setTitle(title: String): MenuItemBuilder { 258 | this.title = title 259 | return this 260 | } 261 | 262 | fun setIcon(icon: Drawable?): MenuItemBuilder { 263 | this.icon = icon 264 | return this 265 | } 266 | 267 | fun setIcon(@DrawableRes icon: Int): MenuItemBuilder = 268 | setIcon(ResourcesCompat.getDrawable(context.resources, icon, context.theme)) 269 | 270 | fun setEnabled(enabled:Boolean): MenuItemBuilder { 271 | this.enabled = enabled 272 | return this 273 | } 274 | 275 | fun build(): MenuItem = BottomSheetMenuItem(context, id, title, enabled, icon) 276 | } 277 | } -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/menu/BottomSheetMenuItem.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet.menu 2 | 3 | /* 4 | * Copyright (C) 2010 The Android Open Source Project 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import android.content.Context 20 | import android.content.Intent 21 | import android.graphics.drawable.Drawable 22 | import android.view.ActionProvider 23 | import android.view.ContextMenu.ContextMenuInfo 24 | import android.view.MenuItem 25 | import android.view.SubMenu 26 | import android.view.View 27 | import androidx.annotation.DrawableRes 28 | import androidx.core.content.res.ResourcesCompat 29 | 30 | internal class BottomSheetMenuItem 31 | /** 32 | * Creates a MenuItem 33 | * 34 | * @param context Context of the MenuItem 35 | * @param group Group id of the MenuItem 36 | * @param id Id of the MenuItem 37 | * @param categoryOrder Category order of the MenuItem 38 | * @param ordering Ordering of the MenuItem 39 | * @param title Title of the MenuItem 40 | */ 41 | (private val context: Context, 42 | private val group: Int, 43 | private val id: Int, 44 | private val categoryOrder: Int, 45 | private val ordering: Int, 46 | private var title: CharSequence?) : MenuItem { 47 | 48 | private var titleCondensed: CharSequence? = null 49 | 50 | private var mIntent: Intent? = null 51 | 52 | private var shortcutNumericChar: Char = ' ' 53 | 54 | private var shortcutAlphabeticChar: Char = ' ' 55 | 56 | private var iconDrawable: Drawable? = null 57 | 58 | private var iconResId = NO_ICON 59 | 60 | private var clickListener: MenuItem.OnMenuItemClickListener? = null 61 | 62 | private var flags = ENABLED 63 | 64 | /** 65 | * Creates a MenuItem 66 | * 67 | * @param context Context of the MenuItem 68 | * @param id Id of the MenuItem 69 | * @param title Title of the MenuItem 70 | * @param enabled If the item is enabled 71 | * @param icon Drawable of the MenuItem 72 | */ 73 | constructor(context: Context, id: Int, title: CharSequence, enabled: Boolean, icon: Drawable?) : this(context, 0, id, 0, 0, title) { 74 | setIcon(icon) 75 | isEnabled = enabled 76 | } 77 | 78 | override fun getAlphabeticShortcut(): Char { 79 | return shortcutAlphabeticChar 80 | } 81 | 82 | override fun getGroupId(): Int { 83 | return group 84 | } 85 | 86 | override fun getIcon(): Drawable? { 87 | return iconDrawable 88 | } 89 | 90 | override fun getIntent(): Intent? { 91 | return mIntent 92 | } 93 | 94 | override fun getItemId(): Int { 95 | return id 96 | } 97 | 98 | override fun getMenuInfo(): ContextMenuInfo? { 99 | return null 100 | } 101 | 102 | override fun getNumericShortcut(): Char { 103 | return shortcutNumericChar 104 | } 105 | 106 | override fun getOrder(): Int { 107 | return ordering 108 | } 109 | 110 | override fun getSubMenu(): SubMenu? { 111 | return null 112 | } 113 | 114 | override fun getTitle(): CharSequence? { 115 | return title 116 | } 117 | 118 | override fun getTitleCondensed(): CharSequence? { 119 | return if (titleCondensed != null) titleCondensed else title 120 | } 121 | 122 | override fun hasSubMenu(): Boolean { 123 | return false 124 | } 125 | 126 | override fun isCheckable(): Boolean { 127 | return flags and CHECKABLE != 0 128 | } 129 | 130 | override fun isChecked(): Boolean { 131 | return flags and CHECKED != 0 132 | } 133 | 134 | override fun isEnabled(): Boolean { 135 | return flags and ENABLED != 0 136 | } 137 | 138 | override fun isVisible(): Boolean { 139 | return flags and HIDDEN == 0 140 | } 141 | 142 | override fun setAlphabeticShortcut(alphaChar: Char): MenuItem { 143 | shortcutAlphabeticChar = alphaChar 144 | return this 145 | } 146 | 147 | override fun setCheckable(checkable: Boolean): MenuItem { 148 | flags = flags and CHECKABLE.inv() or if (checkable) CHECKABLE else 0 149 | return this 150 | } 151 | 152 | fun setExclusiveCheckable(exclusive: Boolean): BottomSheetMenuItem { 153 | flags = flags and EXCLUSIVE.inv() or if (exclusive) EXCLUSIVE else 0 154 | return this 155 | } 156 | 157 | override fun setChecked(checked: Boolean): MenuItem { 158 | flags = flags and CHECKED.inv() or if (checked) CHECKED else 0 159 | return this 160 | } 161 | 162 | override fun setEnabled(enabled: Boolean): MenuItem { 163 | flags = flags and ENABLED.inv() or if (enabled) ENABLED else 0 164 | return this 165 | } 166 | 167 | override fun setIcon(icon: Drawable?): MenuItem { 168 | iconDrawable = icon 169 | iconResId = NO_ICON 170 | return this 171 | } 172 | 173 | override fun setIcon(iconRes: Int): MenuItem { 174 | if (iconRes != NO_ICON) { 175 | iconResId = iconRes 176 | iconDrawable = ResourcesCompat.getDrawable(context.resources, iconResId, context.theme) 177 | } 178 | 179 | return this 180 | } 181 | 182 | override fun setIntent(intent: Intent?): MenuItem { 183 | mIntent = intent 184 | return this 185 | } 186 | 187 | override fun setNumericShortcut(numericChar: Char): MenuItem { 188 | shortcutNumericChar = numericChar 189 | return this 190 | } 191 | 192 | override fun setOnMenuItemClickListener(menuItemClickListener: MenuItem.OnMenuItemClickListener?): MenuItem { 193 | clickListener = menuItemClickListener 194 | return this 195 | } 196 | 197 | override fun setShortcut(numericChar: Char, alphaChar: Char): MenuItem { 198 | shortcutNumericChar = numericChar 199 | shortcutAlphabeticChar = alphaChar 200 | return this 201 | } 202 | 203 | override fun setTitle(title: CharSequence?): MenuItem { 204 | this.title = title 205 | return this 206 | } 207 | 208 | override fun setTitle(title: Int): MenuItem { 209 | this.title = context.resources.getString(title) 210 | return this 211 | } 212 | 213 | override fun setTitleCondensed(title: CharSequence?): MenuItem { 214 | titleCondensed = title 215 | return this 216 | } 217 | 218 | override fun setVisible(visible: Boolean): MenuItem { 219 | flags = flags and HIDDEN or if (visible) 0 else HIDDEN 220 | return this 221 | } 222 | 223 | operator fun invoke(): Boolean { 224 | if (clickListener != null && clickListener!!.onMenuItemClick(this)) { 225 | return true 226 | } 227 | 228 | if (mIntent != null) { 229 | context.startActivity(mIntent) 230 | return true 231 | } 232 | 233 | return false 234 | } 235 | 236 | override fun setShowAsAction(show: Int) { 237 | // Do nothing. ActionMenuItems always show as action buttons. 238 | } 239 | 240 | override fun setActionView(actionView: View?): MenuItem { 241 | throw UnsupportedOperationException() 242 | } 243 | 244 | override fun getActionView(): View? { 245 | return null 246 | } 247 | 248 | override fun setActionView(resId: Int): MenuItem { 249 | throw UnsupportedOperationException() 250 | } 251 | 252 | override fun getActionProvider(): ActionProvider? { 253 | return null 254 | } 255 | 256 | override fun setActionProvider(actionProvider: ActionProvider?): MenuItem { 257 | throw UnsupportedOperationException() 258 | } 259 | 260 | override fun setShowAsActionFlags(actionEnum: Int): MenuItem { 261 | setShowAsAction(actionEnum) 262 | return this 263 | } 264 | 265 | override fun expandActionView(): Boolean { 266 | return false 267 | } 268 | 269 | override fun collapseActionView(): Boolean { 270 | return false 271 | } 272 | 273 | override fun isActionViewExpanded(): Boolean { 274 | return false 275 | } 276 | 277 | override fun setOnActionExpandListener(listener: MenuItem.OnActionExpandListener?): MenuItem { 278 | // No need to save the listener; ActionMenuItem does not support collapsing items. 279 | return this 280 | } 281 | 282 | 283 | 284 | companion object { 285 | 286 | private const val NO_ICON = 0 287 | 288 | private const val CHECKABLE = 0x00000001 289 | 290 | private const val CHECKED = 0x00000002 291 | 292 | private const val EXCLUSIVE = 0x00000004 293 | 294 | private const val HIDDEN = 0x00000008 295 | 296 | private const val ENABLED = 0x00000010 297 | } 298 | } -------------------------------------------------------------------------------- /library/src/main/java/com/kennyc/bottomsheet/menu/BottomSheetViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheet.menu 2 | 3 | import android.view.MenuItem 4 | import androidx.lifecycle.ViewModel 5 | import com.kennyc.bottomsheet.BottomSheetMenuDialogFragment 6 | import com.kennyc.bottomsheet.R 7 | 8 | internal class BottomSheetViewModel : ViewModel() { 9 | 10 | var builder: BottomSheetMenuDialogFragment.Builder? = null 11 | val style get() = builder?.style ?: R.style.Theme_BottomSheetMenuDialog_Light 12 | val autoExpand get() = builder?.autoExpand ?: false 13 | val isGrid get() = builder?.isGrid ?: false 14 | val cancelable get() = builder?.cancelable ?: true 15 | val columnCount get() = builder?.columnCount ?: -1 16 | val menuItems: List get() = builder?.menuItems ?: emptyList() 17 | val listener get() = builder?.listener 18 | val `object` get() = builder?.`object` 19 | val title get() = builder?.title 20 | val closeTitle get() = builder?.closeTitle 21 | } -------------------------------------------------------------------------------- /library/src/main/res/drawable/ic_baseline_close_24.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /library/src/main/res/layout/bottom_sheet_grid_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 16 | 17 | 22 | 23 | -------------------------------------------------------------------------------- /library/src/main/res/layout/bottom_sheet_list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 22 | 23 | -------------------------------------------------------------------------------- /library/src/main/res/layout/bottom_sheet_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 19 | 20 | 28 | 29 | 35 | 36 | 37 | 42 | 43 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /library/src/main/res/values-sw600dp/bools.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /library/src/main/res/values/bools.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | false 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 8dp 6 | 24dp 7 | 48dp 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 21 | 37 | 38 | 54 | 55 | 60 | 61 | 71 | 72 | 79 | 80 | 88 | 89 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdk sdk_version 6 | namespace "com.kennyc.bottomsheetsample" 7 | 8 | defaultConfig { 9 | applicationId "com.kennyc.bottomsheetsample" 10 | minSdkVersion 21 11 | targetSdkVersion sdk_version 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | } 19 | } 20 | 21 | compileOptions { 22 | sourceCompatibility JavaVersion.VERSION_17 23 | targetCompatibility JavaVersion.VERSION_17 24 | } 25 | 26 | kotlinOptions { 27 | jvmTarget = "17" 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | implementation 'androidx.appcompat:appcompat:1.7.0' 34 | implementation 'com.google.android.material:material:1.12.0' 35 | implementation project(':library') 36 | } 37 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /sample/src/main/java/com/kennyc/bottomsheetsample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.kennyc.bottomsheetsample 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.util.Log 6 | import android.view.Menu 7 | import android.view.MenuItem 8 | import android.view.View 9 | import android.widget.Button 10 | import android.widget.Toast 11 | import androidx.appcompat.app.AppCompatActivity 12 | import com.kennyc.bottomsheet.BottomSheetListener 13 | import com.kennyc.bottomsheet.BottomSheetMenuDialogFragment 14 | import com.kennyc.bottomsheet.menu.BottomSheetMenu 15 | import kotlin.random.Random 16 | 17 | 18 | class MainActivity : AppCompatActivity(), View.OnClickListener, BottomSheetListener { 19 | 20 | private val TAG = MainActivity::class.java.simpleName 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setContentView(R.layout.activity_main) 25 | findViewById