Search code examples
androidandroid-fragmentsnavigation-drawerandroid-navigationandroid-navigationview

How to implement the method onNavigationItemSelected?


I have created a navigation drawer activity. I want to create a fragment that is opened when the respective item in the drawer list is clicked.

How can I implement this so as to show an image when clicking on respective item?


Solution

  • Let me share a simple and complete code to work with Navigation Drawer. The MainActivity class.

    public class MainActivity extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener{
    
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        navigationView = (NavigationView) findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(this);
    
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle =
                new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) {
                    @Override
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                    }
    
                    @Override
                    public void onDrawerOpened(View drawerView){
                        super.onDrawerOpened(drawerView);
                    }
                };
    
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
    
        actionBarDrawerToggle.syncState();
    
    }
    
    @Override
    public boolean onNavigationItemSelected(MenuItem item){
    
        int id = item.getItemId();
    
        switch (id){
            case R.id.item1:
               /* getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new MySimpleFragment())
                        .addToBackStack(null)
                        .commit();*/
                Intent intent = new Intent(this, Class1.class);
                startActivity(intent);
                break;
            case R.id.item2:
                intent = new Intent(this, Class2.class);
                startActivity(intent);
                break;
            case R.id.item3:
                intent = new Intent(this, Class3.class);
                startActivity(intent);
                break;
            default:
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
    

    }

    activity_main.xml file looks like this :

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:padding="3dp"
        tools:context=".MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            android:background="@android:color/transparent"/>
    
        <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/container"
            android:layout_below="@id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            tools:context=".MainActivity"
            tools:ignore="MergeRootFrame"
            android:background="@drawable/jackson">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textStyle="italic"
                android:padding="30dp"
                android:layout_marginLeft="50dp"
                android:textSize="15dp"
                android:layout_marginTop="40dp"
                android:textColor="@color/red"
                android:text="Just Beat It"/>
        </FrameLayout>
    
    </RelativeLayout>
    
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/drawer"
        app:itemIconTint="@color/colorAccent"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer">
    </android.support.design.widget.NavigationView>
    

    And, the layout for header.xml file is

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:background="@drawable/header"
        android:orientation="vertical">
    
        <ImageView
            android:layout_width="76dp"
            android:layout_height="76dp"
            android:id="@+id/profile_image"
            android:layout_marginLeft="54dp"
            android:layout_marginTop="50dp"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:src="@drawable/myphoto_cutmypic"/>
    
        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/profile_image"
            android:layout_marginLeft="54dp"
            android:textStyle="bold"
            android:paddingTop="10dp"
            android:textColor="@color/red"
            android:text="@string/my_name"/>
        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt1"
            android:textStyle="bold"
            android:paddingTop="10dp"
            android:layout_marginLeft="54dp"
            android:textColor="@color/red"
            android:text="@string/my_email"/>
    
    </RelativeLayout>
    

    And, the menu/drawer.xml file looks like :

    <?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/item1"
                android:checked="false"
                android:icon="@drawable/ic_call1"
                android:title="item1"/>
    
            <item
                android:id="@+id/item2"
                android:checked="false"
                android:icon="@drawable/ic_call2"
                android:title="item2"/>
    
            <item
                android:id="@+id/item3"
                android:checked="false"
                android:icon="@drawable/ic_call3"
                android:title="item3"/>
    
        </group>
    </menu>
    

    If you have any doubts, reply to this answer