Share this post on:

Let see how to make Side Navigation Bar in Android Studio. Side Navigation is used in Application by which you can move to another activity. Here I will use java Code to Declare Navigation bar in Android studio.

Step 1: Create Menu

For menu you need to go to your Resourse file and Create Android Resourse Directory Folder name it as menu. Then create new menuResourseFile like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_categories"
android:icon="@drawable/categories"
android:title="@string/all_categories" />
</group>
<item android:title="">
<menu>
<group android:checkableBehavior="single">

<item
android:id="@+id/nav_order"
android:icon="@drawable/my_order"
android:title="@string/my_order" />
<item
android:id="@+id/nav_history"
android:icon="@drawable/history"
android:title="@string/order_history" />
<item
android:id="@+id/nav_cart"
android:icon="@drawable/navigation_cart"
android:title="@string/my_cart" />

<item
android:id="@+id/nav_wishlist"
android:icon="@drawable/whishlist"
android:title="@string/wishlist" />
<item
android:id="@+id/nav_myaccount"
android:icon="@drawable/my_account"
android:title="@string/my_account" />
<item
android:id="@+id/nav_chat"
android:icon="@drawable/my_chat"
android:title="@string/my_chat" />
</group>
</menu>
</item>
<item android:title="">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_privacypolicy"
android:title="@string/privacy_policy" />
<item
android:id="@+id/nav_share"
android:title="@string/share" />
</group>
</menu>
</item>
</menu>

Step 2 : Now Create Header of Navigation bar in layout folder:

<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/head_navigation_design"
android:paddingBottom="20dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:orientation="vertical"
android:gravity="bottom">

<ImageView
android:id="@+id/imageView"
android:layout_width="120dp"
android:layout_height="100dp"
android:paddingTop="20dp"
android:layout_gravity="center"
android:src="@drawable/userprofileimage"
android:layout_marginTop="20dp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:id="@+id/username"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:text="Mohit Kumar"/>

<!-- <TextView-->
<!-- android:id="@+id/textView"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:paddingTop="@dimen/nav_header_vertical_spacing"-->
<!-- android:text="Rank : Diamond Manager"-->
<!-- android:textAppearance="@style/TextAppearance.AppCompat.Body1"-->
<!-- android:textColor="#070707"-->
<!-- android:textSize="10sp"-->
<!-- android:textStyle="bold"/>-->
</LinearLayout>

Step 3: Now you need to Get those menu and header in you main Activity where you want to show Navigation bar:

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/sidenavigation_view"
app:headerLayout="@layout/header_navigation_layout"
app:menu="@menu/side_navigation"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>

Step 4 : Finally You need to give the intensions to the menu item that where you want to move when there will be a click on that menu item, This should be declare in you main activity java class.

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener  {

    NavigationView navigationView;
    

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

        navigationView=findViewById(R.id.sidenavigation_view);
        
        navigationView.bringToFront();

        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,
                R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);


    }

    @Override
    public void onBackPressed() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START))
        {
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else
        {
            super.onBackPressed();
        }

        if (backPressedTime + 2000>System.currentTimeMillis())
        {
            backToast.cancel();
            super.onBackPressed();
            return;
        }
        else
        {
            backToast= StyleableToast.makeText(this,"Press back again to exit",R.style.exampletoast);
//            backToast=Toast.makeText(this, "Press back Again to exit", Toast.LENGTH_SHORT);
            backToast.show();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuitem) {

        switch(menuitem.getItemId()){
            case R.id.nav_categories:
                startActivity(new Intent(getApplicationContext(),Categories_Activity.class));
                break;
            case  R.id.nav_history:
                startActivity(new Intent(getApplicationContext(), OrderHistory_Activity.class));
                break;
            case R.id.nav_order:
                startActivity(new Intent(getApplicationContext(),MyOrder_Activity.class));
                break;
            case R.id.nav_cart:
                 startActivity(new Intent(getApplicationContext(),Cart_Actitivity.class));
                break;

            case R.id.nav_wishlist:
                startActivity(new Intent(getApplicationContext(),MyWishList.class));
                break;

            case R.id.nav_myaccount:
                startActivity(new Intent(getApplicationContext(),MyAccount.class));
                break;

            case R.id.nav_chat:
                startActivity(new Intent(getApplicationContext(),MyChat_Activity.class));
                break;

            case R.id.nav_privacypolicy:
                startActivity(new Intent(getApplicationContext(),Matching_Status.class));
                finish();
                break;
            case R.id.nav_share:
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                String body="Download App: https://play.google.com/store/apps/details?id=com.rntech.shopping";
                intent.putExtra(Intent.EXTRA_TEXT,body);

                startActivity(Intent.createChooser(intent,"Share Using"));
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

}

Now Navigation bar is declared in you Activity.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

One Comment

  1. Avatar Raiyan

    Khalid khan

    I was searching on the internet about side navigation, there was a lot of solution available but none of them were so clear. You have provided the exact solution for it. Thank you

Leave a Comment

Your email address will not be published. Required fields are marked *