├── sample ├── .gitignore ├── gradle.properties ├── src │ └── main │ │ ├── res │ │ ├── drawable-hdpi │ │ │ ├── icn_1.png │ │ │ ├── icn_2.png │ │ │ ├── icn_3.png │ │ │ ├── icn_4.png │ │ │ ├── icn_5.png │ │ │ ├── photo.png │ │ │ ├── btn_add.png │ │ │ ├── ic_launcher.png │ │ │ └── icn_close.png │ │ ├── drawable-mdpi │ │ │ ├── icn_1.png │ │ │ ├── icn_2.png │ │ │ ├── icn_3.png │ │ │ ├── icn_4.png │ │ │ ├── icn_5.png │ │ │ ├── photo.png │ │ │ ├── btn_add.png │ │ │ ├── ic_launcher.png │ │ │ └── icn_close.png │ │ ├── drawable-xhdpi │ │ │ ├── icn_1.png │ │ │ ├── icn_2.png │ │ │ ├── icn_3.png │ │ │ ├── icn_4.png │ │ │ ├── icn_5.png │ │ │ ├── photo.png │ │ │ ├── btn_add.png │ │ │ ├── icn_close.png │ │ │ └── ic_launcher.png │ │ ├── drawable-xxhdpi │ │ │ ├── icn_1.png │ │ │ ├── icn_2.png │ │ │ ├── icn_3.png │ │ │ ├── icn_4.png │ │ │ ├── icn_5.png │ │ │ ├── photo.png │ │ │ ├── btn_add.png │ │ │ ├── icn_close.png │ │ │ └── ic_launcher.png │ │ ├── drawable-xxxhdpi │ │ │ ├── icn_1.png │ │ │ ├── icn_2.png │ │ │ ├── icn_3.png │ │ │ ├── icn_4.png │ │ │ ├── icn_5.png │ │ │ ├── photo.png │ │ │ ├── btn_add.png │ │ │ ├── icn_close.png │ │ │ └── ic_launcher.png │ │ ├── values │ │ │ ├── colors.xml │ │ │ ├── styles.xml │ │ │ ├── dimens.xml │ │ │ └── strings.xml │ │ ├── drawable │ │ │ └── ic_arrow_back.xml │ │ ├── menu │ │ │ └── menu_main.xml │ │ └── layout │ │ │ ├── activity_sample.xml │ │ │ ├── toolbar.xml │ │ │ └── scrollable_content.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── yalantis │ │ └── contextmenu │ │ └── sample │ │ └── SampleActivity.kt ├── proguard-rules.pro └── build.gradle ├── lib ├── .gitignore ├── gradle.properties ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── yalantis │ │ │ └── contextmenu │ │ │ └── lib │ │ │ ├── MenuGravity.kt │ │ │ ├── HesitateInterpolator.kt │ │ │ ├── extensions │ │ │ ├── Animator.kt │ │ │ ├── View.kt │ │ │ └── Context.kt │ │ │ ├── MenuParams.kt │ │ │ ├── WrapperView.kt │ │ │ ├── ContextMenuDialogFragment.kt │ │ │ ├── MenuObject.kt │ │ │ └── MenuAdapter.kt │ │ ├── AndroidManifest.xml │ │ └── res │ │ ├── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ └── styles.xml │ │ └── layout │ │ └── fragment_menu.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── badge.png ├── screenshot.jpg ├── screenshot.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── gradlew ├── README.md └── LICENSE /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | lib.iml -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':lib' 2 | -------------------------------------------------------------------------------- /badge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/badge.png -------------------------------------------------------------------------------- /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/screenshot.jpg -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/screenshot.png -------------------------------------------------------------------------------- /lib/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=Context Menu 2 | POM_ARTIFACT_ID=contextmenu 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /sample/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=ContextMenu 2 | POM_ARTIFACT_ID=contextmenu 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/icn_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/icn_1.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/icn_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/icn_2.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/icn_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/icn_3.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/icn_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/icn_4.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/icn_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/icn_5.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/photo.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/icn_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/icn_1.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/icn_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/icn_2.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/icn_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/icn_3.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/icn_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/icn_4.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/icn_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/icn_5.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/photo.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/icn_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/icn_1.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/icn_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/icn_2.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/icn_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/icn_3.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/icn_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/icn_4.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/icn_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/icn_5.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/photo.png -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/MenuGravity.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | enum class MenuGravity { 4 | END, START 5 | } -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/btn_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/btn_add.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/btn_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/btn_add.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/btn_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/btn_add.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/icn_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/icn_1.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/icn_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/icn_2.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/icn_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/icn_3.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/icn_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/icn_4.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/icn_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/icn_5.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/photo.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/icn_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/icn_1.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/icn_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/icn_2.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/icn_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/icn_3.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/icn_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/icn_4.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/icn_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/icn_5.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/photo.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/icn_close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-hdpi/icn_close.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/icn_close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-mdpi/icn_close.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/icn_close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/icn_close.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/btn_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/btn_add.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/icn_close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/icn_close.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/btn_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/btn_add.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/icn_close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/icn_close.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #222222 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yalantis/Context-Menu.Android/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Jan 15 12:30:17 EET 2019 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-4.10.1-all.zip -------------------------------------------------------------------------------- /lib/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #B3000000 5 | #f2f2f2 6 | #b5b5b5 7 | 8 | -------------------------------------------------------------------------------- /lib/src/main/res/layout/fragment_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/HesitateInterpolator.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | import android.view.animation.Interpolator 4 | 5 | class HesitateInterpolator : Interpolator { 6 | 7 | override fun getInterpolation(input: Float): Float { 8 | val x = 2.0f * input - 1.0f 9 | return 0.5f * (x * x * x + 1.0f) 10 | } 11 | } -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22sp 5 | 10dp 6 | 25dp 7 | 26sp 8 | 56dp 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_arrow_back.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /lib/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2dp 4 | 1dp 5 | 6 | 8dp 7 | 16dp 8 | 8dp 9 | 16dp 10 | 11 | 15sp 12 | 13 | 14 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # files for the dex VM 2 | *.dex 3 | 4 | # Java class files 5 | *.class 6 | 7 | # generated files 8 | bin/ 9 | gen/ 10 | out/ 11 | build/ 12 | 13 | # Local configuration file (sdk path, etc) 14 | local.properties 15 | 16 | # Eclipse project files 17 | .classpath 18 | .project 19 | 20 | # Windows thumbnail db 21 | .DS_Store 22 | 23 | # IDEA/Android Studio project files, because 24 | # the project can be imported from settings.gradle 25 | .idea 26 | *.iml 27 | 28 | # Old-style IDEA project files 29 | *.ipr 30 | *.iws 31 | 32 | # Local IDEA workspace 33 | .idea/workspace.xml 34 | 35 | # Gradle cache 36 | .gradle 37 | 38 | # Sandbox stuff 39 | _sandbox -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ContextMenu 5 | Squid blog crucifix Marfa, consectetur vero placeat mixtape. Non seitan sriracha eu, do Banksy fashion axe cred qui vegan delectus consequat Helvetica mollit. XOXO gluten-free disrupt, gastropub officia cliche chillwave hella pop-up. Fixie swag raw denim, YOLO craft beer assumenda occupy veniam photo booth pug consectetur next level Portland tote bag +1. Mlkshk PBR aliquip placeat Wes Anderson. 6 | My Description 7 | Context menu 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Program Files (x86)\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /lib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:/Android/adt-bundle-windows-x86_64-20140702/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/extensions/Animator.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib.extensions 2 | 3 | import android.animation.Animator 4 | 5 | internal fun Animator.onAnimationEnd(onAnimationEnd: (Animator?) -> Unit) { 6 | this.addListener(object : Animator.AnimatorListener { 7 | override fun onAnimationRepeat(animation: Animator?) { 8 | // do nothing 9 | } 10 | 11 | override fun onAnimationEnd(animation: Animator?) { 12 | onAnimationEnd(animation) 13 | } 14 | 15 | override fun onAnimationCancel(animation: Animator?) { 16 | // do nothing 17 | } 18 | 19 | override fun onAnimationStart(animation: Animator?) { 20 | // do nothing 21 | } 22 | }) 23 | } -------------------------------------------------------------------------------- /lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | 8 | defaultConfig { 9 | minSdkVersion 19 10 | targetSdkVersion 28 11 | versionCode Integer.parseInt(project.VERSION_CODE) 12 | versionName VERSION_NAME 13 | } 14 | 15 | compileOptions { 16 | sourceCompatibility JavaVersion.VERSION_1_7 17 | targetCompatibility JavaVersion.VERSION_1_7 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | 24 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 25 | 26 | implementation 'com.android.support:appcompat-v7:28.0.0' 27 | } -------------------------------------------------------------------------------- /sample/src/main/res/layout/toolbar.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 19 | 20 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 13 | 14 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | 8 | defaultConfig { 9 | applicationId "com.yalantis.contextmenu.sample" 10 | minSdkVersion 19 11 | targetSdkVersion 28 12 | versionCode Integer.parseInt(project.VERSION_CODE) 13 | versionName VERSION_NAME 14 | vectorDrawables.useSupportLibrary = true 15 | } 16 | 17 | compileOptions { 18 | sourceCompatibility JavaVersion.VERSION_1_7 19 | targetCompatibility JavaVersion.VERSION_1_7 20 | } 21 | } 22 | 23 | dependencies { 24 | implementation fileTree(dir: 'libs', include: ['*.jar']) 25 | 26 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 27 | 28 | implementation 'com.android.support:appcompat-v7:28.0.0' 29 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 30 | 31 | implementation 'com.github.yalantis:context-menu.android:1.1.4' 32 | } -------------------------------------------------------------------------------- /lib/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 18 | 19 | 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | 20 | VERSION_NAME=1.1.0 21 | VERSION_CODE=10 22 | GROUP=com.yalantis 23 | 24 | POM_DESCRIPTION=Android Library to display awesome context menu 25 | POM_URL=https://github.com/Yalantis/Context-Menu.Android 26 | POM_SCM_URL=https://github.com/Yalantis/Context-Menu.Android 27 | POM_SCM_CONNECTION=scm:git@github.com/Yalantis/Context-Menu.Android.git 28 | POM_SCM_DEV_CONNECTION=scm:git@github.com/Yalantis/Context-Menu.Android.git 29 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 30 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0 31 | POM_LICENCE_DIST=repo 32 | POM_DEVELOPER_ID=yalantis 33 | POM_DEVELOPER_NAME=Yalantis -------------------------------------------------------------------------------- /sample/src/main/res/layout/scrollable_content.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 11 | 12 | 22 | 23 | 37 | 38 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/MenuParams.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | import android.os.Parcel 4 | import android.os.Parcelable 5 | 6 | /** 7 | * @property [animationDelay] 8 | * Delay after opening and before closing. 9 | * @see ContextMenuDialogFragment 10 | * 11 | * @property [isClosableOutside] 12 | * If option menu can be closed on touch to non-button area. 13 | * 14 | * @property [gravity] 15 | * You can change the side. By default - MenuGravity.END 16 | * @see MenuGravity 17 | */ 18 | data class MenuParams( 19 | var actionBarSize: Int = 0, 20 | var menuObjects: List = listOf(), 21 | var animationDelay: Long = 0L, 22 | var animationDuration: Long = ANIMATION_DURATION, 23 | var backgroundColorAnimationDuration: Long = BACKGROUND_COLOR_ANIMATION_DURATION, 24 | var isFitsSystemWindow: Boolean = false, 25 | var isClipToPadding: Boolean = true, 26 | var isClosableOutside: Boolean = false, 27 | var gravity: MenuGravity = MenuGravity.END 28 | ) : Parcelable { 29 | 30 | private constructor(parcel: Parcel) : this( 31 | parcel.readInt(), 32 | parcel.createTypedArrayList(MenuObject.CREATOR) ?: listOf(), 33 | parcel.readLong(), 34 | parcel.readLong(), 35 | parcel.readLong(), 36 | parcel.readByte() != 0.toByte(), 37 | parcel.readByte() != 0.toByte(), 38 | parcel.readByte() != 0.toByte(), 39 | parcel.readSerializable() as? MenuGravity ?: MenuGravity.END 40 | ) 41 | 42 | override fun writeToParcel(parcel: Parcel, flags: Int) { 43 | parcel.apply { 44 | writeInt(actionBarSize) 45 | writeTypedList(menuObjects) 46 | writeLong(animationDelay) 47 | writeLong(animationDuration) 48 | writeLong(backgroundColorAnimationDuration) 49 | writeByte(if (isFitsSystemWindow) 1 else 0) 50 | writeByte(if (isClipToPadding) 1 else 0) 51 | writeByte(if (isClosableOutside) 1 else 0) 52 | writeSerializable(gravity) 53 | } 54 | } 55 | 56 | override fun describeContents(): Int = 0 57 | 58 | companion object { 59 | 60 | const val ANIMATION_DURATION = 100L 61 | const val BACKGROUND_COLOR_ANIMATION_DURATION = 200L 62 | 63 | @JvmField 64 | val CREATOR = object : Parcelable.Creator { 65 | override fun createFromParcel(parcel: Parcel): MenuParams = MenuParams(parcel) 66 | 67 | override fun newArray(size: Int): Array = arrayOfNulls(size) 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/WrapperView.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | import android.content.Context 4 | import android.support.v4.view.ViewCompat 5 | import android.util.AttributeSet 6 | import android.view.Gravity 7 | import android.widget.FrameLayout 8 | import android.widget.LinearLayout 9 | import android.widget.RelativeLayout 10 | import android.widget.ScrollView 11 | import com.yalantis.contextmenu.lib.extensions.getDimension 12 | 13 | open class WrapperView @JvmOverloads constructor( 14 | context: Context, 15 | attrs: AttributeSet? = null, 16 | defStyleAttr: Int = 0 17 | ) : ScrollView(context, attrs, defStyleAttr) { 18 | 19 | val rootRelativeLayout: RelativeLayout by lazy { RelativeLayout(context) } 20 | val wrapperButtons: LinearLayout by lazy { LinearLayout(context) } 21 | val wrapperText: LinearLayout by lazy { LinearLayout(context) } 22 | 23 | init { 24 | setupScrollView() 25 | setupRootRelativeLayout() 26 | setupWrappers() 27 | } 28 | 29 | open fun show(menuGravity: MenuGravity) { 30 | wrapperButtons.layoutParams = 31 | RelativeLayout.LayoutParams( 32 | RelativeLayout.LayoutParams.WRAP_CONTENT, 33 | RelativeLayout.LayoutParams.WRAP_CONTENT 34 | ).apply { 35 | addRule( 36 | if (menuGravity == MenuGravity.START) { 37 | RelativeLayout.ALIGN_PARENT_START 38 | } else { 39 | RelativeLayout.ALIGN_PARENT_END 40 | } 41 | ) 42 | } 43 | 44 | wrapperText.apply { 45 | gravity = if (menuGravity == MenuGravity.START) { 46 | Gravity.START 47 | } else { 48 | Gravity.END 49 | } 50 | layoutParams = 51 | RelativeLayout.LayoutParams( 52 | RelativeLayout.LayoutParams.WRAP_CONTENT, 53 | RelativeLayout.LayoutParams.WRAP_CONTENT 54 | ).apply { 55 | val verb = if (menuGravity == MenuGravity.START) { 56 | RelativeLayout.END_OF 57 | } else { 58 | RelativeLayout.START_OF 59 | } 60 | addRule(verb, wrapperButtons.id) 61 | setMargins( 62 | 0, 63 | 0, 64 | context.getDimension(R.dimen.text_start_end_margin), 65 | 0 66 | ) 67 | } 68 | } 69 | } 70 | 71 | private fun setupScrollView() { 72 | isFillViewport = true 73 | } 74 | 75 | private fun setupRootRelativeLayout() { 76 | addView( 77 | rootRelativeLayout.apply { 78 | id = ViewCompat.generateViewId() 79 | layoutParams = FrameLayout.LayoutParams( 80 | FrameLayout.LayoutParams.MATCH_PARENT, 81 | FrameLayout.LayoutParams.WRAP_CONTENT 82 | ) 83 | } 84 | ) 85 | } 86 | 87 | private fun setupWrappers() { 88 | rootRelativeLayout.addView( 89 | wrapperButtons.apply { 90 | id = ViewCompat.generateViewId() 91 | orientation = LinearLayout.VERTICAL 92 | } 93 | ) 94 | 95 | rootRelativeLayout.addView( 96 | wrapperText.apply { 97 | id = ViewCompat.generateViewId() 98 | orientation = LinearLayout.VERTICAL 99 | } 100 | ) 101 | } 102 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/ContextMenuDialogFragment.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | import android.os.Bundle 4 | import android.os.Handler 5 | import android.support.v4.app.DialogFragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import android.view.WindowManager 10 | import com.yalantis.contextmenu.lib.extensions.backgroundColorAppear 11 | import com.yalantis.contextmenu.lib.extensions.backgroundColorDisappear 12 | import kotlinx.android.synthetic.main.fragment_menu.* 13 | 14 | open class ContextMenuDialogFragment : DialogFragment() { 15 | 16 | var menuItemClickListener: (view: View, position: Int) -> Unit = { _, _ -> } 17 | var menuItemLongClickListener: (view: View, position: Int) -> Unit = { _, _ -> } 18 | 19 | private lateinit var menuParams: MenuParams 20 | private lateinit var dropDownMenuAdapter: MenuAdapter 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setStyle(STYLE_NO_FRAME, R.style.MenuFragmentStyle) 25 | menuParams = arguments?.getParcelable(ARGS_MENU_PARAMS) as? MenuParams ?: MenuParams() 26 | } 27 | 28 | override fun onCreateView( 29 | inflater: LayoutInflater, 30 | container: ViewGroup?, 31 | savedInstanceState: Bundle? 32 | ): View? = inflater.inflate(R.layout.fragment_menu, container, false)?.apply { 33 | fitsSystemWindows = menuParams.isFitsSystemWindow 34 | (this as ViewGroup).clipToPadding = menuParams.isClipToPadding 35 | dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) 36 | } 37 | 38 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 39 | initDropDownMenuAdapter() 40 | 41 | Handler().postDelayed({ 42 | dropDownMenuAdapter.menuToggle() 43 | }, menuParams.animationDelay) 44 | 45 | wrapperView.apply { 46 | backgroundColorAppear(menuParams.backgroundColorAnimationDuration) 47 | show(menuParams.gravity) 48 | 49 | if (menuParams.isClosableOutside) { 50 | rootRelativeLayout.setOnClickListener { 51 | if (isAdded) { 52 | dropDownMenuAdapter.closeOutside() 53 | } 54 | } 55 | } 56 | } 57 | } 58 | 59 | private fun initDropDownMenuAdapter() { 60 | activity?.let { 61 | dropDownMenuAdapter = MenuAdapter( 62 | it, 63 | wrapperView.wrapperButtons, 64 | wrapperView.wrapperText, 65 | menuParams.menuObjects, 66 | menuParams.actionBarSize, 67 | menuParams.gravity 68 | ).apply { 69 | setAnimationDuration(menuParams.animationDuration) 70 | 71 | onCloseOutsideClickListener = { _ -> 72 | close() 73 | } 74 | 75 | onItemClickListener = { view -> 76 | val position = (view.parent as ViewGroup).indexOfChild(view) 77 | menuItemClickListener(view, position) 78 | close() 79 | } 80 | 81 | onItemLongClickListener = { view -> 82 | val position = (view.parent as ViewGroup).indexOfChild(view) 83 | menuItemLongClickListener(view, position) 84 | close() 85 | } 86 | } 87 | } 88 | } 89 | 90 | private fun close() { 91 | wrapperView.backgroundColorDisappear(menuParams.backgroundColorAnimationDuration) { 92 | Handler().postDelayed({ 93 | dismissAllowingStateLoss() 94 | }, menuParams.animationDelay) 95 | } 96 | } 97 | 98 | companion object { 99 | 100 | const val TAG = "ContextMenuDialogFragment" 101 | private const val ARGS_MENU_PARAMS = "menuParams" 102 | 103 | @JvmStatic 104 | fun newInstance(menuParams: MenuParams): ContextMenuDialogFragment = 105 | ContextMenuDialogFragment().apply { 106 | arguments = Bundle().apply { 107 | putParcelable(ARGS_MENU_PARAMS, menuParams) 108 | } 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/extensions/View.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib.extensions 2 | 3 | import android.animation.AnimatorSet 4 | import android.animation.ArgbEvaluator 5 | import android.animation.ObjectAnimator 6 | import android.support.annotation.ColorRes 7 | import android.view.View 8 | import com.yalantis.contextmenu.lib.MenuGravity 9 | import com.yalantis.contextmenu.lib.R 10 | 11 | const val ROTATION_ZERO_DEGREES = 0f 12 | const val ROTATION_NINETY_DEGREES = 90f 13 | const val ALPHA_INVISIBLE = 0f 14 | const val ALPHA_VISIBLE = 1f 15 | const val TRANSLATION_ZERO_VALUE = 0f 16 | 17 | private const val ROTATION_Y_PROPERTY = "rotationY" 18 | private const val ROTATION_X_PROPERTY = "rotationX" 19 | private const val ALPHA_PROPERTY = "alpha" 20 | private const val TRANSLATION_X_PROPERTY = "translationX" 21 | private const val BACKGROUND_COLOR_PROPERTY = "backgroundColor" 22 | 23 | internal fun View.rotationCloseHorizontal(gravity: MenuGravity): ObjectAnimator { 24 | val from = ROTATION_ZERO_DEGREES 25 | var to = when (gravity) { 26 | MenuGravity.END -> -ROTATION_NINETY_DEGREES 27 | MenuGravity.START -> ROTATION_NINETY_DEGREES 28 | } 29 | 30 | if (context.isLayoutDirectionRtl()) { 31 | to *= -1f 32 | } 33 | 34 | return ObjectAnimator.ofFloat(this, ROTATION_Y_PROPERTY, from, to) 35 | } 36 | 37 | internal fun View.rotationOpenHorizontal(gravity: MenuGravity): ObjectAnimator { 38 | var from = when (gravity) { 39 | MenuGravity.END -> -ROTATION_NINETY_DEGREES 40 | MenuGravity.START -> ROTATION_NINETY_DEGREES 41 | } 42 | val to = ROTATION_ZERO_DEGREES 43 | 44 | if (context.isLayoutDirectionRtl()) { 45 | from *= -1f 46 | } 47 | 48 | return ObjectAnimator.ofFloat(this, ROTATION_Y_PROPERTY, from, to) 49 | } 50 | 51 | internal fun View.rotationCloseVertical(): ObjectAnimator = 52 | ObjectAnimator.ofFloat(this, ROTATION_X_PROPERTY, ROTATION_ZERO_DEGREES, -ROTATION_NINETY_DEGREES) 53 | 54 | internal fun View.rotationOpenVertical(): ObjectAnimator = 55 | ObjectAnimator.ofFloat(this, ROTATION_X_PROPERTY, -ROTATION_NINETY_DEGREES, ROTATION_ZERO_DEGREES) 56 | 57 | internal fun View.alphaDisappear(): ObjectAnimator = 58 | ObjectAnimator.ofFloat(this, ALPHA_PROPERTY, ALPHA_VISIBLE, ALPHA_INVISIBLE) 59 | 60 | internal fun View.alphaAppear(): ObjectAnimator = 61 | ObjectAnimator.ofFloat(this, ALPHA_PROPERTY, ALPHA_INVISIBLE, ALPHA_VISIBLE) 62 | 63 | internal fun View.translationEnd(x: Float): ObjectAnimator { 64 | var from = TRANSLATION_ZERO_VALUE 65 | var to = x 66 | 67 | if (context.isLayoutDirectionRtl()) { 68 | from = x 69 | to = TRANSLATION_ZERO_VALUE 70 | } 71 | 72 | return ObjectAnimator.ofFloat(this, TRANSLATION_X_PROPERTY, from, to) 73 | } 74 | 75 | internal fun View.translationStart(x: Float): ObjectAnimator { 76 | var from = x 77 | var to = TRANSLATION_ZERO_VALUE 78 | 79 | if (context.isLayoutDirectionRtl()) { 80 | from = TRANSLATION_ZERO_VALUE 81 | to = x 82 | } 83 | 84 | return ObjectAnimator.ofFloat(this, TRANSLATION_X_PROPERTY, from, to) 85 | } 86 | 87 | internal fun View.fadeOutSet(x: Float, gravity: MenuGravity): AnimatorSet = AnimatorSet().apply { 88 | val translation = when (gravity) { 89 | MenuGravity.END -> translationEnd(x) 90 | MenuGravity.START -> translationStart(x) 91 | } 92 | playTogether(alphaDisappear(), translation) 93 | } 94 | 95 | internal fun View.colorAnimation( 96 | duration: Long, 97 | @ColorRes startColorResId: Int, 98 | @ColorRes endColorResId: Int 99 | ): ObjectAnimator = 100 | ObjectAnimator.ofObject( 101 | this, 102 | BACKGROUND_COLOR_PROPERTY, 103 | ArgbEvaluator(), 104 | context.getColorCompat(startColorResId), 105 | context.getColorCompat(endColorResId) 106 | ).apply { 107 | this.duration = duration 108 | start() 109 | } 110 | 111 | internal fun View.backgroundColorAppear(duration: Long): ObjectAnimator = 112 | colorAnimation(duration, android.R.color.transparent, R.color.menu_fragment_background) 113 | 114 | internal fun View.backgroundColorDisappear( 115 | duration: Long, 116 | onAnimationEnd: () -> Unit 117 | ): ObjectAnimator = 118 | colorAnimation( 119 | duration, 120 | R.color.menu_fragment_background, 121 | android.R.color.transparent 122 | ).apply { 123 | onAnimationEnd { 124 | onAnimationEnd() 125 | } 126 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/MenuObject.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | import android.graphics.Bitmap 4 | import android.graphics.drawable.BitmapDrawable 5 | import android.graphics.drawable.ColorDrawable 6 | import android.graphics.drawable.Drawable 7 | import android.os.Parcel 8 | import android.os.Parcelable 9 | import android.widget.ImageView 10 | 11 | open class MenuObject(var title: String = "") : Parcelable { 12 | 13 | var bgDrawable: Drawable? = null 14 | 15 | var bgColor: Int = 0 16 | private set 17 | 18 | var bgResource: Int = 0 19 | private set 20 | 21 | var drawable: Drawable? = null 22 | 23 | var color: Int = 0 24 | private set 25 | 26 | var bitmap: Bitmap? = null 27 | private set 28 | 29 | var resource: Int = 0 30 | private set 31 | 32 | var scaleType: ImageView.ScaleType = ImageView.ScaleType.CENTER_INSIDE 33 | var textColor: Int = 0 34 | var dividerColor: Int = Integer.MAX_VALUE 35 | var menuTextAppearanceStyle: Int = 0 36 | 37 | private constructor(parcel: Parcel) : this(parcel.readString() ?: "") { 38 | val bitmapBgDrawable = parcel.readParcelable(Bitmap::class.java.classLoader) 39 | bgDrawable = if (bitmapBgDrawable == null) { 40 | ColorDrawable(parcel.readInt()) 41 | } else { 42 | // TODO create BitmapDrawable with resources 43 | BitmapDrawable(bitmapBgDrawable) 44 | } 45 | 46 | bgColor = parcel.readInt() 47 | bgResource = parcel.readInt() 48 | 49 | val bitmapDrawable = parcel.readParcelable(Bitmap::class.java.classLoader) 50 | // TODO create BitmapDrawable with resources 51 | bitmapDrawable?.let { drawable = BitmapDrawable(it) } 52 | 53 | color = parcel.readInt() 54 | bitmap = parcel.readParcelable(Bitmap::class.java.classLoader) 55 | resource = parcel.readInt() 56 | scaleType = ImageView.ScaleType.values()[parcel.readInt()] 57 | textColor = parcel.readInt() 58 | dividerColor = parcel.readInt() 59 | menuTextAppearanceStyle = parcel.readInt() 60 | } 61 | 62 | override fun writeToParcel(parcel: Parcel, flags: Int) { 63 | parcel.apply { 64 | writeString(title) 65 | 66 | when (bgDrawable) { 67 | null -> writeParcelable(null, flags) 68 | is BitmapDrawable -> writeParcelable((bgDrawable as BitmapDrawable).bitmap, flags) 69 | is ColorDrawable -> writeInt((bgDrawable as ColorDrawable).color) 70 | } 71 | 72 | writeInt(bgColor) 73 | writeInt(bgResource) 74 | 75 | when (drawable) { 76 | null -> writeParcelable(null, flags) 77 | is BitmapDrawable -> writeParcelable((drawable as BitmapDrawable).bitmap, flags) 78 | } 79 | 80 | writeInt(color) 81 | writeParcelable(bitmap, flags) 82 | writeInt(resource) 83 | writeInt(scaleType.ordinal) 84 | writeInt(textColor) 85 | writeInt(dividerColor) 86 | writeInt(menuTextAppearanceStyle) 87 | } 88 | } 89 | 90 | override fun describeContents(): Int = 0 91 | 92 | fun setBgDrawable(drawable: ColorDrawable) { 93 | setBgDrawableInternal(drawable) 94 | } 95 | 96 | fun setBgDrawable(drawable: BitmapDrawable) { 97 | setBgDrawableInternal(drawable) 98 | } 99 | 100 | fun setBgColorValue(value: Int) { 101 | bgColor = value 102 | bgDrawable = null 103 | bgResource = 0 104 | } 105 | 106 | fun setBgResourceValue(value: Int) { 107 | bgResource = value 108 | bgDrawable = null 109 | bgColor = 0 110 | } 111 | 112 | fun setColorValue(value: Int) { 113 | color = value 114 | drawable = null 115 | bitmap = null 116 | resource = 0 117 | } 118 | 119 | fun setBitmapValue(value: Bitmap) { 120 | bitmap = value 121 | drawable = null 122 | color = 0 123 | resource = 0 124 | } 125 | 126 | fun setResourceValue(value: Int) { 127 | resource = value 128 | drawable = null 129 | color = 0 130 | bitmap = null 131 | } 132 | 133 | private fun setBgDrawableInternal(drawable: Drawable) { 134 | bgDrawable = drawable 135 | bgColor = 0 136 | bgResource = 0 137 | } 138 | 139 | companion object CREATOR : Parcelable.Creator { 140 | override fun createFromParcel(parcel: Parcel): MenuObject = MenuObject(parcel) 141 | 142 | override fun newArray(size: Int): Array = arrayOfNulls(size) 143 | } 144 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/extensions/Context.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib.extensions 2 | 3 | import android.content.Context 4 | import android.graphics.Color 5 | import android.graphics.drawable.ColorDrawable 6 | import android.os.Build 7 | import android.support.annotation.ColorRes 8 | import android.support.annotation.DimenRes 9 | import android.support.v4.content.ContextCompat 10 | import android.view.Gravity 11 | import android.view.View 12 | import android.view.ViewGroup 13 | import android.widget.* 14 | import com.yalantis.contextmenu.lib.MenuObject 15 | import com.yalantis.contextmenu.lib.R 16 | 17 | @JvmName("getDefaultActionBarSize") 18 | fun Context.getDefaultActionBarSize(): Int { 19 | val styledAttrs = theme.obtainStyledAttributes(intArrayOf(android.R.attr.actionBarSize)) 20 | val actionBarSize = styledAttrs.getDimension(0, 0f).toInt() 21 | styledAttrs.recycle() 22 | return actionBarSize 23 | } 24 | 25 | internal fun Context.getColorCompat(@ColorRes color: Int) = ContextCompat.getColor(this, color) 26 | 27 | internal fun Context.getDimension(@DimenRes dimen: Int) = resources.getDimension(dimen).toInt() 28 | 29 | internal fun Context.isLayoutDirectionRtl(): Boolean = 30 | resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL 31 | 32 | internal fun Context.getItemTextView( 33 | menuItem: MenuObject, 34 | menuItemSize: Int, 35 | onCLick: View.OnClickListener, 36 | onLongClick: View.OnLongClickListener 37 | ): TextView = TextView(this).apply { 38 | val textColor = if (menuItem.textColor == 0) { 39 | android.R.color.white 40 | } else { 41 | menuItem.textColor 42 | } 43 | 44 | val styleResId = if (menuItem.menuTextAppearanceStyle > 0) { 45 | menuItem.menuTextAppearanceStyle 46 | } else { 47 | R.style.TextView_DefaultStyle 48 | } 49 | 50 | layoutParams = RelativeLayout.LayoutParams( 51 | ViewGroup.LayoutParams.WRAP_CONTENT, 52 | menuItemSize 53 | ) 54 | text = menuItem.title 55 | gravity = Gravity.CENTER_VERTICAL 56 | 57 | setOnClickListener(onCLick) 58 | setOnLongClickListener(onLongClick) 59 | setPadding( 60 | getDimension(R.dimen.text_start_end_padding_medium), 61 | 0, 62 | getDimension(R.dimen.text_start_end_padding_small), 63 | 0 64 | ) 65 | 66 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 67 | setTextAppearance(context, styleResId) 68 | } else { 69 | setTextAppearance(styleResId) 70 | } 71 | 72 | setTextColor(getColorCompat(textColor)) 73 | } 74 | 75 | internal fun Context.getItemImageButton(menuItem: MenuObject): ImageView = 76 | ImageButton(this).apply { 77 | val paddingValue = getDimension(R.dimen.menu_item_padding) 78 | 79 | layoutParams = RelativeLayout.LayoutParams( 80 | ViewGroup.LayoutParams.MATCH_PARENT, 81 | ViewGroup.LayoutParams.MATCH_PARENT 82 | ) 83 | isClickable = false 84 | isFocusable = false 85 | scaleType = menuItem.scaleType 86 | 87 | setPadding(paddingValue, paddingValue, paddingValue, paddingValue) 88 | setBackgroundColor(Color.TRANSPARENT) 89 | 90 | menuItem.apply { 91 | when { 92 | color != 0 -> setImageDrawable(ColorDrawable(color)) 93 | resource != 0 -> setImageResource(resource) 94 | bitmap != null -> setImageBitmap(bitmap) 95 | drawable != null -> setImageDrawable(drawable) 96 | } 97 | } 98 | } 99 | 100 | internal fun Context.getDivider(menuItem: MenuObject): View = View(this).apply { 101 | val dividerColor = if (menuItem.dividerColor == Integer.MAX_VALUE) { 102 | R.color.divider_color 103 | } else { 104 | menuItem.dividerColor 105 | } 106 | 107 | layoutParams = RelativeLayout.LayoutParams( 108 | ViewGroup.LayoutParams.MATCH_PARENT, 109 | getDimension(R.dimen.divider_height) 110 | ).apply { addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) } 111 | isClickable = true 112 | 113 | setBackgroundColor(getColorCompat(dividerColor)) 114 | } 115 | 116 | internal fun Context.getImageWrapper( 117 | menuItem: MenuObject, 118 | menuItemSize: Int, 119 | onCLick: View.OnClickListener, 120 | onLongClick: View.OnLongClickListener, 121 | showDivider: Boolean 122 | ): RelativeLayout = RelativeLayout(this).apply { 123 | layoutParams = LinearLayout.LayoutParams(menuItemSize, menuItemSize) 124 | 125 | setOnClickListener(onCLick) 126 | setOnLongClickListener(onLongClick) 127 | addView(getItemImageButton(menuItem)) 128 | if (showDivider) { 129 | addView(getDivider(menuItem)) 130 | } 131 | 132 | menuItem.apply { 133 | when { 134 | bgColor != 0 -> setBackgroundColor(bgColor) 135 | bgDrawable != null -> background = bgDrawable 136 | bgResource != 0 -> setBackgroundResource(bgResource) 137 | else -> setBackgroundColor(getColorCompat(R.color.menu_item_background)) 138 | } 139 | } 140 | } -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yalantis/contextmenu/sample/SampleActivity.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.sample 2 | 3 | import android.graphics.BitmapFactory 4 | import android.graphics.drawable.BitmapDrawable 5 | import android.os.Bundle 6 | import android.support.v7.app.AppCompatActivity 7 | import android.view.Menu 8 | import android.view.MenuItem 9 | import android.widget.Toast 10 | import com.yalantis.contextmenu.R 11 | import com.yalantis.contextmenu.lib.ContextMenuDialogFragment 12 | import com.yalantis.contextmenu.lib.MenuObject 13 | import com.yalantis.contextmenu.lib.MenuParams 14 | import kotlinx.android.synthetic.main.toolbar.* 15 | 16 | class SampleActivity : AppCompatActivity() { 17 | 18 | private lateinit var contextMenuDialogFragment: ContextMenuDialogFragment 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_sample) 23 | 24 | initToolbar() 25 | initMenuFragment() 26 | } 27 | 28 | override fun onCreateOptionsMenu(menu: Menu?): Boolean { 29 | menuInflater.inflate(R.menu.menu_main, menu) 30 | return true 31 | } 32 | 33 | override fun onOptionsItemSelected(item: MenuItem?): Boolean { 34 | item?.let { 35 | when (it.itemId) { 36 | R.id.context_menu -> { 37 | showContextMenuDialogFragment() 38 | } 39 | } 40 | } 41 | 42 | return super.onOptionsItemSelected(item) 43 | } 44 | 45 | override fun onBackPressed() { 46 | if (::contextMenuDialogFragment.isInitialized && contextMenuDialogFragment.isAdded) { 47 | contextMenuDialogFragment.dismiss() 48 | } else { 49 | finish() 50 | } 51 | } 52 | 53 | private fun initToolbar() { 54 | setSupportActionBar(toolbar) 55 | 56 | supportActionBar?.apply { 57 | setHomeButtonEnabled(true) 58 | setDisplayHomeAsUpEnabled(true) 59 | setDisplayShowTitleEnabled(false) 60 | } 61 | 62 | toolbar.apply { 63 | setNavigationIcon(R.drawable.ic_arrow_back) 64 | setNavigationOnClickListener { onBackPressed() } 65 | } 66 | 67 | tvToolbarTitle.text = "Samantha" 68 | } 69 | 70 | /** 71 | * If you want to change the side you need to add 'gravity' parameter, 72 | * by default it is MenuGravity.END. 73 | * 74 | * For example: 75 | * 76 | * MenuParams( 77 | * actionBarSize = resources.getDimension(R.dimen.tool_bar_height).toInt(), 78 | * menuObjects = getMenuObjects(), 79 | * isClosableOutside = false, 80 | * gravity = MenuGravity.START 81 | * ) 82 | */ 83 | private fun initMenuFragment() { 84 | val menuParams = MenuParams( 85 | actionBarSize = resources.getDimension(R.dimen.tool_bar_height).toInt(), 86 | menuObjects = getMenuObjects(), 87 | isClosableOutside = false 88 | ) 89 | 90 | contextMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams).apply { 91 | menuItemClickListener = { view, position -> 92 | Toast.makeText( 93 | this@SampleActivity, 94 | "Clicked on position: $position", 95 | Toast.LENGTH_SHORT 96 | ).show() 97 | } 98 | menuItemLongClickListener = { view, position -> 99 | Toast.makeText( 100 | this@SampleActivity, 101 | "Long clicked on position: $position", 102 | Toast.LENGTH_SHORT 103 | ).show() 104 | } 105 | } 106 | } 107 | 108 | /** 109 | * You can use any (drawable, resource, bitmap, color) as image: 110 | * menuObject.drawable = ... 111 | * menuObject.setResourceValue(...) 112 | * menuObject.setBitmapValue(...) 113 | * menuObject.setColorValue(...) 114 | * 115 | * You can set image ScaleType: 116 | * menuObject.scaleType = ScaleType.FIT_XY 117 | * 118 | * You can use any [resource, drawable, color] as background: 119 | * menuObject.setBgResourceValue(...) 120 | * menuObject.setBgDrawable(...) 121 | * menuObject.setBgColorValue(...) 122 | * 123 | * You can use any (color) as text color: 124 | * menuObject.textColor = ... 125 | * 126 | * You can set any (color) as divider color: 127 | * menuObject.dividerColor = ... 128 | */ 129 | private fun getMenuObjects() = mutableListOf().apply { 130 | val close = MenuObject().apply { setResourceValue(R.drawable.icn_close) } 131 | val send = MenuObject("Send message").apply { setResourceValue(R.drawable.icn_1) } 132 | val like = MenuObject("Like profile").apply { 133 | setBitmapValue(BitmapFactory.decodeResource(resources, R.drawable.icn_2)) 134 | } 135 | val addFriend = MenuObject("Add to friends").apply { 136 | drawable = BitmapDrawable( 137 | resources, 138 | BitmapFactory.decodeResource(resources, R.drawable.icn_3) 139 | ) 140 | } 141 | val addFavorite = MenuObject("Add to favorites").apply { 142 | setResourceValue(R.drawable.icn_4) 143 | } 144 | val block = MenuObject("Block user").apply { setResourceValue(R.drawable.icn_5) } 145 | 146 | add(close) 147 | add(send) 148 | add(like) 149 | add(addFriend) 150 | add(addFavorite) 151 | add(block) 152 | } 153 | 154 | private fun showContextMenuDialogFragment() { 155 | if (supportFragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) { 156 | contextMenuDialogFragment.show(supportFragmentManager, ContextMenuDialogFragment.TAG) 157 | } 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Context--Menu.Android-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1387) [![Yalantis](https://github.com/Yalantis/Context-Menu.Android/blob/master/badge.png)](https://yalantis.com/?utm_source=github) [![](https://jitpack.io/v/yalantis/context-menu.android.svg)](https://jitpack.io/#yalantis/context-menu.android) 2 | 3 | # ContextMenu 4 | 5 | #### You can easily add awesome animated context menu to your app. 6 | 7 | Check this [project on dribbble](https://dribbble.com/shots/1785274-Menu-Animation-for-Additional-Functions?list=users&offset=17) 8 | 9 | Check this [project on Behance](https://www.behance.net/gallery/20411445/Mobile-Animations-Interactions) 10 | 11 | 12 | 13 | ### Usage: 14 | 15 | *For a working implementation, have a look at the ```app``` module* 16 | 17 | #### 1. Clone repository and add sources into your project or use Gradle: 18 | 19 | Add it in your root build.gradle at the end of repositories: 20 | ``` 21 | allprojects { 22 | repositories { 23 | ... 24 | maven { url 'https://jitpack.io' } 25 | } 26 | } 27 | ``` 28 | Add the dependency 29 | ``` 30 | dependencies { 31 | implementation 'com.github.Yalantis:Context-Menu.Android:1.1.4' 32 | } 33 | ``` 34 | #### 2. Create list of `MenuObject`, which consists of icon or icon and description. 35 | You can use any `drawable, resource, bitmap, color` as image: 36 | ``` 37 | menuObject.drawable = ... 38 | menuObject.setResourceValue(...) 39 | menuObject.setBitmapValue(...) 40 | menuObject.setColorValue(...) 41 | ``` 42 | You can set image `ScaleType`: 43 | ``` 44 | menuObject.scaleType = ScaleType.FIT_XY 45 | ``` 46 | You can use any `resource, drawable, color` as background: 47 | ``` 48 | menuObject.setBgResourceValue(...) 49 | menuObject.setBgDrawable(...) 50 | menuObject.setBgColorValue(...) 51 | ``` 52 | Now You can easily add text appearance style for menu titles: 53 | ``` 54 | In your project styles create style for text appearance 55 | (For better visual effect extend it from TextView.DefaultStyle): 56 | 57 | 61 | 62 | And set it's id to your MenuObject : 63 | 64 | val bitmapDrawable = BitmapDrawable( 65 | resources, 66 | BitmapFactory.decodeResource(resources, R.drawable.icn_3) 67 | ) 68 | 69 | val menuObject = MenuObject("Add to friends").apply { 70 | drawable = bitmapDrawable 71 | menuTextAppearanceStyle = R.style.TextViewStyle 72 | } 73 | ``` 74 | You can use any `color` as text color: 75 | ``` 76 | menuObject.textColor = ... 77 | ``` 78 | You can set any `color` as divider color: 79 | ``` 80 | menuObject.dividerColor = ... 81 | ``` 82 | Example: 83 | ``` 84 | val close = MenuObject().apply { setResourceValue(R.drawable.icn_close) } 85 | 86 | val send = MenuObject("Send message").apply { setResourceValue(R.drawable.icn_1) } 87 | 88 | val addFriend = MenuObject("Add to friends").apply { 89 | drawable = BitmapDrawable( 90 | resources, 91 | BitmapFactory.decodeResource(resources, R.drawable.icn_3) 92 | ) 93 | } 94 | 95 | val menuObjects = mutableListOf().apply { 96 | add(close) 97 | add(send) 98 | add(addFriend) 99 | } 100 | ``` 101 | 102 | #### 3. Create `newInstance` of `ContextMenuDialogFragment`, which received `MenuParams` object. 103 | 104 | ``` 105 | val menuParams = MenuParams( 106 | actionBarSize = resources.getDimension(R.dimen.tool_bar_height).toInt(), 107 | menuObjects = getMenuObjects(), 108 | isClosableOutside = false 109 | // set other settings to meet your needs 110 | ) 111 | 112 | // If you want to change the side you need to add 'gravity' parameter, 113 | // by default it is MenuGravity.END. 114 | 115 | // For example: 116 | 117 | val menuParams = MenuParams( 118 | actionBarSize = resources.getDimension(R.dimen.tool_bar_height).toInt(), 119 | menuObjects = getMenuObjects(), 120 | isClosableOutside = false, 121 | gravity = MenuGravity.START 122 | ) 123 | 124 | val contextMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams) 125 | ``` 126 | 127 | #### 4. Set menu with button, which will open `ContextMenuDialogFragment`. 128 | 129 | ``` 130 | override fun onCreateOptionsMenu(menu: Menu?): Boolean { 131 | menuInflater.inflate(R.menu.menu_main, menu) 132 | return true 133 | } 134 | 135 | override fun onOptionsItemSelected(item: MenuItem?): Boolean { 136 | item?.let { 137 | when (it.itemId) { 138 | R.id.context_menu -> { 139 | showContextMenuDialogFragment() 140 | } 141 | } 142 | } 143 | 144 | return super.onOptionsItemSelected(item) 145 | } 146 | ``` 147 | 148 | #### 5. Add menu item listeners. 149 | ``` 150 | contextMenuDialogFragment = menuItemClickListener = { view, position -> 151 | // do something here 152 | } 153 | 154 | contextMenuDialogFragment = menuItemLongClickListener = { view, position -> 155 | // do something here 156 | } 157 | ``` 158 | 159 | ## Customization: 160 | For better experience menu item size should be equal to `ActionBar` height. 161 | 162 | `newInstance` of `ContextMenuDialogFragment` receives `MenuParams` object that has the fields: 163 | 164 | `menuObjects` - list of MenuObject objects, 165 | 166 | `animationDelay` - delay in millis after fragment opening and before closing, which will make animation smoother on slow devices, 167 | 168 | `animationDuration` - duration of every piece of animation in millis, 169 | 170 | `isClosableOutside` - if menu can be closed on touch to non-button area, 171 | 172 | `isFitSystemWindows` - if true, then the default implementation of fitSystemWindows(Rect) will be executed, 173 | 174 | `isClipToPadding` - true to clip children to the padding of the group, false otherwise. 175 | 176 | The last two parameters may be useful if you use _Translucent_ parameters in your theme: 177 | ``` 178 | true 179 | ``` 180 | To stay `Context Menu` below Status Bar set `fitSystemWindows` to true and `clipToPadding` to false. 181 | 182 | ## Compatibility 183 | 184 | * Android KitKat 4.4+ 185 | 186 | # Changelog 187 | 188 | ### Version: 1.1.4 189 | 190 | * added background color animation 191 | 192 | ### Version: 1.1.2 193 | 194 | * added animation on close outside menu 195 | 196 | ### Version: 1.1.1 197 | 198 | * bug with `menuObject.textColor` was fixed 199 | 200 | ### Version: 1.1.0 201 | 202 | * library rewrited on Kotlin 203 | * added `rtl` support 204 | * added `gravity` parameter for `MenuParams` 205 | 206 | ### Version: 1.0.8 207 | 208 | * added transparent menu background support 209 | * dependencies actualized 210 | 211 | ### Version: 1.0.7 212 | 213 | * Text in menu now also clickable 214 | * Support libs and gradle updated 215 | 216 | ### Version: 1.0.6 217 | 218 | * com.android.tools.build:gradle:1.5.0 219 | * Support libs and sdk updated to 23 220 | 221 | ### Version: 1.0.5 222 | 223 | * Fixed `setClosableOutside` [issue](https://github.com/Yalantis/Context-Menu.Android/issues/25). 224 | * Fixed `setAnimationDuration` doesn´t work for open event [issue](https://github.com/Yalantis/Context-Menu.Android/issues/22). 225 | * Fixed menu item listener setting mechanism. It can be not activity but any class that implements listeners now. [Issue](https://github.com/Yalantis/Context-Menu.Android/issues/24). Attention! You have to set listeners to the context fragment manually. Check block 5 in the `Usage`. 226 | 227 | ### Version: 1.0.4 228 | 229 | * Old `ContextMenuDialogFragment` `newInstance` methods are deprecated. Use new universal one that received `MenuParams`. 230 | * Added possibility to dismiss menu by clicking on non-button area. See `MenuParams.setClosableOutside(boolean)`. 231 | 232 | ### Version: 1.0.3 233 | 234 | * Added menu text appearence style. (Note: other text style methods are deprecated). 235 | 236 | ### Version: 1.0.2 237 | 238 | * Changed `MenuObject` constructors. Image setting is moved to methods 239 | * Added styling of `MenuObject` image, background, text color, divider color 240 | * Added possibility to interact with translucent Status Bar 241 | 242 | ### Version: 1.0.1 243 | 244 | * Added `OnMenuItemLongClickListener` (usage: the same as `OnMenuItemClickListener`, check sample app) 245 | * Renamed: 246 | ``` 247 | com.yalantis.contextmenu.lib.ContextMenuDialogFragment.ItemClickListener -> 248 | com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener 249 | 250 | com.yalantis.contextmenu.lib.ContextMenuDialogFragment.ItemClickListener.onItemClick(...) -> 251 | com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener.onMenuItemClick(...) 252 | ``` 253 | 254 | ### Version: 1.0 255 | 256 | * Pilot version 257 | 258 | #### Let us know! 259 | 260 | We’d be really happy if you sent us links to your projects where you use our component. Just send an email to github@yalantis.com And do let us know if you have any questions or suggestion regarding the animation. 261 | 262 | P.S. We’re going to publish more awesomeness wrapped in code and a tutorial on how to make UI for Android (iOS) better than better. Stay tuned! 263 | 264 | ## License 265 | 266 | Copyright 2019, Yalantis 267 | 268 | Licensed under the Apache License, Version 2.0 (the "License"); 269 | you may not use this file except in compliance with the License. 270 | You may obtain a copy of the License at 271 | 272 | http://www.apache.org/licenses/LICENSE-2.0 273 | 274 | Unless required by applicable law or agreed to in writing, software 275 | distributed under the License is distributed on an "AS IS" BASIS, 276 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 277 | See the License for the specific language governing permissions and 278 | limitations under the License. 279 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/src/main/java/com/yalantis/contextmenu/lib/MenuAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.yalantis.contextmenu.lib 2 | 3 | import android.animation.Animator 4 | import android.animation.AnimatorSet 5 | import android.content.Context 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import android.widget.LinearLayout 9 | import com.yalantis.contextmenu.lib.extensions.* 10 | 11 | open class MenuAdapter( 12 | private val context: Context, 13 | private val menuWrapper: LinearLayout, 14 | private val textWrapper: LinearLayout, 15 | private val menuObjects: List, 16 | private val actionBarSize: Int, 17 | private val gravity: MenuGravity 18 | ) { 19 | 20 | var onCloseOutsideClickListener: (view: View) -> Unit = {} 21 | var onItemClickListener: (view: View) -> Unit = {} 22 | var onItemLongClickListener: (view: View) -> Unit = {} 23 | private var onItemClickListenerCalled: (view: View) -> Unit = {} 24 | private var onItemLongClickListenerCalled: (view: View) -> Unit = {} 25 | 26 | private var clickedView: View? = null 27 | 28 | private val hideMenuAnimatorSet: AnimatorSet by lazy { setOpenCloseAnimation(true) } 29 | private val showMenuAnimatorSet: AnimatorSet by lazy { setOpenCloseAnimation(false) } 30 | 31 | private var isMenuOpen = false 32 | private var isAnimationRun = false 33 | 34 | private var animationDuration = MenuParams.ANIMATION_DURATION 35 | 36 | private val itemClickListener = View.OnClickListener { view -> 37 | onItemClickListenerCalled = onItemClickListener 38 | viewClicked(view) 39 | } 40 | 41 | private val itemLongClickListener = View.OnLongClickListener { view -> 42 | onItemLongClickListenerCalled = onItemLongClickListener 43 | viewClicked(view) 44 | true 45 | } 46 | 47 | init { 48 | setViews() 49 | } 50 | 51 | open fun setAnimationDuration(duration: Long) { 52 | animationDuration = duration 53 | showMenuAnimatorSet.duration = animationDuration 54 | hideMenuAnimatorSet.duration = animationDuration 55 | } 56 | 57 | fun menuToggle() { 58 | if (!isAnimationRun) { 59 | resetAnimations() 60 | isAnimationRun = true 61 | 62 | if (isMenuOpen) { 63 | hideMenuAnimatorSet.start() 64 | } else { 65 | showMenuAnimatorSet.start() 66 | } 67 | 68 | toggleIsMenuOpen() 69 | } 70 | } 71 | 72 | fun closeOutside() { 73 | onItemClickListenerCalled = onCloseOutsideClickListener 74 | menuWrapper.getChildAt(FIRST_CHILD_INDEX)?.let { 75 | viewClicked(it) 76 | } 77 | } 78 | 79 | fun getItemCount() = menuObjects.size 80 | 81 | private fun getLastItemPosition() = getItemCount() - 1 82 | 83 | /** 84 | * Creating views and filling to wrappers 85 | */ 86 | private fun setViews() { 87 | menuObjects.forEachIndexed { index, menuObject -> 88 | context.apply { 89 | textWrapper.addView( 90 | getItemTextView( 91 | menuObject, 92 | actionBarSize, 93 | itemClickListener, 94 | itemLongClickListener 95 | ) 96 | ) 97 | menuWrapper.addView( 98 | getImageWrapper( 99 | menuObject, 100 | actionBarSize, 101 | itemClickListener, 102 | itemLongClickListener, 103 | index != getLastItemPosition() 104 | ) 105 | ) 106 | } 107 | } 108 | } 109 | 110 | /** 111 | * Set starting params to vertical animations 112 | */ 113 | private fun resetVerticalAnimation(view: View, toTop: Boolean) { 114 | view.apply { 115 | if (!isMenuOpen) { 116 | rotation = ROTATION_ZERO_DEGREES 117 | rotationY = ROTATION_ZERO_DEGREES 118 | rotationX = -ROTATION_NINETY_DEGREES 119 | } 120 | 121 | pivotX = (actionBarSize / 2).toFloat() 122 | pivotY = (if (!toTop) 0 else actionBarSize).toFloat() 123 | } 124 | } 125 | 126 | /** 127 | * Set starting params to side animations 128 | */ 129 | private fun resetSideAnimation(view: View) { 130 | view.apply { 131 | if (!isMenuOpen) { 132 | rotation = ROTATION_ZERO_DEGREES 133 | rotationY = this@MenuAdapter.getRotationY() 134 | rotationX = ROTATION_ZERO_DEGREES 135 | } 136 | 137 | pivotX = this@MenuAdapter.getPivotX() 138 | pivotY = (actionBarSize / 2).toFloat() 139 | } 140 | } 141 | 142 | private fun getRotationY() = 143 | when (gravity) { 144 | MenuGravity.END -> if (context.isLayoutDirectionRtl()) { 145 | ROTATION_NINETY_DEGREES 146 | } else { 147 | -ROTATION_NINETY_DEGREES 148 | } 149 | MenuGravity.START -> if (context.isLayoutDirectionRtl()) { 150 | -ROTATION_NINETY_DEGREES 151 | } else { 152 | ROTATION_NINETY_DEGREES 153 | } 154 | } 155 | 156 | private fun getPivotX() = 157 | when (gravity) { 158 | MenuGravity.END -> if (context.isLayoutDirectionRtl()) { 159 | ROTATION_ZERO_DEGREES 160 | } else { 161 | actionBarSize.toFloat() 162 | } 163 | MenuGravity.START -> if (context.isLayoutDirectionRtl()) { 164 | actionBarSize.toFloat() 165 | } else { 166 | ROTATION_ZERO_DEGREES 167 | } 168 | } 169 | 170 | /** 171 | * Set starting params to text animations 172 | */ 173 | private fun resetTextAnimation(view: View) { 174 | view.apply { 175 | alpha = if (!isMenuOpen) ALPHA_INVISIBLE else ALPHA_VISIBLE 176 | translationX = if (!isMenuOpen) actionBarSize.toFloat() else TRANSLATION_ZERO_VALUE 177 | } 178 | } 179 | 180 | /** 181 | * Set starting params to all animations 182 | */ 183 | private fun resetAnimations() { 184 | for (i in 0 until getItemCount()) { 185 | resetTextAnimation(textWrapper.getChildAt(i)) 186 | 187 | if (i == 0) { 188 | resetSideAnimation(menuWrapper.getChildAt(i)) 189 | } else { 190 | resetVerticalAnimation(menuWrapper.getChildAt(i), false) 191 | } 192 | } 193 | } 194 | 195 | /** 196 | * Creates open/close AnimatorSet 197 | */ 198 | private fun setOpenCloseAnimation(isCloseAnimation: Boolean): AnimatorSet { 199 | val textAnimations = mutableListOf() 200 | val imageAnimations = mutableListOf() 201 | 202 | if (isCloseAnimation) { 203 | for (i in getLastItemPosition() downTo 0) { 204 | fillOpenClosingAnimations(true, textAnimations, imageAnimations, i) 205 | } 206 | } else { 207 | for (i in 0 until getItemCount()) { 208 | fillOpenClosingAnimations(false, textAnimations, imageAnimations, i) 209 | } 210 | } 211 | 212 | return AnimatorSet().apply { 213 | duration = animationDuration 214 | startDelay = 0 215 | interpolator = HesitateInterpolator() 216 | 217 | playTogether( 218 | AnimatorSet().apply { playSequentially(textAnimations) }, 219 | AnimatorSet().apply { playSequentially(imageAnimations) } 220 | ) 221 | onAnimationEnd { toggleIsAnimationRun() } 222 | } 223 | } 224 | 225 | /** 226 | * Filling arrays of animations to build Set of Closing / Opening animations 227 | */ 228 | private fun fillOpenClosingAnimations( 229 | isCloseAnimation: Boolean, 230 | textAnimations: MutableList, 231 | imageAnimations: MutableList, 232 | wrapperPosition: Int 233 | ) { 234 | textWrapper.getChildAt(wrapperPosition).apply { 235 | textAnimations.add(AnimatorSet().apply { 236 | val textAppearance = if (isCloseAnimation) { 237 | alphaDisappear() 238 | } else { 239 | alphaAppear() 240 | } 241 | 242 | val textTranslation = if (isCloseAnimation) { 243 | when (gravity) { 244 | MenuGravity.END -> translationEnd(getTextEndTranslation()) 245 | MenuGravity.START -> translationStart(getTextEndTranslation()) 246 | } 247 | } else { 248 | when (gravity) { 249 | MenuGravity.END -> translationStart(getTextEndTranslation()) 250 | MenuGravity.START -> translationEnd(getTextEndTranslation()) 251 | } 252 | } 253 | 254 | playTogether(textAppearance, textTranslation) 255 | }) 256 | } 257 | 258 | menuWrapper.getChildAt(wrapperPosition).apply { 259 | imageAnimations.add( 260 | if (isCloseAnimation) { 261 | if (wrapperPosition == 0) { 262 | rotationCloseHorizontal(gravity) 263 | } else { 264 | rotationCloseVertical() 265 | } 266 | } else { 267 | if (wrapperPosition == 0) { 268 | rotationOpenHorizontal(gravity) 269 | } else { 270 | rotationOpenVertical() 271 | } 272 | } 273 | ) 274 | } 275 | } 276 | 277 | private fun viewClicked(view: View) { 278 | if (isMenuOpen && !isAnimationRun) { 279 | clickedView = view 280 | 281 | val childIndex = (view.parent as ViewGroup).indexOfChild(view) 282 | if (childIndex == -1) { 283 | return 284 | } 285 | 286 | toggleIsAnimationRun() 287 | buildChosenAnimation(childIndex) 288 | toggleIsMenuOpen() 289 | } 290 | } 291 | 292 | private fun buildChosenAnimation(childIndex: Int) { 293 | val fadeOutTextTopAnimatorList = mutableListOf() 294 | val closeToBottomImageAnimatorList = mutableListOf() 295 | val fadeOutTextBottomAnimatorList = mutableListOf() 296 | val closeToTopImageAnimatorList = mutableListOf() 297 | 298 | fillAnimatorLists( 299 | childIndex, 300 | fadeOutTextTopAnimatorList, 301 | closeToBottomImageAnimatorList, 302 | fadeOutTextBottomAnimatorList, 303 | closeToTopImageAnimatorList 304 | ) 305 | 306 | resetSideAnimation(menuWrapper.getChildAt(childIndex)) 307 | 308 | val fullAnimatorSetPair = getFullAnimatorSetPair( 309 | childIndex, 310 | fadeOutTextTopAnimatorList, 311 | closeToBottomImageAnimatorList, 312 | fadeOutTextBottomAnimatorList, 313 | closeToTopImageAnimatorList 314 | ) 315 | 316 | AnimatorSet().apply { 317 | duration = animationDuration 318 | interpolator = HesitateInterpolator() 319 | playTogether(fullAnimatorSetPair.first, fullAnimatorSetPair.second) 320 | start() 321 | } 322 | } 323 | 324 | private fun fillAnimatorLists( 325 | childIndex: Int, 326 | fadeOutTextTopAnimatorList: MutableList, 327 | closeToBottomImageAnimatorList: MutableList, 328 | fadeOutTextBottomAnimatorList: MutableList, 329 | closeToTopImageAnimatorList: MutableList 330 | ) { 331 | for (i in 0..getLastItemPosition()) { 332 | val menuWrapperChild = menuWrapper.getChildAt(i) 333 | val menuWrapperChildRotation = menuWrapperChild.rotationCloseVertical() 334 | val textWrapperChildFadeOut = 335 | textWrapper.getChildAt(i).fadeOutSet(getTextEndTranslation(), gravity) 336 | 337 | when (i) { 338 | in 0 until childIndex -> { 339 | resetVerticalAnimation(menuWrapperChild, true) 340 | closeToBottomImageAnimatorList.add(menuWrapperChildRotation) 341 | fadeOutTextTopAnimatorList.add(textWrapperChildFadeOut) 342 | } 343 | in childIndex + 1..getLastItemPosition() -> { 344 | resetVerticalAnimation(menuWrapperChild, false) 345 | closeToTopImageAnimatorList.add(menuWrapperChildRotation) 346 | fadeOutTextBottomAnimatorList.add(textWrapperChildFadeOut) 347 | } 348 | } 349 | } 350 | 351 | closeToTopImageAnimatorList.reverse() 352 | fadeOutTextBottomAnimatorList.reverse() 353 | } 354 | 355 | private fun getFullAnimatorSetPair( 356 | childIndex: Int, 357 | fadeOutTextTopAnimatorList: MutableList, 358 | closeToBottomImageAnimatorList: MutableList, 359 | fadeOutTextBottomAnimatorList: MutableList, 360 | closeToTopImageAnimatorList: MutableList 361 | ): Pair { 362 | val closeToBottom = AnimatorSet() 363 | closeToBottom.playSequentially(closeToBottomImageAnimatorList) 364 | val fadeOutTop = AnimatorSet() 365 | fadeOutTop.playSequentially(fadeOutTextTopAnimatorList) 366 | 367 | val closeToTop = AnimatorSet() 368 | closeToTop.playSequentially(closeToTopImageAnimatorList) 369 | val fadeOutBottom = AnimatorSet() 370 | fadeOutBottom.playSequentially(fadeOutTextBottomAnimatorList) 371 | 372 | val closeToEnd = menuWrapper.getChildAt(childIndex).rotationCloseHorizontal(gravity) 373 | closeToEnd.onAnimationEnd { 374 | toggleIsAnimationRun() 375 | 376 | clickedView?.let { notNullView -> 377 | onItemClickListenerCalled(notNullView) 378 | onItemLongClickListenerCalled(notNullView) 379 | } 380 | } 381 | val fadeOutChosenText = 382 | textWrapper.getChildAt(childIndex).fadeOutSet(getTextEndTranslation(), gravity) 383 | 384 | val imageFullAnimatorSet = AnimatorSet() 385 | imageFullAnimatorSet.play(closeToBottom).with(closeToTop) 386 | val textFullAnimatorSet = AnimatorSet() 387 | textFullAnimatorSet.play(fadeOutTop).with(fadeOutBottom) 388 | 389 | if (closeToBottomImageAnimatorList.size >= closeToTopImageAnimatorList.size) { 390 | imageFullAnimatorSet.play(closeToBottom).before(closeToEnd) 391 | textFullAnimatorSet.play(fadeOutTop).before(fadeOutChosenText) 392 | } else { 393 | imageFullAnimatorSet.play(closeToTop).before(closeToEnd) 394 | textFullAnimatorSet.play(fadeOutBottom).before(fadeOutChosenText) 395 | } 396 | 397 | return Pair(imageFullAnimatorSet, textFullAnimatorSet) 398 | } 399 | 400 | private fun getTextEndTranslation() = 401 | context.getDimension(R.dimen.text_translation).toFloat() 402 | 403 | private fun toggleIsAnimationRun() { 404 | isAnimationRun = !isAnimationRun 405 | } 406 | 407 | private fun toggleIsMenuOpen() { 408 | isMenuOpen = !isMenuOpen 409 | } 410 | 411 | companion object { 412 | 413 | private const val FIRST_CHILD_INDEX = 0 414 | } 415 | } --------------------------------------------------------------------------------