Custom Bottom Navigation Bar || Chip Navigation bar in Android

 

Custom Bottom Navigation Bar

..

 in Main Activity Xml Code...

  



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:weightSum="1"
tools:context=".activities.MainActivity">

<FrameLayout
android:id="@+id/frame_layout"
android:layout_weight="0.9"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Hello World!" />


<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="@+id/navigation"
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="0dp"
app:cnb_menuResource="@menu/bottom_menu"
app:cnb_unselectedColor="@color/black"
app:cnb_orientationMode="horizontal"
app:cnb_addBottomInset="false"
app:cnb_addLeftInset="false"
app:cnb_addRightInset="false"
app:cnb_addTopInset="false"
app:cnb_radius="30dp"
app:cnb_iconSize="24dp"
app:cnb_animationDuration="175"/>

</LinearLayout>

 

 Bottom Menu File...

<?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/nav_home"
android:icon="@drawable/baseline_home_24"
android:title="Home"
app:cnb_backgroundColor="@color/red"
app:cnb_iconColor="@color/white"
app:cnb_textColor="@color/white" />

<item
android:id="@+id/nav_settings"
android:icon="@drawable/settings"
android:title="Settings"
app:cnb_backgroundColor="@color/red"
app:cnb_iconColor="@color/white"
app:cnb_textColor="@color/white" />

<item
android:id="@+id/nav_dashboard"
android:icon="@drawable/dashboard"
android:title="Dashboard"
app:cnb_backgroundColor="@color/red"
app:cnb_iconColor="@color/white"
app:cnb_textColor="@color/white" />

<item
android:id="@+id/nav_profile"
android:icon="@drawable/baseline_person_2_24"
android:title="Profile"
app:cnb_backgroundColor="@color/red"
app:cnb_iconColor="@color/white"
app:cnb_textColor="@color/white" />


</menu>

 .... Then Create Fragments As Your Need....


Then Go to Main Activity..

package com.daizzyinfosys.onlyleaningpurpose.activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;

import com.daizzyinfosys.onlyleaningpurpose.R;
import com.daizzyinfosys.onlyleaningpurpose.databinding.ActivityMainBinding;
import com.daizzyinfosys.onlyleaningpurpose.fragments.HomeFragment;
import com.ismaeldivita.chipnavigation.ChipNavigationBar;

public class MainActivity extends AppCompatActivity {



ActivityMainBinding binding;
ChipNavigationBar navigation;

@Override
protected void onPostResume() {
navigation.setItemSelected(R.id.nav_home,true);
openFragment(new HomeFragment());
super.onPostResume();
}

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

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

navigation=findViewById(R.id.navigation);

navigation.setItemSelected(R.id.nav_home,true);


navigation.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {
@Override
public void onItemSelected(int item) {

int id = item;


if(id==R.id.nav_home) {

openFragment(new HomeFragment());

}else if(id==R.id.nav_dashboard) {

Intent i = new Intent(MainActivity.this, Dashboard.class);
startActivity(i);


}else if(id==R.id.nav_profile) {

Intent i = new Intent(MainActivity.this, Profile.class);
startActivity(i);


}else if(id==R.id.nav_settings) {

Intent i = new Intent(MainActivity.this, Setting.class);
startActivity(i);


}

}
});


}


public void openFragment(Fragment fragment){

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.frame_layout,fragment);
ft.addToBackStack(null);
ft.commit();

}


@Override
public void onBackPressed() {

openFragment(new HomeFragment());
}
}

 

 

 

in Gradle file...

plugins {
id 'com.android.application'
}

android {
namespace 'com.daizzyinfosys.onlyleaningpurpose'
compileSdk 33

defaultConfig {
applicationId "com.daizzyinfosys.onlyleaningpurpose"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
viewBinding true
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

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

//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:3.6.0'


}

 in Gradle Settings...


pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
rootProject.name = "OnlyLeaningPurpose"
include ':app'

Comments

Popular posts from this blog

API

How to get latitude longitude simply in Android using Address

Manifest