Drawer Navigation

Circle Imageview dependencies
implementation("de.hdodenhof:circleimageview:3.1.0")
drawer_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/nav_setting"
android:icon="@drawable/ic_setting"
android:title="Setting" />
<item
android:id="@+id/nav_delete"
android:icon="@drawable/ic_delete"
android:title="Delete" />
<item
android:id="@+id/nav_account"
android:icon="@drawable/ic_account"
android:title="Account" />
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_share"
android:title="Share" />
<item
android:id="@+id/nav_exit"
android:icon="@drawable/ic_exit"
android:title="Exit" />
</group>
</menu>
drawer_navigation_header.xml
<?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="@color/colorPrimary"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/userImage"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher_background"
app:civ_border_color="@color/red"
app:civ_border_width="2dp" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:textColor="@color/white"
android:textSize="18sp" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/userEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[email protected]"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main.xml
<?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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity">

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

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?actionBarSize">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:elevation="5dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>


<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigationView"
android:fitsSystemWindows="false"
android:layout_gravity="start"
app:menu="@menu/drawer_navigation"
app:headerLayout="@layout/drawer_navigation_header"/>

</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.java
                        package in.hindicode.example;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawerLayout;
private Toolbar toolbar;
private FrameLayout frameLayout;
private NavigationView navigationView;

private CircleImageView userImage;
private AppCompatTextView userName;
private AppCompatTextView userEmail;


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

drawerLayout = findViewById(R.id.drawerLayout);
toolbar = findViewById(R.id.toolbar);
frameLayout = findViewById(R.id.frameLayout);
navigationView = findViewById(R.id.navigationView);

setSupportActionBar(toolbar);

navigationView.setNavigationItemSelectedListener(this);

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


if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new SettingFragment()).commit();
navigationView.setCheckedItem(R.id.nav_setting);
}

setupNavigationHeader();

}

private void setupNavigationHeader() {
userImage = navigationView.getHeaderView(0).findViewById(R.id.userImage);
userName = navigationView.getHeaderView(0).findViewById(R.id.userName);
userEmail = navigationView.getHeaderView(0).findViewById(R.id.userEmail);

userName.setText("Hindicode");
userEmail.setText("[email protected]");
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();

if (id == R.id.nav_home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_setting) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new SettingFragment()).commit();
} else if (id == R.id.nav_delete) {
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_account) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new AccountFragment()).commit();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_exit) {
finish();
System.exit(0);
}
drawerLayout.closeDrawer(GravityCompat.START);

return true;
}
}

Fragments xml files

fragment_account.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:gravity="center"
android:orientation="vertical"
tools:context=".AccountFragment">

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Account Fragment"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>
fragment_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue_dark"
android:gravity="center"
android:orientation="vertical"
tools:context=".SettingFragment">

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Setting Fragment"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>

Fragments Java files

AccountFragment.java
package in.hindicode.example;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class AccountFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_account, container, false);
}
}
SettingFragment.java
package in.hindicode.example;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SettingFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_setting, container, false);
}
}

Video for Drawer Navigation

Article By: Brajlal Prasad
Created on: 09 Oct 2024  217  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store