CLICK2CODE

first you learn then you earn

In this article, we can create a Navigation drawer manually and you can also customize yourself. So, before proceeding steps you must watch our YouTube Video CLICK HERE.

STEP: 01

You can create copy below codes under string.xml file in your project.

<resources>
<string name="app_name">Quotes App</string>

<string name="close">Close</string>
<string name="open">Open</string>

<string name="facebook_page">Follow on Facebook</string>
<string name="instagram">Follow on Instagram</string>
<string name="youtube_channel">Visit Our Channel</string>
<string name="communication">Communication</string>
<string name="nav_rate">Rate or Feedback</string>
<string name="nav_share">Share</string>
<string name="nav_privacy_policy">Privacy Policy</string>
<string name="nav_terms_conditions">Terms and Conditions</string>
</resources>
STEP: 02

Now, you can create an “Android Resource Directory” and select the root element “menu” after that right-click on the menu folder and create a Menu Resource File named “navigation_menu“. In this, you can paste below all codes in your project.

<?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/facebookPage"
android:icon="@drawable/facebook"
android:title="@string/facebook_page" />
<item
android:id="@+id/instagramPage"
android:icon="@drawable/ic_instagram"
android:title="@string/instagram" />
<item
android:id="@+id/youtube"
android:icon="@drawable/youtube"
android:title="@string/youtube_channel" />


</group>

<item android:title="@string/communication">

<menu>

<item
android:id="@+id/nav_rate"
android:icon="@drawable/rate"
android:title="@string/nav_rate" />

<item
android:id="@+id/nav_share"
android:icon="@drawable/share"
android:title="@string/nav_share" />

<item
android:id="@+id/nav_privacy_policy"
android:icon="@drawable/privacy"
android:title="@string/nav_privacy_policy" />

<item
android:id="@+id/nav_terms_conditions"
android:icon="@drawable/terms"
android:title="@string/nav_terms_conditions" />

</menu>
</item>


</menu>
STEP: 03

Now, you can create a nav_header.xml under layout in your project. And you can copy below codes and paste in your project.

<?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="146dp"
android:background="@color/colorPrimaryDark"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/icon"
app:civ_border_color="#FFFFFF"
app:civ_border_width="2dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/open_sans_semibold"
android:gravity="center_horizontal"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white"
android:textStyle="bold" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/open_sans"
android:gravity="center_horizontal"
android:text="nilkanthpawan@gmail.com"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@android:color/white" />


</androidx.appcompat.widget.LinearLayoutCompat>
STEP: 04

Now, you can create some code in “activity_home.xml” and all codes are given below.

<?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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
tools:openDrawer="start">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/app_name" />

</LinearLayout>

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


</androidx.drawerlayout.widget.DrawerLayout>
STEP: 05

Now, you can open your “HomeActivity.java” file and copy the below codes follow step by step.

NavigationView navigationView;
ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
Toolbar toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);

navigationView = findViewById(R.id.navigation_menu);
navigationView.setItemIconTintList(null);
drawerLayout = findViewById(R.id.drawer);

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.youtube:
drawerLayout.closeDrawer(GravityCompat.START);
 Intent youtube = new Intent(HomeActivity.this, YouTube.class);
 startActivity(youtube);
 break;

 case R.id.facebookPage:
drawerLayout.closeDrawer(GravityCompat.START);
 Intent facebook = new Intent(HomeActivity.this, Facebook.class);
 startActivity(facebook);
 break;

 case R.id.instagramPage:
drawerLayout.closeDrawer(GravityCompat.START);
 Intent instagram = new Intent(HomeActivity.this, Instagram.class);
 startActivity(instagram);
 break;

 case R.id.nav_rate:
drawerLayout.closeDrawer(GravityCompat.START);
 try {
startActivity(new Intent(Intent.ACTION_VIEW,
 Uri.parse("market://details?id=" + getPackageName())));

 } catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
 Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
 }
break;

 case R.id.nav_share:
drawerLayout.closeDrawer(GravityCompat.START);
 Intent myIntent = new Intent(Intent.ACTION_SEND);
 myIntent.setType("text/plain");
 String shareBody = "Hindi Shayari = get all type of quotes \n" + "https://play.google.com/store/apps/details?id=com.click2code.rationcarddetailsjharkhand&hl=en";
 String shareSub = "Your Subject Here";
 myIntent.putExtra(Intent.EXTRA_SUBJECT, shareSub);
 myIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
 startActivity(Intent.createChooser(myIntent, "Share using"));
 break;

 case R.id.nav_privacy_policy:
drawerLayout.closeDrawer(GravityCompat.START);
 Intent privacy = new Intent(HomeActivity.this, PrivacyPolicy.class);
 startActivity(privacy);
 break;

 case R.id.nav_terms_conditions:
drawerLayout.closeDrawer(GravityCompat.START);
 Intent terms = new Intent(HomeActivity.this, TermsConditions.class);
 startActivity(terms);
 break;
 }
return true;
 }
});

Finally, you can paste all the above code in your project. So, now you can “RUN” this project with your real device.

!! THANK YOU !!

Leave a Reply

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