Customising NavigationView - Adding dynamic headerView, Android Support Design Library
Solution 1
first create header XML like lay_header.xml
<TextView
android:id="@+id/tvThought"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
on your java file inflate this above header in a TextView. like
TextView headerView = (TextView) LayoutInflater.from(this).inflate(R.layout.lay_header, null);
headerView.setText("Your_thoght");
Now add it as a HeaderView
navView = (NavigationView) findViewById(R.id.navView);
navView.addHeaderView(headerView);
Thats it...
Solution 2
After the new support library update (23.1.1),
You could do this -
Add the headerview in the app:headerLayout="@layout/drawer_header"
inside NavigationView.
Then, you can access it by,
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
or if you have multiple headers
navigationView.getHeaderCount()
Ref : https://code.google.com/p/android/issues/detail?id=190226#c31
Solution 3
TextView txt2;
txt2 = (TextView) navigationView.inflateHeaderView(R.layout.nav_header_main).findViewById(R.id.textView2);
txt2.setText("wow! It works like a charm");
Solution 4
create header layout take text view inside,
<TextView
android:id="@+id/profile_email_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/profile_image"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/expand_account_box_indicator"
android:ellipsize="end"
android:maxLines="1"
android:paddingBottom="16dp"
android:singleLine="true"
android:clickable="true"
android:onClick="onSelectText"
android:text="[email protected]"
android:textColor="@color/body_text_2_inverse"
android:textSize="@dimen/text_size_medium" />
in onCreate,
((TextView) findViewById(R.id.profile_email_text)).setText("test");
create method onSelectText
in your activity
public void onSelectText(View v){
if(v.getId() == R.id.profile_email_text){
Snackbar
.make(fab, "clicked on sub title", Snackbar.LENGTH_LONG)
//.setAction(R.string.snackbar_action, myOnClickListener)
.show();
drawer_layout.closeDrawers();
}
}
Solution 5
You may add your custom header programmatically by calling addHeaderView
on your navigationView, or define it in the layout file using app:headerLayout="@layout/myheader"
.
Abdul Rahman
An IT student from Singapore. Proficient programming languages: C#, Java, PHP Intermediate: Objective C, JavaScript Markup Languages: HTML, CSS, XAML, XML
Updated on July 09, 2020Comments
-
Abdul Rahman almost 4 years
I tried the navigationView from the new android support design library. I want to have a dynamic headerview. Basically, my headerview will show something like quote of the day. I have like around 10 quotes and i want to randomly select a quote and display in a textview in the headerView. I also want to add onClick method for the headerView.
Right now, I don't see any possibilities of changing the headerview layout programmatically. Any suggestions to implement this?