Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Last active March 15, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeevuz/f9486ec911730fe1bcf0 to your computer and use it in GitHub Desktop.
Save Jeevuz/f9486ec911730fe1bcf0 to your computer and use it in GitHub Desktop.
Investigation results of getting this work.
// Add the view(s)
mNavigationView.getMenu().add(R.id.group_name, R.id.predefined_id, 0, "Title");
mNavigationView.getMenu().findItem(R.id.action_skip).setIcon(R.drawable.custom_icon_with_selector);
...
// Allow changing of selected item via NavigationView.setCheckedItem(id)
mNavigationView.getMenu().setGroupCheckable(R.id.group_name, true, true);
// Remove tinting to see the custom icons
mNavigationView.setItemIconTintList(null)
@Jeevuz
Copy link
Author

Jeevuz commented Mar 15, 2016

If you don't know the count and don't want to create excessive predefined ids, you can use this:

/**
* Generates int with {@link View#generateViewId()} on api 17 and latest, 
* and prior this uses passed {@code random} to generate id in range {@code [min, max)}.
* This is potentially dangerous on versions of api prior 17. See <a href="http://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically">this SO answer</a>
*/
private int generateViewId(Random random, int min, int max) {
        int id = min + random.nextInt(max-min);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            id = View.generateViewId();
        }
        return id;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment