Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

android - OnClick for navigation drawer header not working

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.

To get the id of this textview I used the following code, since it is in a different layout file compared to the one in the setContentView in onCreate.

    final LayoutInflater factory = getLayoutInflater();

    final View textEntryView = factory.inflate(R.layout.header, null);

    TextView home = (TextView) textEntryView.findViewById(R.id.home);
    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show();

        }
    });

header.xml contains the header of the navigation drawer. It has an item named home. I need to make it clickable. The above code is in the onCreate method.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For me other Answers didn't work. I have tried the below code. I know it's too late. Hope this will help some.

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 
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...