OnClick for navigation drawer header not working

前端 未结 6 585
鱼传尺愫
鱼传尺愫 2020-12-05 07:25

I have a navigation drawer in my app which contains a header and some list items. The header has a textview which i want to make clickable but I am not able to do it.

6条回答
  •  被撕碎了的回忆
    2020-12-05 07:54

    For me other Answers didnt work. I have tried the below code. I know it's too late.

    What I did to access the view of header.

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    View headerview = navigationView.getHeaderView(0);
    TextView profilename = (TextView) headerview.findViewById(R.id.prof_username);
    profilename.setText("your name")
    

    for clicking the views of header, here I have used a linearlayout of headerview

    LinearLayout header = (LinearLayout) headerview.findViewById(R.id.header);
    header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HomeActivity.this, "clicked", Toast.LENGTH_SHORT).show();
                drawer.closeDrawer(GravityCompat.START);
            }
        });
    

    Or

     headerview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Your code here 
            }
        });
    

提交回复
热议问题