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