Android NavigationView Headerlayout's view elements are not triggering click events

715 views Asked by At

I am trying to create Navigation view like gmail. In the header layout I have one image view and two textView. I have registered the click events for the TextView. But when I click the onClick methods the listener are not triggering (Code is in Kotlin)

navigation_view is the id of my NavigationView

var headerView = navigation_view.getHeaderView(0)

var accountName: TextView = headerView.findViewById(R.id.account_name)

accountName.setOnClickListener { Toast.makeText(this, "Redirect to login", Toast.LENGTH_LONG).show() } 

the toast in never displayed when I click on the textView and drawerLayout closes.

1

There are 1 answers

0
saurabh dixit On

Try like this way

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

    View view = navigationView.getHeaderView(0);


   TextView account_name = (TextView) view.findViewById(R.id.account_name);

    account_name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(HomeActivity.this,"Clicked",Toast.LENGTH_LONG).show();
        }
    });