Search code examples
androidandroid-menuandroid-drawer

How to call OnClick method for right Drawer menu in Android?


I want use two Drawer Menu, one of this menus in left and another one of this menus in right. left Drawer Menu is ok, when items clicked, the operation is performed. but in right Drawer Menu, when click on items the operation does not!
My XML code :

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tb="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 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"
        android:background="@color/background_color"
        tools:context=".Main_Page">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/viewpagertab">

        </android.support.v4.view.ViewPager>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/main_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header_menu_app"
        app:itemIconTint="@color/post_card_color"
        app:itemTextColor="@color/post_card_color"
        app:menu="@menu/menu_main__page" />

    <android.support.design.widget.NavigationView
        android:id="@+id/main_drawer2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        app:headerLayout="@layout/header_drawer_main"
        app:itemIconTint="@color/post_card_color"
        app:itemTextColor="@color/post_card_color"
        app:menu="@menu/menu_main_profile" />

</android.support.v4.widget.DrawerLayout>

My JAVA code :

public class Main_Page extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main__page);

    // Left DraweLayout Open
        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
        mNaviView = (NavigationView) findViewById(R.id.main_drawer);
        mNaviView2 = (NavigationView) findViewById(R.id.main_drawer2);
        mNaviView.setNavigationItemSelectedListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (con.isOnline()) {
                    new get_menu_info().execute(public_username);
                }
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }
        };
        Drawer.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

    // Right DraweLayout Open
        ImageView profile_menu = (ImageView) toolbar.findViewById(R.id.right_tool);
        profile_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Drawer.openDrawer(mNaviView2);
            }
        });
}

  @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        Intent intent;

        if (menuItem.getItemId() == R.id.menu_action_mySMS) {
            intent = new Intent(this, MySMS_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_profile_action_favorite) {
            intent = new Intent(this, Fav_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_category) {
            intent = new Intent(this, category_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_setting) {
            intent = new Intent(this, setting_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_about) {
            intent = new Intent(this, about_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_top) {
            intent = new Intent(this, top_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_free_com) {
            intent = new Intent(this, freeCon_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            return true;
        }
        return false;
    }
}

Please help me to call right menu items when click on.
tnx <3


Solution

  • Try this code :

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
         if (item.getItemId() == R.id.menuitemid) {
            if(mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                            mDrawerLayout.closeDrawer(Gravity.LEFT); // set Gravity as per your need
                        }else{
                            mDrawerLayout.openDrawer(Gravity.LEFT); // set Gravity as per your need
                        }      
    
        }else if (item.getItemId()== R.id.action_settings) {
            return true;
        } 
        return super.onOptionsItemSelected(item);
    }
    

    Hope it will work.