Some test text!

Search
Hamburger Icon

Xamarin / Guides / Integrate

Integrate the new Viewer UI

Integrate the new Viewer UI in Xamarin.Android

If you are newly adding the document viewer into your app, you can follow this guide to add the viewer with the new UI. If you are looking to migrate from the your viewer in your app to use the new UI, you can follow this ui migration guide.

Integrate via Fragment

To use the new drop-in viewer fragment, create a PdfViewCtrlTabHostFragment2 using the new ViewerBuilder2 class.

// Add a viewer fragment to the layout container in the specified 
// activity, and returns the added fragment
public PdfViewCtrlTabHostFragment2 AddViewerFragment(int fragmentContainer, 
            AppCompatActivity activity, Android.Net.Uri fileUri, String password)
{
    // Create the viewer fragment
    PdfViewCtrlTabHostFragment2 fragment = (PdfViewCtrlTabHostFragment2)ViewerBuilder2.WithUri(fileUri, password).Build(activity);

    // Add the fragment to the layout fragment container
    activity.SupportFragmentManager.BeginTransaction()
            .Replace(fragmentContainer, fragment)
            .Commit();

    return fragment;
}

Integrate via Activity

A new builder class DocumentActivity.IntentBuilder has been added to help you more easily customize the activity document viewer. You can also use this new builder to specify the viewer to use the new UI as follows:

private void OpenContentUriDocument(Context context, Android.Net.Uri contentUri)
{
    Intent intent = DocumentActivity.IntentBuilder.FromActivityClass(this, Java.Lang.Class.FromType(typeof(DocumentActivity)))
        .WithUri(contentUri)
        .UsingNewUi(true)
        .Build();
    StartActivity(intent);
}

Building a custom annotation toolbar

Buttons in the annotation toolbar can be customized using the AnnotationToolbarBuilder API. 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.

For example, built-in PDF tool buttons can be added to a viewer as follows:

// Supply a unique tag for the toolbar that will be referenced internally
AnnotationToolbarBuilder builder = AnnotationToolbarBuilder.WithTag("my_unique_annotate_toolbar_tag")
    // Set a display name for this toolbar
    .SetToolbarName("Text Annotate")
    // Adds three tool buttons (text highlight, freehand highlight, and text underline) to the scrollable region (left)
    .AddToolButton(ToolbarButtonType.TextHighlight, DefaultToolbars.ButtonId.TextHighlight.Value())
    .AddToolButton(ToolbarButtonType.FreeHighlight, DefaultToolbars.ButtonId.FreeHighlight.Value())
    .AddToolButton(ToolbarButtonType.TextUnderline, DefaultToolbars.ButtonId.TextUnderline.Value())
    // Adds two tool buttons (undo and redo) to the sticky region
    .AddToolStickyButton(ToolbarButtonType.Undo, DefaultToolbars.ButtonId.Undo.Value())
    .AddToolStickyButton(ToolbarButtonType.Redo, DefaultToolbars.ButtonId.Redo.Value());

// Then supply the builder to ViewerConfig, which will be passed on to your viewer
ViewerConfig config = new ViewerConfig.Builder()
    // Add out custom annotation toolbar
    .AddToolbarBuilder(builder)
    // Other ViewerConfig settings...
    .Build();

Afterwards, you can supply this AnnotationToolbarBuilder to the viewer using the ViewerConfig class.

If you would like to add a button with custom functionality, you can add a generic button as so:

AnnotationToolbarBuilder.WithTag("my_unique_annotate_toolbar_tag")
        // Here we add a button with custom title, icon, and an id of 1000. 
        // This id will be referenced when implementing callbacks
        .AddCustomButton(Resource.String.custom_title, Resource.Drawable.custom_icon, 1000);

Then you can listen for annotation toolbar button events by adding a PdfViewCtrlTabHostFragment2.TabHostListener:

mPdfViewCtrlTabHostFragment.ToolbarOptionsItemSelected += (sender, e) =>
{
    // The button id defined previously in the AnnotationToolbarBuilder
    var id = e.P0.ItemId;
    e.Handled = false;
};

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales