android – Modify the activity_main_drawer.xml from an external server xml

Question:

I have this activity_main_drawer.xml file inside res/menu/ folder

<?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/nav_inicio"
            android:icon="@mipmap/ic_home"
            android:title="@string/inicio" />
    </group>
</menu>

What I am trying to do is modify it, based on an xml that I get from an external server. Or instead of modifying it, use the external xml , instead of this for the android side menu

EDIT

The server xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<zonas>
    <zona>
        <idZona>1</idZona>
        <nombre idZona="1">Home</nombre>
    </zona>
    <zona>
        <idZona>2</idZona>
        <nombre idZona="2">Pantalla 1</nombre>
    </zona>
    <zona>
        <idZona>3</idZona>
        <nombre idZona="3">Pantalla 2</nombre>
    </zona>
    <zona>
        <idZona>4</idZona>
        <nombre idZona="4">Pantalla 3</nombre>
    </zona>
</zonas>

And I read it through Parser SAX

Answer:

If I'm wrong, you can change the elements of the Navigation Drawer in the OnCreate() method of your Activity . At first I can think of several options:

Option 1 : change the menu to another that exists in the resources. Example:

 navigationView.getMenu().clear(); //Borrar los elementos anteriores.
 navigationView.inflateMenu(R.menu.new_navigation_drawer_items);

Option 2 : change the menu by adding menu items by hand using the menu's own functions add () and addSubMenu () . Example:

 Menu menu = navigationView.getMenu();
 for (int i = 1; i <= 3; i++) {
     menu.add("Menu "+ i);
 }

 SubMenu subMenu = menu.addSubMenu("SubMenu");
 for (int i = 1; i <= 2; i++) {
     subMenu.add("SubMenu " + i);
 }
Scroll to Top