Skip to content

Commit

Permalink
remove traditional callback interface, refactor code, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed Nov 4, 2019
1 parent 5e9e16c commit 960cc86
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 109 deletions.
113 changes: 64 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

# NiceBottomBar

A lightweight Android material bottom navigation bar library

[![](https://jitpack.io/v/ibrahimsn98/NiceBottomBar.svg)](https://jitpack.io/#ibrahimsn98/NiceBottomBar)
[![API](https://img.shields.io/badge/API-22%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=22)
[![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16)

## GIF

<img src="https://github.com/ibrahimsn98/NiceBottomBar/blob/master/art/gif.gif?raw=true"/>

## Usage

- Create menu.xml under your res/menu/ folder
```xml
<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -36,72 +39,81 @@ A lightweight Android material bottom navigation bar library

</menu>
```


- Add view into your layout file
```xml
<me.ibrahimsn.lib.NiceBottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="@menu/bottom_menu" />
<me.ibrahimsn.lib.NiceBottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="@menu/bottom_menu" />
```
- Use NiceBottomBar functions in your activity
```kotlin
bottomBar.setActiveItem(1)
bottomBar.setBadge(2)
bottomBar.removeBadge(2)

bottomBar.setBottomBarCallback(object: NiceBottomBar.BottomBarCallback {

override fun onItemLongClick(pos: Int) {

}

override fun onItemSelect(pos: Int) {
- Use NiceBottomBar functions in your activity
```kotlin
bottomBar.setActiveItem(1)
bottomBar.setBadge(2)
bottomBar.removeBadge(2)

}
bottomBar.onItemSelected = {
status.text = "Item $it selected"
}

override fun onItemReselect(pos: Int) {
bottomBar.onItemReselected = {
status.text = "Item $it re-selected"
}

}
})
bottomBar.onItemLongClick = {
status.text = "Item $it long click"
}
```


## Customization

```xml
<me.ibrahimsn.lib.NiceBottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="@menu/bottom_menu"
app:backgroundColor=""
app:indicatorEnabled=""
app:indicatorColor=""
app:indicatorWidth=""
app:indicatorGravity=""
app:itemFontFamily=""
app:textColor=""
app:textColorActive=""
app:textSize=""
app:iconSize=""
app:iconMargin=""
app:activeItem=""
app:badgeColor=""
app:indicatorInterpolator="anticipateOvershoot" />
<me.ibrahimsn.lib.NiceBottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="@menu/bottom_menu"
app:backgroundColor=""
app:indicatorEnabled=""
app:indicatorColor=""
app:indicatorWidth=""
app:indicatorGravity=""
app:itemFontFamily=""
app:textColor=""
app:textColorActive=""
app:textSize=""
app:iconSize=""
app:iconMargin=""
app:activeItem=""
app:badgeColor=""
app:indicatorInterpolator="anticipateOvershoot" />
```


## Setup

```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.ibrahimsn98:NiceBottomBar:2.1'
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.ibrahimsn98:NiceBottomBar:2.2'
}
```


## License

```
MIT License
Expand All @@ -125,3 +137,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```


> Follow me on Twitter [@ibrahimsn98](https://twitter.com/ibrahimsn98)
19 changes: 9 additions & 10 deletions app/src/main/java/me/ibrahimsn/nicebottombar/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ class MainActivity : AppCompatActivity() {
bottomBar.setBadge(2)
bottomBar.removeBadge(2)

bottomBar.setBottomBarCallback(object: NiceBottomBar.BottomBarCallback {
override fun onItemLongClick(pos: Int) {
bottomBar.removeBadge(pos)
}
override fun onItemSelect(pos: Int) {
bottomBar.onItemSelected = {
status.text = "Item $it selected"
}

}
override fun onItemReselect(pos: Int) {
bottomBar.setBadge(pos)
}
bottomBar.onItemReselected = {
status.text = "Item $it re-selected"
}

})
bottomBar.onItemLongClick = {
status.text = "Item $it long click"
}
}
}
13 changes: 12 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/bottomBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<me.ibrahimsn.lib.NiceBottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
Expand Down
47 changes: 26 additions & 21 deletions lib/src/main/java/me/ibrahimsn/lib/BottomBarParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,44 @@ import android.content.Context
import android.content.res.XmlResourceParser
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import org.xmlpull.v1.XmlPullParserException

class BottomBarParser(private val context: Context, res: Int) {

private val parser: XmlResourceParser = context.resources.getXml(res)
private val items: ArrayList<BottomBarItem> = arrayListOf()

fun parse(): ArrayList<BottomBarItem> {
try {
var eventType: Int?
do {
eventType = parser.next()
if (eventType == XmlResourceParser.START_TAG && "item" == parser.name) {
items.add(getTabConfig(parser))
}
} while (eventType != XmlResourceParser.END_DOCUMENT)
} catch (e: XmlPullParserException) {
e.printStackTrace()
throw Exception()
}
return items

fun parse(): List<BottomBarItem> {
val items: MutableList<BottomBarItem> = mutableListOf()
var eventType: Int?

do {
eventType = parser.next()

if (eventType == XmlResourceParser.START_TAG && parser.name == Constants.ITEM_TAG)
items.add(getTabConfig(parser))

} while (eventType != XmlResourceParser.END_DOCUMENT)

return items.toList()
}

private fun getTabConfig(parser: XmlResourceParser): BottomBarItem {
val attributeCount = parser.attributeCount
var itemText: String? = null
var itemDrawable: Drawable? = null
for (i in 0 until attributeCount) {

for (i in 0 until attributeCount)
when (parser.getAttributeName(i)) {
"title" -> itemText = context.getString(parser.getAttributeResourceValue(i, 0))
"icon" -> itemDrawable = ContextCompat.getDrawable(context, parser.getAttributeResourceValue(i, 0))!!
Constants.ICON_ATTRIBUTE -> itemDrawable =
ContextCompat.getDrawable(context, parser.getAttributeResourceValue(i, 0))
Constants.TITLE_ATTRIBUTE -> {
itemText = try {
context.getString(parser.getAttributeResourceValue(i, 0))
} catch (e: Exception) {
parser.getAttributeValue(i)
}
}
}
}

return BottomBarItem(itemText!!, itemDrawable!!)
}
}
15 changes: 15 additions & 0 deletions lib/src/main/java/me/ibrahimsn/lib/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.ibrahimsn.lib

/**
* This is a static class to hold constants
*/
object Constants {
const val ITEM_TAG = "item"
const val ICON_ATTRIBUTE = "icon"
const val TITLE_ATTRIBUTE = "title"
const val WHITE_COLOR_HEX = "#FFFFFF"

const val DEFAULT_INDICATOR_COLOR = "#426dfe"
const val DEFAULT_TEXT_COLOR = "#444444"
const val DEFAULT_TEXT_COLOR_ACTIVE = "#426dfe"
}
Loading

0 comments on commit 960cc86

Please sign in to comment.