Skip to content

Instantly share code, notes, and snippets.

@sharifulislam52
Last active March 2, 2018 09:22
Show Gist options
  • Save sharifulislam52/3f6fc67f6aaa3518cae8fb2f28fc5cc5 to your computer and use it in GitHub Desktop.
Save sharifulislam52/3f6fc67f6aaa3518cae8fb2f28fc5cc5 to your computer and use it in GitHub Desktop.
public class your_activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout myDrawerLayout;
private ActionBarDrawerToggle myToggle;
NavigationView navigationView;
DrawerLayout mydrawer;
//-----> into onCreate method
navigationView = (NavigationView) findViewById(R.id.nav_view_id);
navigationView.setNavigationItemSelectedListener(this);
mydrawer = (DrawerLayout) findViewById(R.id.drawer);
// call method
nav_menu_method();
dynamically_generate_nav_menu_item();
//<----- into onCreate method
//-----> all about navigation menu
public void nav_menu_method(){
myDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout_id);
myToggle = new ActionBarDrawerToggle(your_activity.this,myDrawerLayout,R.string.open_string,R.string.close_string);
myDrawerLayout.addDrawerListener(myToggle);
myToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Toggle button color
myToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.color_name));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (myToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
//<----- all about navigation menu
//-----> dynamically generate navigation menu item
public void dynamically_generate_nav_menu_item(){
String items_array[] = {"item1","item2","item3"};
Menu the_nav_menu = navigationView.getMenu();
for (int i=0; i<items_array.length; i++){
MenuItem menu_items = the_nav_menu.add(R.id.group_items, i, Menu.NONE, items_array[i]);
menu_items.setIcon(R.drawable.item_icon);
menu_items.getIcon().setColorFilter(ContextCompat.getColor(your_activity.this, R.color.color_name), PorterDuff.Mode.SRC_IN);
}
}
//<----- dynamically generate navigation menu item
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.normal_item1:
mydrawer.closeDrawer(GravityCompat.START); // it will close nav after click item
// do something
break;
case R.id.normal_item2:
mydrawer.closeDrawer(GravityCompat.START);
// do something
break;
case R.id.normal_item3:
mydrawer.closeDrawer(GravityCompat.START);
// do something
break;
case 0:
mydrawer.closeDrawer(GravityCompat.START);
// do something for dynamic 1st item
break;
case 1:
mydrawer.closeDrawer(GravityCompat.START);
// do something for dynamic 2nd item
break;
case 2:
mydrawer.closeDrawer(GravityCompat.START);
// do something for dynamic 3rd item
break;
default:
break;
}
//mydrawer.closeDrawer(GravityCompat.START);
return true;
}
}
// your_activity, myDrawerLayout, myToggle, navigationView, nav_view_id, drawer_layout_id, open_string, close_string, color_name, items_array, the_nav_menu, menu_items, item_icon, id, mydrawer, normal_item
// import it into dependencies
implementation 'com.android.support:design:26.1.0'
// design version should be same to appcompat.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="nav_header_height">176dp</dimen>
</resources>
<!-- 176dp -->
<?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="@dimen/nav_header_height"
android:background="@drawable/nav_header_image_or_file"
android:gravity="bottom"
android:orientation="vertical">
</LinearLayout>
<!-- nav_header_height, nav_header_image_or_file -->
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:id="@+id/normal_item"
android:checkableBehavior="none"
android:orderInCategory="0">
<item
android:id="@+id/normal_item1"
android:icon="@drawable/item_icon1"
android:title="Refresh"/>
<item
android:id="@+id/normal_item2"
android:icon="@drawable/item_icon2"
android:title="Notes"/>
<item
android:id="@+id/normal_item3"
android:icon="@drawable/item_icon3"
android:title="Bookmarked"/>
</group>
<item app:showAsAction="always">
<menu
android:id="@+id/group_items"
android:checkableBehavior="none"
android:orderInCategory="1">
<!-- all dynamic item will come here -->
</menu>
</item>
</menu>
<!-- normal_item, normal_item, item_icon, group_items -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_custom_toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/only_activity"
android:orientation="vertical"
android:visibility="visible"
android:padding="10dp">
<!-- What we want to show on our activity -->
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_layout"
app:menu="@menu/nav_menu"
android:background="#ffffff">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
<!-- drawer_layout_id, app_custom_toolbar, only_activity, nav_view_id, nav_header_layout, nav_menu, #ffffff -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment