Custom Drawer Layout
Activity main XML
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFBFA">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/menuIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/menu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fontFamily="@font/kalpurush"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFBFA"
app:headerLayout="@layout/heading_layout" />
Headin Layout XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/head_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFBFA">
<androidx.cardview.widget.CardView
android:id="@+id/circleImageCard"
android:layout_width="92dp"
android:layout_height="92dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="45dp"
app:cardElevation="4dp">
<ImageView
android:id="@+id/circleImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/logo" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/appName"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/circleImageCard"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/kalpurush"
android:padding="5dp"
android:text="Your App Name"
android:textSize="22sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appName"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:fontFamily="@font/kalpurush"
android:gravity="center_vertical"
android:padding="5dp"
android:textColor="@color/black"
android:text="Version 1.0"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="@+id/setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/head_card"
>
<ImageView
android:id="@+id/setting_icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/settings" />
<TextView
android:id="@+id/setting_tv"
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="Settings"
android:textSize="17sp" />
</LinearLayout>
<!-- lin 1 end -->
<LinearLayout
android:id="@+id/moreApp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/setting"
>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/more" />
<TextView
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="More App"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/fbPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/moreApp">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/fb_icon" />
<TextView
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="Fb Page"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/aboutUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/fbPage">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/aboutus" />
<TextView
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="About Us"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/rateUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/aboutUs">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/star_rate" />
<TextView
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="Rate Us"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/shareApp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/rateUs">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/share" />
<TextView
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="Share App"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/privacy_lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="15dp"
android:layout_below="@+id/shareApp"
>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:src="@drawable/policy" />
<TextView
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:fontFamily="@font/kalpurush"
android:text="Privacy Policy"
android:textSize="17sp" />
</LinearLayout>
</RelativeLayout>
Main Activity Java Code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
ImageView menuIcon = findViewById(R.id.menuIcon);
NavigationView nav_view = findViewById(R.id.nav_view);
Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.close, R.string.open);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
menuIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
// navigation view theke header layout er item gulake get kora hoise
View headerView = nav_view.getHeaderView(0);
// drawer layout er item ke poricoi koriye dewa hoiyese
LinearLayout string = headerView.findViewById(R.id.setting);
// drawer layout er item gular set Onclick toiri kora hoise
string.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
Toast.makeText(MainActivity.this, "Setting Clicked", Toast.LENGTH_SHORT).show();
}
});
} // on create teg end here
}
Circle Background Code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#0195FF"/>
<padding android:bottom="10dp"/>
<padding android:left="10dp"/>
<padding android:right="10dp"/>
<padding android:top="10dp"/>
</shape>