1

I created an android navigation drawer and I want a header Image to be clickable. I checked many resource but not able to make it work.

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        View headerView = navigationView.getHeaderView(0);
        ImageView profileView = headerView.findViewById(R.id.imageView);
        profileView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Header is clicked", Toast.LENGTH_SHORT).show();
                Log.e("Header", "Header is clicked");
            }
        });
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
}

and nav_header_main.xml file contains the ImageView(imageView) inside the LinearView

Here I am not getting the toast or any Logs from inside the OnClickListener.

Thanks.

updated activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>
raulsi
  • 41
  • 1
  • 5
  • Does the rest of the `NavigationView` work as it should? That is, do the menu items navigate as expected? Please post the `Activity` and header layouts. – Mike M. May 14 '20 at 00:40
  • no that is also not changing I added the `onNavigationItemSelected` function in ManiActivity but not receiving any event there also. @MikeM. – raulsi May 14 '20 at 00:58
  • Yeah, it sounds like an issue in the layout. Please post `activity_main`. – Mike M. May 14 '20 at 01:00
  • @MikeM. added the `activity_main` – raulsi May 14 '20 at 01:10
  • 2
    The drawer in a `` must be listed last in order to receive touch events properly. Move the `` to after the ``. – Mike M. May 14 '20 at 01:17
  • @MikeM. Thanks a lot that worked I was stuck for 1 day, it was so stupid error. Thanks again – raulsi May 14 '20 at 01:19
  • 1
    No problem. If you didn't arrange that layout like that yourself, your IDE might have done it automatically, in alphabetical order. If you notice that this issue comes back, you might need to adjust a setting to get Android Studio to stop doing that automatically. It's explained in [this post](https://stackoverflow.com/q/57591080). Just FYI. Glad you got it working. Cheers! – Mike M. May 14 '20 at 01:25

0 Answers0