Some test text!

Search
Hamburger Icon

Xamarin / Guides

Annotation toolbar customization in Xamarin

This tutorial only applies to Xamarin.Android. See Xamarin.iOS equivalent here .

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.

Annotation toolbar
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:

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

AnnotationToolbarComponent annotationToolbarComponent;
// ...
private void SetupAnnotationToolbar(ToolManager toolManager)
{
    // First find our container view group
    var mToolbarContainer = FindViewById(Resource.Id.annotation_toolbar_container);

    // Then setup ToolManager and related ViewModels with AnnotationToolbarComponent
    // Remember to initialize your ToolManager before calling below
    var vmProvider = ViewModelProviders.Of(this);
    var toolManagerVm = (ToolManagerViewModel)vmProvider.Get(Java.Lang.Class.FromType(typeof(ToolManagerViewModel)));
    toolManagerVm.ToolManager = toolManager;
    var presetVm = (PresetBarViewModel)vmProvider.Get(Java.Lang.Class.FromType(typeof(PresetBarViewModel)));
    var annotToolbarVm = (AnnotationToolbarViewModel)vmProvider.Get(Java.Lang.Class.FromType(typeof(AnnotationToolbarViewModel)));

    // Create our UI component for the annotation toolbar
    annotationToolbarComponent = new AnnotationToolbarComponent(this, annotToolbarVm, presetVm, toolManagerVm, mToolbarContainer);
}

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

// Use a default toolbar
annotationToolbarComponent.InflateWithBuilder(DefaultToolbars.DefaultAnnotateToolbar);

// Or create a custom toolbar from scratch
annotationToolbarComponent.InflateWithBuilder(
    AnnotationToolbarBuilder.WithTag("Custom Toolbar")
    // Add some pre-defined tools that will be handled internally using ToolManager
    .AddToolButton(ToolbarButtonType.Square, 100)
    .AddToolButton(ToolbarButtonType.Ink, 102)
    .AddToolButton(ToolbarButtonType.FreeHighlight, 103)
    .AddToolButton(ToolbarButtonType.Eraser, 104)
    // We can also add a custom button to handle ourselves, for example here
    // we have added a "favorites" button that contains a star icon
    .AddCustomButton(Resource.String.favorites, Resource.Drawable.annotation_note_icon_star_fill, 105)
    .AddToolStickyButton(ToolbarButtonType.Undo, 106)
    .AddToolStickyButton(ToolbarButtonType.Redo, 107)
    );

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:

// Disable TextHighlightCreate tool, TextUnderlineCreate tool,
// TextSquigglyCreate tool and TextStrikeoutCreate tool
mToolManager.DisableToolMode(new ToolManager.ToolMode[] {
    ToolManager.ToolMode.TextHighlight,
    ToolManager.ToolMode.TextUnderline,
    ToolManager.ToolMode.TextSquiggly,
    ToolManager.ToolMode.TextStrikeout
});

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

IconToolMode
Rectangle imageRECT_CREATE
Ellipse imageOVAL_CREATE
Polyline imagePOLYLINE_CREATE
Polygon imagePOLYGON_CREATE
Cloud imageCLOUD_CREATE
Line imageLINE_CREATE
Arrow imageARROW_CREATE
Ruler imageRULER_CREATE
Free text imageTEXT_CREATE
Callout imageCALLOUT_CREATE
Note imageTEXT_ANNOT_CREATE
Ink imageINK_CREATE
Eraser imageINK_ERASER
Signature imageSIGNATURE
Stamp imageSTAMPER
Rubber stamp imageRUBBER_STAMPER
Free highlighter imageFREE_HIGHLIGHTER
Highlight imageTEXT_HIGHLIGHT
Underline imageTEXT_UNDERLINE
Squiggly imageTEXT_SQUIGGLY
Strikeout imageTEXT_STRIKEOUT
Group select imageANNOT_EDIT_RECT_GROUP

Annotation toolbar listener

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

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 .

Get the answers you need: Chat with us