Annotation toolbar customization in Android

AnnotationToolbarComponent is a class that contains the logic and views that make up the annotation toolbar. The annotation toolbar can be customized to contain a number of annotation creation tools button as well as any custom button. With the annotation toolbar, users are able to conveniently create annotations and switch between different tools.

The buttons on the annotation toolbar are contained in two groups: the scrollable region (left) and the sticky region (right). The scrollable region can contain any number of buttons and scrolling is used to access buttons that are off-screen. Buttons in the sticky region will always show on the screen and will not be scrollable.

Apryse Docs Image

To learn more about each icon, see the icon cheat sheet .

Create a custom annotation toolbar

The annotation toolbar allows users to easily switch tools when adding annotations to a document. To add an annotation toolbar to your app, first define some layout groups in your app that will contain the annotation toolbar:

XML

1<FrameLayout android:id="@+id/annotation_toolbar_container" android:layout_width="match_parent" android:layout_height="wrap_content"/>

Then in your activity, setup your AnnotationToolbarComponent with ToolManager and attach the required ViewModel classes:

1private AnnotationToolbarComponent mAnnotationToolbarComponent;
2...
3public void setupAnnotationToolbar(ToolManager toolManager) {
4 // First find our container view group
5 FrameLayout mToolbarContainer = findViewById(R.id.annotation_toolbar_container);
6
7 // Then setup ToolManager and related ViewModels with AnnotationToolbarComponent
8 // Remember to initialize your ToolManager before calling below
9 ViewModelProvider vmProvider = ViewModelProviders.of(this);
10 ToolManagerViewModel toolManagerVm = vmProvider.get(ToolManagerViewModel.class);
11 toolManagerVm.setToolManager(toolManager);
12 PresetBarViewModel presetVm = vmProvider.get(PresetBarViewModel.class);
13 AnnotationToolbarViewModel annotToolbarVm = vmProvider.get(AnnotationToolbarViewModel.class);
14
15 // Create our UI component for the annotation toolbar
16 mAnnotationToolbarComponent = new AnnotationToolbarComponent(
17 this,
18 annotToolbarVm,
19 presetVm,
20 toolManagerVm,
21 mToolbarContainer
22 );
23}

Finally, you can customize the annotation toolbar by either using one of our default toolbars or creating a custom toolbar from scratch.

1// Use a default toolbar
2mAnnotationToolbarComponent.inflateWithBuilder(
3 DefaultToolbars.defaultAnnotateToolbar
4);
5
6// Or create a custom toolbar from scratch
7mAnnotationToolbarComponent.inflateWithBuilder(
8 AnnotationToolbarBuilder.withTag("Custom Toolbar")
9 // Add some pre-defined tools that will be handled internally using ToolManager
10 .addToolButton(ToolbarButtonType.SQUARE, 100)
11 .addToolButton(ToolbarButtonType.INK, 102)
12 .addToolButton(ToolbarButtonType.FREE_HIGHLIGHT, 103)
13 .addToolButton(ToolbarButtonType.ERASER, 104)
14 // We can also add a custom button to handle ourselves, for example here
15 // we have added a "favorites" button that contains a star icon
16 .addCustomButton(R.string.favorites, R.drawable.annotation_note_icon_star_fill, 105)
17 .addToolStickyButton(ToolbarButtonType.UNDO, 106)
18 .addToolStickyButton(ToolbarButtonType.REDO, 107)
19);

Show and hide annotation toolbar

You can show and hide the toolbar by calling AnnotationToolbarComponent.show(boolean) and AnnotationToolbarComponent.hide(boolean).

Hide annotation toolbar items

If you have used ToolManager to hide certain tools, then the associated tool buttons will also be hidden in the annotation toolbar.

Hide tool buttons

If there are tools that you would like to remove from the annotation toolbar, you can disable them by calling ToolManager.disableToolMode(ToolMode[]). This will also hide the tool in the quick menu. For example:

1// Disable TextHighlightCreate tool, TextUnderlineCreate tool,
2// TextSquigglyCreate tool and TextStrikeoutCreate tool
3mToolManager.disableToolMode(new ToolMode[]{
4 ToolManager.ToolMode.TEXT_HIGHLIGHT,
5 ToolManager.ToolMode.TEXT_UNDERLINE,
6 ToolManager.ToolMode.TEXT_SQUIGGLY,
7 ToolManager.ToolMode.TEXT_STRIKEOUT}
8);

For reference, a mapping of ToolMode to button icons is shown below:

Icon

ToolMode

Apryse Docs Image

RECT_CREATE

Apryse Docs Image

OVAL_CREATE

Apryse Docs Image

POLYLINE_CREATE

Apryse Docs Image

POLYGON_CREATE

Apryse Docs Image

CLOUD_CREATE

Apryse Docs Image

LINE_CREATE

Apryse Docs Image

ARROW_CREATE

Apryse Docs Image

RULER_CREATE

Apryse Docs Image

TEXT_CREATE

Apryse Docs Image

CALLOUT_CREATE

Apryse Docs Image

TEXT_ANNOT_CREATE

Apryse Docs Image

INK_CREATE

Apryse Docs Image

INK_ERASER

Apryse Docs Image

SIGNATURE

Apryse Docs Image

STAMPER

Apryse Docs Image

RUBBER_STAMPER

Apryse Docs Image

FREE_HIGHLIGHTER

Apryse Docs Image

TEXT_HIGHLIGHT

Apryse Docs Image

TEXT_UNDERLINE

Apryse Docs Image

TEXT_SQUIGGLY

Apryse Docs Image

TEXT_STRIKEOUT

Apryse Docs Image

ANNOT_EDIT_RECT_GROUP

Annotation toolbar listener

You can set an AnnotationButtonClickListener to be notified of annotation toolbar button click events.

Java

1mAnnotationToolbarComponent.addButtonClickListener(new AnnotationToolbarComponent.AnnotationButtonClickListener() {
2 @Override
3 public boolean onInterceptItemClick(MenuItem menuItem) {
4 // We can intercept button click events by implementing this method.
5 // Here we can handle our custom "favorites" button by referring to the button's id
6 // that was defined above.
7 if (menuItem.getItemId() == 105) {
8 Toast.makeText(MainActivity.this, "Favorites Pressed", Toast.LENGTH_SHORT).show();
9 return true;
10 }
11 return false;
12 }
13 @Override
14 public void onPreItemClick(MenuItem menuItem) {
15 }
16 @Override
17 public void onPostItemClick(MenuItem menuItem) {
18 }
19});

Edit annotations continuously

For continuous annotation creation, AnnotationToolbarComponent will use value set by PdfViewCtrlSettingsManager.setContinuousAnnotationEdit(Context, boolean) (by default true). If continuous annotation editing is disabled, then the tool button will be de-selected in the annotation toolbar after creating an annotation.

Appearance styles

In order to customize the appearance of UI elements in your annotation toolbar, you can define a new style that extends PTAnnotationToolbarTheme inside your res\values\style.xml file and pass it as an attribute to your viewer's theme named pt_annotation_toolbar_style. For more information on customizing the style of UI components, please take a look at our viewer theme guide .

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales