NavigationDrawer with Dynamic Modules


Navigation Drawer with Dynamic Modules

Output Screen



Project Structure:



HomeFragment.java
package com.example.mynavigation.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mynavigation.CustomAdapter;
import com.example.mynavigation.Module;
import com.example.mynavigation.R;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView rec = view.findViewById(R.id.myrec);
dummyModule();
rec.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rec.setAdapter(new CustomAdapter(list));
}

List<Module> list = new ArrayList<>();

private void dummyModule() {
Module m1 = new Module();
m1.setName("Profile");
m1.setImage(R.drawable.ic_menu_share);

Module m2 = new Module();
m2.setName("Gallery");
m2.setImage(R.drawable.ic_menu_gallery);

Module m3 = new Module();
m3.setName("Slider");
m3.setImage(R.drawable.ic_menu_camera);

Module m4 = new Module();
m4.setName("Logout");
m4.setImage(R.drawable.ic_menu_manage);

Module m5 = new Module();
m5.setName("Share");
m5.setImage(R.drawable.ic_menu_send);

list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
list.add(m5);
}
}

HomeViewModel.java
package com.example.mynavigation.ui.home;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class HomeViewModel extends ViewModel {

private MutableLiveData<String> mText;

public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}

public LiveData<String> getText() {
return mText;
}
}

CustomAdapter.java
package com.example.mynavigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyView> {
List<Module> list;

public CustomAdapter(List<Module> list) {
this.list = list;
}

@NonNull
@Override
public MyView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,
        parent, false);
return new MyView(view);
}

@Override
public void onBindViewHolder(@NonNull MyView holder, int position) {
Module m = list.get(position);
holder.im.setImageResource(m.getImage());
holder.tname.setText(m.getName());
}

@Override
public int getItemCount() {
return list.size();
}

class MyView extends RecyclerView.ViewHolder {
ImageView im;
TextView tname;

public MyView(@NonNull View itemView) {
super(itemView);
tname = itemView.findViewById(R.id.mytext);
im = itemView.findViewById(R.id.myimage);
}
}

}

Module.java
package com.example.mynavigation;

public class Module {
private String name;
private Integer image;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getImage() {
return image;
}

public void setImage(Integer image) {
this.image = image;
}
}

fragment_home.xml
<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"
android:orientation="vertical">

<androidx.cardview.widget.CardView
android:id="@+id/myhead"
android:layout_width="match_parent"
android:layout_height="130dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="5dp"
app:cardMaxElevation="10dp"
app:cardUseCompatPadding="true">

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

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="20dp"
android:src="@drawable/teacher" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Deep Singh"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="deep@gmail.com"
android:textColor="#fff"
android:typeface="serif" />

</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myrec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myhead" />

</RelativeLayout>

item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">

<ImageView
android:id="@+id/myimage"
android:layout_width="50dp"
android:src="@drawable/std"
android:layout_gravity="center"
android:layout_height="50dp" />

<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deep"
android:textStyle="bold"
android:layout_gravity="center"/>

</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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<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:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
tools:context=".MainActivity">

<include layout="@layout/content_main" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">

<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml
<resources>
<string name="app_name">MyNavigation</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_title">Android Studio</string>
<string name="nav_header_subtitle">android.studio@android.com</string>
<string name="nav_header_desc">Navigation header</string>
<string name="action_settings">Settings</string>

<string name="menu_home">Home</string>
<string name="menu_gallery">Gallery</string>
<string name="menu_slideshow">Slideshow</string>
<string name="menu_profile">Profile</string>
</resources>


No comments: