0

Good day, I know that this question has been already been asked. But as searched none of their solutions works for me regarding with this topic. I compiled the gradle - compile 'de.hdodenhof:circleimageview:1.3.0' . I created header.xml

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="80dp"
    android:layout_height="80dp"
    app:border_color="#FF000000"
    android:layout_marginLeft="24dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_margin="@dimen/activity_vertical_margin"
    android:layout_marginStart="@dimen/activity_vertical_margin"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:src="@drawable/avatar"
    android:foreground="?attr/selectableItemBackground"
    android:onClick="onClick"
    android:clickable="true" />

For my navigationView.

<!--NAVIGATION DRAWER-->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header"
    app:menu="@menu/navigation_drawer"
    app:itemIconTint="@color/colorTextNavigationView" />

In my java file activity_main.java I tried the onClickListener yet it will stop my program - nullPointerException (cannot mapped my circleImageView findViewById).and even setNavigationItemSelectedListener will not launched the the intents if I clicked the circle image.

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            mDrawerLayout.closeDrawers();

            int id = menuItem.getItemId();
            switch (id){ // INTENTS HERE}

}

header

Is there a way to make this clickable?

RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56

2 Answers2

2

First you have to access the headerlayout that you are using to inflate, which can be done something like this,

View headerLayout = navigationView.getHeaderView(0);

then from the headerLayout you can get the imageview by using

ImageView profileimage = (ImageView)headerLayout.findViewById(R.id.yourImage);

then you can set it clickable to it.

Arnold Laishram
  • 1,801
  • 2
  • 18
  • 25
0

This is the nav_header_main.xml

(Here Layout_ww represents width & height as wrap_content)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/nav_header"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/navimgProfile"
        android:layout_width="@dimen/eighty_dp"
        android:layout_height="@dimen/eighty_dp"
        android:layout_gravity="center_horizontal"
        app:civ_border_color="@color/white" />

    <TextView
        android:id="@+id/nav_profile_name"
        style="@style/Layout_mw"
        android:gravity="center"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio" />

    <TextView
        android:id="@+id/pro_location"
        style="@style/Layout_mw"
        android:layout_marginTop="@dimen/ten_dp"
        android:gravity="center"
        android:text="android.studio@android.com" />

</LinearLayout>

In the actvity, navigation is represented as follows:

Inside the onCreate:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
     View header = navigationView.getHeaderView(0);
            CircleImageView imgProfile = (CircleImageView) header.findViewById(R.id.navimgProfile);
            proName = (TextView) header.findViewById(R.id.nav_profile_name);
            proLocation = (TextView) header.findViewById(R.id.pro_location);


  proName.setText(USERNAME);
  proLocation.setText(Navemail);
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48