Customising NavigationView - Adding dynamic headerView, Android Support Design Library

Abdul Rahman picture Abdul Rahman · Jun 3, 2015 · Viewed 19.9k times · Source

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?

Answer

Moinkhan picture Moinkhan · Jun 5, 2015

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...