# Customizing toolbars in our Android PDF viewer

Nutrient comes with a flexible [toolbar system](https://www.nutrient.io/guides/android/customizing-the-interface/using-toolbars-within-fragment.md) that provides basic action bar functionality, as well as powerful editing features.

This guide covers contextual toolbar customization. For customizing the main toolbar (referred to as a menu), see our [customizing menus](https://www.nutrient.io/guides/android/customizing-the-interface/customizing-menus.md) guide.

## Styling the toolbar

By default, Nutrient will apply default styles to all of its toolbars. A large set of style attributes allows you to modify the appearance of toolbars according to your app’s look. Check out the [styling guide](https://www.nutrient.io/guides/android/customizing-the-interface/appearance-styling.md#toolbar-styling), which gives an overview of all available style attributes.

### Change the grouping of toolbar items

Each toolbar contains [`ContextualToolbarMenuItem`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-contextual-toolbar-menu-item/index.html)s representing toolbar elements/items.

The rules of how menu items in each toolbar are grouped are defined in classes extending [`PresetMenuItemGroupingRule`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar.grouping.presets/-preset-menu-item-grouping-rule/index.html), which you can set through [`setMenuItemGroupingRule()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-contextual-toolbar/set-menu-item-grouping-rule.html) called on the specific toolbar. What you want to do is override [`PresetMenuItemGroupingRule#getGroupPreset()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar.grouping.presets/-preset-menu-item-grouping-rule/get-group-preset.html), returning a different structure depending on the given toolbar capacity.

[`MenuItem`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar.grouping.presets/-menu-item/index.html) represents a structure, using IDs for identification, of how [`ContextualToolbarMenuItem`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-contextual-toolbar-menu-item/index.html)s with those IDs would be positioned/grouped. For more details on the implementation, check out `CustomToolbarIconGroupingExample` in the Catalog app.

**Custom toolbar grouping**: A markup item group, a color picker, a custom menu item, and an image picker, all defined in a custom grouping structure.

For example, let’s define a custom grouping of menu items for [`AnnotationToolbar`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-annotation-toolbar/index.html), but with our own added custom item:

### MYCUSTOMGROUPING.KT

```kt

class MyCustomGrouping(context: Context) : PresetMenuItemGroupingRule(context) {

    override fun getGroupPreset(capacity: Int, itemsCount: Int): List<MenuItem> {
        // The capacity shouldn't be less than 4. If that is the case, return an empty list.
        if (capacity < ContextualToolbar.MIN_TOOLBAR_CAPACITY) return emptyList<MenuItem>()

        return if (capacity <= 7) FOUR_ITEMS_GROUPING else SEVEN_ITEMS_GROUPING
    }

    /** Annotation toolbar grouping with 4 elements. */
    private val FOUR_ITEMS_GROUPING = listOf(
            MenuItem(R.id.pspdf_menu_custom),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_group_markup,
                    intArrayOf(
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_highlight,
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_squiggly,
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_strikeout,
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_underline))
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_picker),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_image)
            )

    /** Annotation toolbar grouping with 7 elements. */
    private val SEVEN_ITEMS_GROUPING = listOf(
            MenuItem(R.id.pspdf_menu_custom),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_highlight),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_squiggly),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_strikeout),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_underline),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_picker),
            MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_image)
        )

}

```

### MYCUSTOMGROUPING.JAVA

```java

public class MyCustomGrouping extends PresetMenuItemGroupingRule {

    public CustomAnnotationCreationToolbarGroupingRule(@NonNull Context context) {
        super(context);
    }

    @Override
    public List<MenuItem> getGroupPreset(@IntRange(from = ContextualToolbar.MIN_TOOLBAR_CAPACITY) int capacity, int itemsCount) {
        // The capacity shouldn't be less than 4. If that is the case, return an empty list.
        if (capacity < ContextualToolbar.MIN_TOOLBAR_CAPACITY) return new ArrayList<>(0);

        return (capacity <= 7)? FOUR_ITEMS_GROUPING : SEVEN_ITEMS_GROUPING;
    }

    /** Annotation toolbar grouping with 4 elements. */
    private static final List<MenuItem> FOUR_ITEMS_GROUPING = new ArrayList<>(4);
    static {
        // Make sure our custom item is included.
        FOUR_ITEMS_GROUPING.add(new MenuItem(R.id.pspdf_menu_custom));
        FOUR_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_group_markup,
                new int[]{
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_highlight,
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_squiggly,
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_strikeout,
                        com.pspdfkit.R.id.pspdf__annotation_toolbar_item_underline}));
        FOUR_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_picker));
        FOUR_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_image));
    }

    /** Annotation toolbar grouping with 7 elements. */
    private static final List<MenuItem> SEVEN_ITEMS_GROUPING = new ArrayList<>(7);
    static {
        // Make sure our custom item is included.
        SEVEN_ITEMS_GROUPING.add(new MenuItem(R.id.pspdf_menu_custom));
        SEVEN_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_highlight));
        SEVEN_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_squiggly));
        SEVEN_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_strikeout));
        SEVEN_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_underline));
        SEVEN_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_picker));
        SEVEN_ITEMS_GROUPING.add(new MenuItem(com.pspdfkit.R.id.pspdf__annotation_toolbar_item_image));
    }

}

```

Now let’s create an activity where we can listen to the annotation toolbar being displayed and apply our own structure and add a custom item to it:

### MYCUSTOMACTIVITY.KT

```kt

class MyCustomActivity : PdfActivity(), ToolbarCoordinatorLayout.OnContextualToolbarLifecycleListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setOnContextualToolbarLifecycleListener(this)
    }

    override fun onPrepareContextualToolbar(toolbar: ContextualToolbar<*>) {
        if (toolbar is AnnotationToolbar) {
            toolbar.setMenuItemGroupingRule(CustomAnnotationCreationToolbarGroupingRule(this))

            // Get the existing menu items so we can add our item later.
            val menuItems = toolbar.menuItems

            // Create our custom menu item.
            val customItem = ContextualToolbarMenuItem.createSingleItem(
                this,
                R.id.pspdf_menu_custom,
                ContextCompat.getDrawable(this, R.drawable.ic_bookmark_outline),
                "Bookmark",
                Color.WHITE,
                Color.WHITE,
                ContextualToolbarMenuItem.Position.START,
                false
            )

            // Tell the toolbar about our new item.
            menuItems.add(customItem)
            toolbar.setMenuItems(menuItems)

            // Add a listener so we can handle clicking on our item.
            toolbar.setOnMenuItemClickListener(
                ContextualToolbar.OnMenuItemClickListener { toolbar, menuItem ->
                    return@OnMenuItemClickListener (
                        if (menuItem.id == R.id.pspdf_menu_custom) {
                            Toast.makeText(this@MyCustomActivity, "Custom Action clicked", Toast.LENGTH_SHORT).show()
                            true
                        } else false
                    )
                }
            )
    }...

}

```

### MYCUSTOMACTIVITY.JAVA

```java

public class MyCustomActivity extends PdfActivity implements ToolbarCoordinatorLayout.OnContextualToolbarLifecycleListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setOnContextualToolbarLifecycleListener(this);
    }

    @Override
    public void onPrepareContextualToolbar(@NonNull ContextualToolbar toolbar) {
        if (toolbar instanceof AnnotationToolbar) {
            toolbar.setMenuItemGroupingRule(new CustomAnnotationCreationToolbarGroupingRule(this));

            // Get the existing menu items so we can add our item later.
            final List<ContextualToolbarMenuItem> menuItems = ((AnnotationToolbar) toolbar).getMenuItems();

            // Create our custom menu item.
            final ContextualToolbarMenuItem customItem = ContextualToolbarMenuItem.createSingleItem(
                this,
                R.id.pspdf_menu_custom,
                ContextCompat.getDrawable(this, R.drawable.ic_bookmark_outline),
                "Bookmark",
                Color.WHITE,
                Color.WHITE,
                ContextualToolbarMenuItem.Position.START,
                false
            );

            // Tell the toolbar about our new item.
            menuItems.add(customItem);
            toolbar.setMenuItems(menuItems);

            // Add a listener so we can handle clicking on our item.
            toolbar.setOnMenuItemClickListener(new ContextualToolbar.OnMenuItemClickListener() {
                @Override
                public boolean onToolbarMenuItemClick(@NonNull ContextualToolbar toolbar, @NonNull ContextualToolbarMenuItem menuItem) {
                    if (menuItem.getId() == R.id.pspdf_menu_custom) {
                        Toast.makeText(MyCustomActivity.this, "Custom Action clicked", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    return false;
                }
            });
        }
    }

}

```

### Group MenuItems

When creating a [`MenuItem`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar.grouping.presets/-menu-item/index.html) with subitems, keep the following two points in mind:

1. For your `id`, always use one of the IDs marked as `group`, e.g. `pspdf__annotation_toolbar_group_drawing` and not `pspdf__annotation_toolbar_item_underline`. This is to ensure you don’t accidentally use the same ID twice — for example, using `pspdf__annotation_toolbar_item_underline` both for the group ID and as an actual item inside the menu. Using an ID marked as `group` also makes the difference between group items and their children more obvious. Starting with Nutrient Android SDK 7, this will be enforced and an exception will be thrown if an invalid ID is passed in for a group item.

2. A group item in the [`AnnotationToolbar`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-annotation-toolbar/index.html) never has its own icon. Instead, it will always look like the last item that was selected from the subgroup. This doesn’t apply in other toolbars where some group items get their own special icon — for example, `pspdf__document_editing_toolbar_group_more` will result in an ellipsis icon instead of using one of the child icons, etc.

## Toolbar positions

You can programmatically set the position of a toolbar or lock the toolbar into a specific position using [`ToolbarCoordinatorLayout.LayoutParams`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-toolbar-coordinator-layout/-layout-params/index.html). To set the toolbar to a specific position, you can call [`setPosition()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-contextual-toolbar/set-position.html) directly on the toolbar instance you want to move:

### MYPDFACTIVITY.KT

```kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // This example will move every toolbar that is shown to the user to the right position.
    setOnContextualToolbarLifecycleListener(object : OnContextualToolbarLifecycleListener {
        override fun onPrepareContextualToolbar(toolbar: ContextualToolbar) {
            toolbar.position = ToolbarCoordinatorLayout.LayoutParams.Position.RIGHT
        }...
    })
}

```

### MYPDFACTIVITY.JAVA

```java

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This example will move every toolbar that is shown to the user to the right position.
    setOnContextualToolbarLifecycleListener(new OnContextualToolbarLifecycleListener() {
        @Override public void onPrepareContextualToolbar(@NonNull ContextualToolbar toolbar) {
            toolbar.setPosition(ToolbarCoordinatorLayout.LayoutParams.Position.RIGHT);
        }...
    });
}

```

### Lock toolbar into position

You can also permanently lock a toolbar to a specific position by limiting the `allowedPositions` using the toolbar’s [`ToolbarCoordinatorLayout.LayoutParams`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-toolbar-coordinator-layout/-layout-params/index.html):

### MYPDFACTIVITY.KT

```kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // This example will lock every toolbar to the left position,
    // making you unable to drag the toolbar to a different position.
    setOnContextualToolbarLifecycleListener(object : OnContextualToolbarLifecycleListener {
        override fun onPrepareContextualToolbar(toolbar: ContextualToolbar) {
            toolbar.layoutParams = ToolbarCoordinatorLayout.LayoutParams(
                Position.LEFT, EnumSet.of(Position.LEFT)
            ))
        }...
    })
}

```

### MYPDFACTIVITY.JAVA

```java

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This example will lock every toolbar to the left position,
    // making you unable to drag the toolbar to a different position.
    setOnContextualToolbarLifecycleListener(new OnContextualToolbarLifecycleListener() {
        @Override public void onPrepareContextualToolbar(@NonNull ContextualToolbar toolbar) {
            toolbar.setLayoutParams(new ToolbarCoordinatorLayout.LayoutParams(
                Position.LEFT, EnumSet.of(Position.LEFT)
            ));
        }...
    });
}

```
---

## Related pages

- [Annotation editing on Android](/guides/android/user-interface/contextual-toolbars/annotation-editing.md)
- [Effortlessly customize text selection in Android](/guides/android/user-interface/contextual-toolbars/text-selection.md)
- [Customizing the PDF editing toolbar on Android](/guides/android/features/document-editor.md)
- [Customizing the annotation creation toolbar in our Android PDF viewer](/guides/android/annotations/custom-annotation-editing-controls.md)
- [Using contextual toolbars within PdfFragment](/guides/android/customizing-the-interface/using-toolbars-within-fragment.md)

