Drawerlayout - control visibility

Asked

Viewed 117 times

1

When the app runs the first Drawer displayed is correct; it only comes with a visible option item. This option when selected activates an Activity that is running normally but at the end of its execution the other options are not visible.

drawer_layout

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single"
        android:id="@+id/drawer_register" android:visible="true">
        <item
            android:id="@+id/nav_drawer_register"
            android:title="@string/drawer_register"
            android:icon="@drawable/ic_person_add_black_24dp" />
    </group>

    <item android:title="@string/drawer_app"
        android:id="@+id/drawer_app" android:visible="false">
        <menu>
            <item
                android:id="@+id/nav_drawer_app_login"
                android:title="@string/drawer_app_login"
                android:icon="@drawable/ic_fingerprint_black_24dp" />
            <item
                android:id="@+id/nav_drawer_app_logout"
                android:title="@string/drawer_app_logout"
                android:icon="@drawable/ic_exit_to_app_black_24dp" />
        </menu>
    </item>

</menu> 

Main Activity

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar,
            R.string.main_navigation_drawer_open,
            R.string.main_navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
    findViewById(R.id.nav_view);
    navigationView.getMenu().clear();
    navigationView.inflateMenu(R.menu.activity_main_teste);
    navigationView.setNavigationItemSelectedListener(this);
    drawer.openDrawer(GravityCompat.START);

Listener

public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    // ================================================================================== //
    //                                FUNCTION REGISTER ME                                //
    // ================================================================================== //

    if (id == R.id.nav_drawer_register) {
        Toast toast =
                Toast.makeText(this,
                        R.string.device_register_warning, Toast.LENGTH_SHORT);
        toast.show();
        userType = "0"; 
        Intent intent = new Intent(RegisterStatusListActivity.ACTION_REG_STATUS_DATA);
        intent.addCategory(RegisterStatusListActivity.CATEGORY_REG_STATUS_DATA);
        intent.putExtra(RegisterStatusListActivity.EXTRA_REG_TYPE, userType);
        startActivity(intent);
        setAllVisible(true);       // <===== ATIVA MUDANÇA DE VISIBILIDADE
        return true;
    } else

setAllVisible

public void setAllVisible(boolean visible){

        NavigationView navigationView = (NavigationView) 
        findViewById(R.id.nav_view);
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.activity_main_teste);
        toggleVisibility(navigationView.getMenu(), R.id.drawer_app, visible);
    }

    private void toggleVisibility(Menu menu, @IdRes int id, boolean visible){
        menu.findItem(id).setVisible(visible);
}

SUMMARY

At the end of Activity that processed the first selected option Drawer continues with the other invisible options. This has solution?

  • The correct thing would be to create in one, and only change visibility when the user made the access, if he does not, the options remain invisible.

1 answer

1

Make two menus and inflate them according to need, whether you are logged in or not.

Example:

Create these two menus in the menu directory

menu_logged.xml

   <?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_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_login"
            android:icon="@drawable/ic_menu_login"
            android:title="Login" />
    </group>


</menu>

menu_deslogado.xml

    <?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_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_menu_logout"
            android:title="Logout" />
    </group>

</menu>

Ai in java, tu inflate them with an if, thus

Navigationview navigationView = (Navigationview) findViewById(R.id.nav_view);

if(islogin)
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.menu_logado);
    } else
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.menu_deslogado);
  • Leonardo good morning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.