Code Monkey home page Code Monkey logo

chip-navigation-bar's Introduction

Release

Chip Navigation Bar

A navigation bar widget inspired on Google Bottom Navigation mixed with Chips component.

Usage

<!-- bottom_menu.xml -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/home"
        android:icon="@drawable/ic_home"
        android:title="Home"
        app:cnb_iconColor="@color/home"/>
    <item
        android:id="@+id/activity"
        android:icon="@drawable/ic_activity"
        android:title="Activity"
        app:cnb_iconColor="@color/activity"/>
    <item
        android:id="@+id/favorites"
        android:icon="@drawable/ic_heart"
        android:title="Favorites"
        app:cnb_iconColor="@color/favorites" />
    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_settings"
        android:title="Settings"
        app:cnb_iconColor="@color/settings" />
</menu>

<!-- layout.xml -->
<com.ismaeldivita.chipnavigation.ChipNavigationBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/white"
    app:cnb_menuResource="@menu/bottom_menu" />

Vertical orientation

ChipNavigationBar supports a vertical orientation mode. This is very useful for tablets or devices with large screens.

Just add the attribute cnb_orientationMode to your xml:

<com.ismaeldivita.chipnavigation.ChipNavigationBar
    android:id="@+id/bottom_menu"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:cnb_menuResource="@menu/test"
    app:cnb_orientationMode="vertical" />

... or programmatically call the method setMenuOrientation before inflate the menu:

menu.setMenuOrientation(MenuOrientation.VERTICAL)
menu.setMenuResource(R.menu.my_menu)

Note: The view exposes methods to expand and collapse the menu but we don't provide the implementation for the toggle button. Check the sample for a basic implementation.


Badges

The library supports badges on the menu items.

menu.showBadge(R.id.menu_home) 
menu.showBadge(R.id.menu_activity, 8)
menu.showBadge(R.id.menu_favorites, 88)
menu.showBadge(R.id.settings, 10000)

XML custom attributes

MenuItem xml custom attributes

attribute description default
android:id id required
android:enabled enabled state true
android:icon icon drawable required
android:title label string required
cnb_iconColor color used to tint the icon on selected state R.attr.colorAccent
cnb_iconTintMode PorterDuff.Mode to apply the color. Possible values: [src_over, src_in, src_atop, multiply, screen] null
cnb_textColor color used for the label on selected state same color used for cnb_iconColor
cnb_backgroundColor color used for the chip background same color used for cnb_iconColor with 15% alpha
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/home"
        android:enabled="true"
        android:icon="@drawable/ic_home"
        android:title="@string/home"
        app:cnb_backgroundColor="@color/home_chip"
        app:cnb_iconColor="@color/home_icon"
        app:cnb_iconTintMode="src_in"
        app:cnb_textColor="@color/home_label" />

        ...

</menu>

ChipNavigationBar xml custom attributes

attribute description default
cnb_menuResource menu resource file optional since you can set this programmatically
cnb_orientationMode menu orientation. Possible values: [horizontal, vertical] horizontal
cnb_addBottomInset property to enable the sum of the window insets on the current bottom padding, useful when you're using the translucent navigation bar false
cnb_addTopInset property to enable the sum of the window insets on the current bottom padding, useful when you're using the translucent status bar with the vertical mode false
cnb_addLeftInset property to enable the sum of the window insets on the current start padding, useful when you're using the translucent navigation bar with landscape false
cnb_addRightInset property to enable the sum of the window insets on the current end padding, useful when you're using the translucent navigation bar with landscape false
cnb_minExpandedWidth minimum width for vertical menu when expanded 0
cnb_unselectedColor color used for unselected state #696969
cnb_badgeColor color used for the badge #F44336
cnb_radius radius used on the background Float.MAX_VALUE fully rounded
cnb_iconSize menu item icon size 24dp
cnb_textAppearance menu item text appearance theme default
cnb_animationDuration animation duration theme default
<com.ismaeldivita.chipnavigation.ChipNavigationBar
    android:id="@+id/menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cnb_menuResource="@menu/bottom_menu"
    app:cnb_orientationMode="horizontal"
    app:cnb_addBottomInset="false"
    app:cnb_addLeftInset="false"
    app:cnb_addRightInset="false"
    app:cnb_addTopInset="false"
    app:cnb_unselectedColor="@color/my_menu_unselected_color"
    app:cnb_badgeColor="@color/my_menu_badge_color"
    app:cnb_radius="8dp"
    app:cnb_iconSize="24dp"
    app:cnb_textAppearance="?myThemeTextAppearance"
    app:cnb_animationDuration="175">

Public API

method description
setMenuResource(@MenuRes menuRes: Int) Inflate a menu from the specified XML resource
setMenuOrientation(menuOrientation: MenuOrientation) Set the menu orientation
setItemEnabled(id: Int, isEnabled: Boolean) Set the enabled state for the menu item with the provided [id]
setItemSelected(id: Int) Remove the selected state from the current item and set the selected state to true for the menu item with the [id]
setOnItemSelectedListener(listener: OnItemSelectedListener) Register a callback to be invoked when a menu item is selected
collapse() Collapse the menu items if orientationMode is VERTICAL otherwise, do nothing
expand() Expand the menu items if orientationMode is VERTICAL otherwise, do nothing
showBadge(id: Int) Display a numberless badge for the menu item with the [id]
showBadge(id: Int, count: Int) Display a countable badge with for the menu item with the [id]
setDuration(duration: Long) Set a custom animation duration

Installation

Required

Gradle

Make sure that the repositories section includes Jitpack

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the library to the dependencies:

implementation 'com.github.ismaeldivita:chip-navigation-bar:1.4.0'

Note: For projects without kotlin, you may need to add org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion to your dependencies since this is a Kotlin library.

License

MIT License

Copyright (c) 2019 Ismael Di Vita

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 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.

chip-navigation-bar's People

Contributors

dh-46 avatar dhina17 avatar ismaeldivita avatar jagaryousef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chip-navigation-bar's Issues

Expand & Collapse animation

Thanks for a great library.

I am tring to achiving something similar to like this.

I have implemented it but navigation bar didn't animated.

Can you please help me out.

Thanks

Add Real fragments

How to Add fragments instead of colors
when click on chip i want to add fragment in container like that

or add fragments in arraylist and pass to chip navigation bar
ArrayList fragList = new ArrayList<>();
fragList.add(new firstFragement());
4 more.

please look in to it

SetItemSelected has no effect in Java

I am trying to init it with a view pager so I need to set the selected item when fragments are selected in the view pager but mbn.setItemSelected(0, true); has no effect and no items are selected.

Button Error

When I run the application it works with no error. I have 4 buttons in my chip navigation bar. When I click on the first three buttons it doesn't click on the button that I clicked, it automatically clicks the last button. How can I fix that bug? (I'm working with Java)

W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@fa524d7
W/System: A resource failed to call close.
W/System: A resource failed to call close.
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
I/chatty: uid=10086(com.example.quitter) RenderThread identical 2 lines
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@f4f37fa
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)
I/chatty: uid=10086(com.example.quitter) RenderThread identical 1 line
D/EGL_emulation: eglMakeCurrent: 0xe6f85300: ver 2 0 (tinfo 0xe6f832c0)

AAPT: error: duplicate value for resource 'attr/pivotX' with config ''.

I am implementing the current last version 1.3.3 but get this error

 AAPT: error: duplicate value for resource 'attr/pivotX' with config ''.

It is basically because it uses the beta version of the constraint layout 2.0.0beta6 so when I reduce it to the stable version 1.1.3 some packages that are used by this library are not there yet! e.g. androidx.constraintlayout.helper

SetOnItemSelectedListener() being called twice on clicking once.

So when I am clicking on one item once, the fragment matching with the clicked item id is being called twice. I don't think this is happening because of my code.

bottomBar is the chip-navigation-bar

binding.bottomBar.setOnItemSelectedListener { id -> when (id) { R.id.home1 -> { setFragment(HomeFragment1()) } R.id.home2 -> { setFragment(HomeFragment2()) } R.id.home3 -> { setFragment(HomeFragment3()) } } }

problem with layout when clicked a item menu

my mainactivity.java code is
package com.asus.eins;

import androidx.annotation.MenuRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.asus.eins.Fragment.Home;
import com.ismaeldivita.chipnavigation.ChipNavigationBar;

import java.util.ArrayList;
import java.util.List;

public class Dashboard extends AppCompatActivity {

ChipNavigationBar chipNavigationBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    chipNavigationBar = findViewById(R.id.menu);

    chipNavigationBar.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {

        @Override
        public void onItemSelected(int id) {
            switch (id) {
                case R.id.home:
                    chipNavigationBar.setMenuResource(R.layout.fragment_home);
                    break;
            }
        }
    });
}

}

and my mainactivity.xml is

<com.ismaeldivita.chipnavigation.ChipNavigationBar
    android:id="@+id/menu"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:background="#fff"
    android:elevation="16dp"
    android:gravity="center"
    android:padding="8dp"
    app:cnb_menuResource="@menu/bottom_menu" >

</com.ismaeldivita.chipnavigation.ChipNavigationBar>

i set on item selected with my fragment
but when i clicked a item menu my app is force close and show a error expecting menu, got relative layout

issue regarding the ".setOnItemSelectedListener"

"java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ismaeldivita.chipnavigation.ChipNavigationBar.setOnItemSelectedListener(com.ismaeldivita.chipnavigation.ChipNavigationBar$OnItemSelectedListener)' on a null object reference"

Unable to Install in my Java Project

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'

I have imported project successfully
And yet cant use app:cnb_menuResource in my XML file.

Android max API 30
min API level 23

Sync with Gradle and cleared caches and restarted no use.

Kotlin cannot access kotlin.jvm.functions.Function1 when calling kotlin function with java lambda

    chipNavigationBar.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {
        @Override
        public void onItemSelected(int i) {

            if (i==R.id.menu_home) {

                LoadFrag(new HomeFrag());


            } else if (i == R.id.menu_fav) {


                LoadFrag(new FavFrag());


            }


        }
    });

while useing above event iam geting error as below during compiling

error: cannot access Function1
class file for kotlin.jvm.functions.Function1 not found

Can't use on my java project

Hi! I have tried to add it to my java project. I've added both dependencies, but I can't use the "app:cnb...." atributes on my XML files. How can it be solved?
Thanks

change the menutext and icon

Is there any way to change the text and icon of a menu item programmatically? For eg:if the user has a store. need to show the store image and store name on fourth item

Item Menu not working in kotlin new version with view binding

click Menu not working please how to fix this issue

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity">

    <FrameLayout
        android:id="@+id/fragmentLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomMenu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.ismaeldivita.chipnavigation.ChipNavigationBar
        android:id="@+id/bottomMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cnb_menuResource="@menu/bottom_menu"
        app:cnb_orientationMode="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin

class MainActivity : AppCompatActivity() {
    
    private val binding : ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupFragment(PlanningFragment())
        setupNavigation()
    }

    private fun setupNavigation() {
        with(binding) {
            bottomMenu.setItemSelected(R.id.home)
            bottomMenu.setOnItemSelectedListener {
                when (it) {
                    R.id.menu_planing -> setupFragment(PlanningFragment())
                    R.id.menu_notes -> setupFragment(NotesFragment())
                    R.id.menu_link -> setupFragment(LinkFragment())
                }
            }
        }
    }

    private fun setupFragment(fragment : Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.fragmentLayout, fragment)
        transaction.commit()
    }
}

How to change Icon when selected?

I already do selector.xml for each icon and i program each icon for true and false android:state_checked. but it didn't work. did anyone have the solution?

menu items cannot be deselected.

chipNavigationBar.setItemSelected(getSelectedItemId, false)

trying to use this method to deselect a particular item . But it doesn't work

OnItemSelctedListener Java

I am not sure what I have to write in the OnItemSelctedListener in Java can you send me an Example sir?

This is a Part of my Current Code what do it Need to Change? i want to open an New Actvity on Click Item

public class MainActivity extends AppCompatActivity {

ChipNavigationBar chipNavigationBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_test);

    chipNavigationBar = findViewById(R.id.menu);

    chipNavigationBar.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {

        @Override
        public void onItemSelected(int id) {
           Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_SHORT).show();
        }
    });

}

Unresolved class error

when i use the dependencies, the "tools:context" field of every layout file shows error.
i am using it with java so used this dependencies:

implementation 'com.ismaeldivita.chipnavigation:chip-navigation-bar:1.3.2'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'

the error shows " Unresolved class 'class_name' ".
please help me with this.

Java compatibility

Hello @ismaeldivita

Please make this library for java compatible, so we can use in java project also. It is very good looking working is there.

Thanks

langauge issues

please provide this in java so that this library can be used for java project. it is not working in java project .

Propper support to NavController

val navController = findNavController(R.id.nav_host_fragment)
binding.bottomNav.setupWithNavController(navController)
binding.bottomNav.setOnNavigationItemReselectedListener {/* NO-OP */ }

Set default selected item

I need to set one of the items as default selected when the view is shown. Right now all items are unselected. How can I make this?

Active menu

I have following menu file but android:enabled="true" is not working (I mean by default when I open my app it is not active)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/home"
        android:icon="@drawable/abc_vector_test"
        android:title="Home"
        android:enabled="true"
        app:cnb_iconColor="@color/home" />
    <item
        android:id="@+id/activity"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/services"
        app:cnb_iconColor="@color/services" />
    <item
        android:id="@+id/favorites"
        android:icon="@android:drawable/arrow_down_float"
        android:title="@string/orders"
        app:cnb_iconColor="@color/orders" />
    <item
        android:id="@+id/settings"
        android:icon="@android:drawable/arrow_up_float"
        android:title="@string/settings"
        app:cnb_iconColor="@color/settings" />
</menu>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.