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.
First fetch header view
And then use onClick Listener
For me other Answers didnt 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.
for clicking the views of header, here I have used a linearlayout of headerview
Or
just add this to your header layout Xml file
Don't forget to define
android:clickable="true"
in your TextView xml.Try like this
i know its late this is for those who facing the same problem.
place your header layout in the navigation view like this
this is in activity_main.xml
create a layout, name it layout_header_profile.xml and put the fill it what ever view you want.
then this header layout file will be in your activity_main.xml only
so in your MainActivity.java you can declare it as you do views from activity_main.xml and perform actions on it, no special code required.
do like this in your onCreate()
Hope it works Happy coding.