How to make custom drawable navigation in an android studio
This post shows you how to make a simple drawable navigation bar in an android studio
We will implement a navigation drawer in an android studio that
most used navigation drawable in an android application.
When a user clicks on the hamburger icon at that time drawer navigation bar show and clicks on blank space automatically hide drawer navigation.
We will add some links.
Create a new project. Whatever you name it.
activity_main.xml
Main_activity.java
Now Create New xml file named
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer" tools:openDrawer="start" tools:context=".MainActivity"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbar" app:title=" Drawer Navigation " app:titleTextColor="#0ff" android:background="@color/teal_700" /> </RelativeLayout> <com.google.android.material.navigation.NavigationView android:layout_width="250sp" android:background="@color/trans" android:layout_height="match_parent" android:id="@+id/navmenu" app:menu="@menu/menu" app:headerLayout="@layout/navheader" android:layout_gravity="start"/> </androidx.drawerlayout.widget.DrawerLayout>
package com.pd.myapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.transition.Slide; import android.os.Bundle; import android.view.MenuItem; import android.widget.Toast; import com.google.android.material.navigation.NavigationView; public class MainActivity extends AppCompatActivity { NavigationView nav; ActionBarDrawerToggle toggle; DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); nav=(NavigationView)findViewById(R.id.navmenu); drawerLayout=(DrawerLayout)findViewById(R.id.drawer); toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close); drawerLayout.addDrawerListener(toggle); toggle.syncState(); nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_home : Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_LONG).show(); drawerLayout.closeDrawer(GravityCompat.START); break; case R.id.menu_share : Toast.makeText(getApplicationContext(),"Share your friends",Toast.LENGTH_LONG).show(); drawerLayout.closeDrawer(GravityCompat.START); break; case R.id.menu_setting : Toast.makeText(getApplicationContext(),"Setting",Toast.LENGTH_LONG).show(); drawerLayout.closeDrawer(GravityCompat.START); break; } return true; } }); } }
navheader.xml
Create a menu folder
In menu folder, create
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/purple_200" android:orientation="vertical"> <ImageView android:layout_marginTop="30dp" android:layout_gravity="center" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_background" /> <TextView android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0ff" android:textSize="20dp" android:text="Hello Word!"/> </androidx.appcompat.widget.LinearLayoutCompat>
menu.xml
SUBSCRIBE to download
Download Source code
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_home" android:icon="@drawable/ic_baseline_home_24" android:title="Home" /> <item android:id="@+id/menu_share" android:icon="@drawable/ic_baseline_share_24" android:title="share"/> <item android:id="@+id/menu_setting" android:icon="@drawable/ic_baseline_settings_24" android:title="Setting"/> </menu>
Download Source code