> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/xamarin/basic-operations/basics/find-text-a.md).

# Add text search bar to Xamarin PDF viewer

Learn how to add text search functionality to your Xamarin.Android viewer with this step-by-step guide. Implement text search UI components and programmatically search for text in PDFViewCtrl. Optimiz

{% hint style="info" %}
**This tutorial only applies to Xamarin.Android. See Xamarin.iOS equivalent here .**
{% endhint %}

Text search functionality can be added to your viewer using the text search UI component. Alternatively, you can programmatically perform text search in PDFViewCtrl.

## Prerequisites

* Set up [PDFViewCtrl](/xamarin/guides/android/basics/open.md#view) and [ToolManager](/xamarin/annotation/config-annot-tools.md#android) in your activity.

## Add text search using UI component

Apryse provides [`SearchToolbar`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.SearchToolbar.html) and [`FindTextOverlay`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.FindTextOverlay.html) as utility classes for implementing text search in your document viewer. Here is a short guide on adding text search to your activity:

1. Add the search controls to your layout, a simple example will be something like:

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">
  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <pdftron.PDF.Controls.SearchToolbar android:id="@+id/searchtoolbar" android:visibility="gone" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" app:contentInsetStart="@dimen/second_keyline" android:background="?attr/colorPrimary" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />
    <pdftron.PDF.PDFViewCtrl android:id="@+id/pdfviewctrl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="vertical|horizontal" />
  </LinearLayout>
  <pdftron.PDF.Controls.FindTextOverlay android:id="@+id/find_text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/>
</RelativeLayout>
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. In `onCreate` of your activity, set up your `SearchToolbar` and `FindTextOverlay`:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
var searchToolbar = FindViewById<pdftron.PDF.Controls.SearchToolbar>(Resource.Id.searchtoolbar);
var searchOverlay = FindViewById<pdftron.PDF.Controls.FindTextOverlay>(Resource.Id.find_text_view);
searchOverlay.SetPdfViewCtrl(mPdfViewCtrl);

searchToolbar.ExitSearch += (sender, e) =>
{
    searchToolbar.Visibility = ViewStates.Gone;
    searchOverlay.Visibility = ViewStates.Gone;
    searchOverlay.ExitSearchMode();
};
searchToolbar.ClearSearchQuery += (sender, e) =>
{
    searchOverlay?.CancelFindText();
};
searchToolbar.SearchQuerySubmit += (sender, e) =>
{
    searchOverlay?.QueryTextSubmit(e.Query);
};
searchToolbar.SearchQueryChange += (sender, e) =>
{
    searchOverlay?.SetSearchQuery(e.Query);
};
searchToolbar.SearchOptionsItemSelected += (sender, e) =>
{
    int id = e.Item.ItemId;
    if (id == Resource.Id.action_match_case)
    {
        bool isChecked = e.Item.IsChecked;
        searchOverlay?.SetSearchMatchCase(!isChecked);
        searchOverlay?.ResetFullTextResults();
        e.Item.SetChecked(!isChecked);
    }
    else if (id == Resource.Id.action_whole_word)
    {
        bool isChecked = e.Item.IsChecked;
        searchOverlay?.SetSearchWholeWord(!isChecked);
        searchOverlay?.ResetFullTextResults();
        e.Item.SetChecked(!isChecked);
    }
};
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Show the layout

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
searchToolbar.Visibility = ViewStates.Visible;
searchOverlay.Visibility = ViewStates.Visible;
```

{% endcode %}
{% endtab %}
{% endtabs %}

Now when running the app, you will see a toolbar that allows you to enter search terms as well as an overlay on top of the PDFViewCtrl that navigates among search results.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-458a76a40e34fcc6632629846962476e9ba5c017%2Fe03209a7fd9601c73d1ea180eea01fc2f0df581b-600x1200.gif?alt=media)

## Search results view

[`SearchResultsView`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.SearchResultsView.html) class enables users to easily search for a query and see the results.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-dbe944bce12238ddbb39e44e9942d70cfc8c9ec9%2F26e5ab9a7adb9084eddbb524fa44c1ec790c62eb-1080x2160.png?alt=media)

To set up your layout for a `SearchResultsView`, add an element to your XML layout as follows:

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<com.pdftron.pdf.controls.SearchResultsView android:id="@+id/searchResultsView" android:layout_width="match_parent" android:layout_height="wrap_content" />
```

{% endcode %}
{% endtab %}
{% endtabs %}

Then, you need to set the following [`events`](https://sdk.apryse.com/api/xamarinandroid/tools/api/pdftron.PDF.Controls.SearchResultsView.html#events)

* An item from the search results is clicked
* A full-text search is started and result has not yet been ready
* A search is found

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
searchResultsView.SetPdfViewCtrl(mPdfViewCtrl);
searchResultsView.FullTextSearchStart += (sender, e) =>
{
    searchToolbar?.SetSearchProgressBarVisible(true);
};
searchResultsView.SearchResultFound += (sender, e) =>
{
    searchToolbar?.SetSearchProgressBarVisible(false);
    searchOverlay?.HighlightFullTextSearchResult(e.Result);
};
searchResultsView.SearchResultClicked += (sender, e) =>
{
    searchOverlay?.HighlightFullTextSearchResult(e.Result);
    mPdfViewCtrl?.SetCurrentPage(e.Result.PageNum);
    searchResultsView.Visibility = ViewStates.Gone;
};
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/xamarin/basic-operations/basics/find-text-a.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
