I\'m using the support NavigationView in my navigation drawer to display menu of items.
I eventually found out what was causing the issue. For NavigationView to work properly with action views, you must use AppCompat support library version 23.1
So instead of
compile 'com.android.support:appcompat-v7:22.3.0'
I had to update to
compile 'com.android.support:appcompat-v7:23.1.1'
which made the trick and the action view in navigation drawer's navigation view started showing properly, exactly as I wanted.
When updating to the new AppCompat version I came across several more problems like ClassNotFoundException
showing up when starting the app, which I fixed by updating all com.android.support libraries to the latest version:
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:support-annotations:23.1.1'
...
Then I was still getting NullPointerException
in my header layout set to the NavigationView. If you're setting app:headerLayout="@layout/drawer_header"
or similarly in code, in AppCompat version 22 it was possible to get the header view by findViewById()
.
AppCompat version 23, though, uses RecyclerView for all the items including the header view, so the way to get reference to its views is following:
mHeaderView = navigationView.getHeaderView(HEADER_INDEX);
Where HEADER_INDEX
is most likely 0 if you're not adding multiple headers.
I can change the text of the counter easily enough. However, The text view that I use is within a LinearLayout
, and it has an android:id as well (I tried this without the LinearLayout
without issue):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textCounter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="@color/colorPrimary"
android:text="5"/>
</LinearLayout>
Given that, I am able to see, access and modify the text view within the main activity using the following :
TextView countText = (TextView)findViewById(R.id.textCounter);
countText.setText("6");
Of course, I'm using a trivial method to calculate the value, just setting it to the value "6", but, I don't know what your counter structure actually looks like, and those are not the main concern of your question.
The issue with your ability to view the counter number remains a mystery. See my questions above and we'll hammer out that element as well.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}