Skip to content

Commit

Permalink
set menu resource dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed Apr 25, 2020
1 parent 88341e7 commit 3377c03
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ allprojects {
}
dependencies {
implementation 'com.github.ibrahimsn98:SmoothBottomBar:1.7.4'
implementation 'com.github.ibrahimsn98:SmoothBottomBar:1.7.5'
}
```

Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "me.ibrahimsn.smoothbottombar"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -27,13 +27,13 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation project(":lib")

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.61'
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.2'
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
9 changes: 4 additions & 5 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28

compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

Expand All @@ -29,8 +28,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
testImplementation 'junit:junit:4.12'
implementation 'androidx.core:core-ktx:1.2.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package me.ibrahimsn.lib
import android.os.Bundle
import android.view.Menu
import androidx.annotation.IdRes
import androidx.core.view.size
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.ui.NavigationUI
Expand All @@ -26,28 +25,32 @@ class NavigationComponentHelper {
return NavigationUI.onNavDestinationSelected(menu.getItem(pos), navController)
}
})

val weakReference = WeakReference(smoothBottomBar)

navController.addOnDestinationChangedListener(object :
NavController.OnDestinationChangedListener {

override fun onDestinationChanged(
controller: NavController,
destination: NavDestination,
arguments: Bundle?
) {
val view = weakReference.get()

if (view == null) {
navController.removeOnDestinationChangedListener(this)
return
}
for (h in 0 until menu.size) {

for (h in 0 until menu.size()) {
val menuItem = menu.getItem(h)
if (matchDestination(destination, menuItem.itemId)) {
menuItem.isChecked = true
smoothBottomBar.setActiveItem(h)
}
}
}

})
}

Expand Down
11 changes: 11 additions & 0 deletions lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import android.view.MotionEvent
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.annotation.FontRes
import androidx.annotation.MenuRes
import androidx.annotation.XmlRes
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.navigation.NavController
Expand Down Expand Up @@ -244,6 +246,15 @@ class SmoothBottomBar : View {
return true
}

fun setMenuRes(@MenuRes menuRes: Int, activeItem: Int? = null) {
items = BottomBarParser(context, menuRes).parse()
invalidate()

activeItem?.let {
setActiveItem(it)
}
}

fun setActiveItem(pos: Int) {
activeItemIndex = pos

Expand Down

0 comments on commit 3377c03

Please sign in to comment.