Some test text!
Android / Guides / Custom quick menu
It is possible to customize the quick menu by overriding the default menu resource XML files, or by creating your own.
It is possible to programatically modify the quick menu by calling methods to add and remove items.
The following example will customize the quick menu for the Square tool by overriding the onShowQuickMenu
function and adding a link button. Menu entries can be moved around and removed as well.
Add a menu entry to the ids.xml
in res/values
to identify it:
<resources>
...
<item name="qm_custom_link" type="id" />
...
</resources>
Override the onShowQuickMenu
function, and add the code below to create a new QuickMenuItem
and specify the order you would like for it to show:
The quick menu for the square tool will now have a link item at the 3rd (0 index) position like this:
Quickmenu items can be removed by calling the removeMenuEntries
method in the QuickMenu
class:
if (annot.getType() == Annot.e_Square) {
QuickMenuItem item = new QuickMenuItem(MainActivity.this, R.id.qm_custom_link,
QuickMenuItem.FIRST_ROW_MENU);
ArrayList<QuickMenuItem> items = new ArrayList<>(1);
items.add(item);
quickMenu.removeMenuEntries(items);
}
In this tutorial you will customize the quick menu for the Pan
tool by overriding the default menu resource XML file in the Apryse SDK.
Create a menu resource XML file called pan.xml
in your project's res/menu
folder. This pan.xml
file will automatically be used instead of the default one in the tools package. To override other quick menus, refer to the table below to determine the name of the menu resource file.
Add two items to you menu in pan.xml
with the id
attributes @+id/qm_free_text
and @+id/qm_floating_sig
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/qm_free_text"
android:title="@string/tools_qm_free_text"
android:icon="@drawable/ic_annotation_freetext_black_24dp" />
<item android:id="@+id/qm_floating_sig"
android:title="@string/tools_qm_signature"
android:icon="@drawable/ic_annotation_signature_black_24dp" />
</menu>
Here we use the ids @+id/qm_free_text
and @+id/qm_floating_sig
to override the Free Text
and Signature
menu items.
Now, when you long-press on a blank space, you will see:
Menu resource | Tool | Triggered event |
---|---|---|
pan | Pan | Long press on blank space |
text_select | TextSelect | Long press on text |
annot_simple_shape | AnnotEdit | Single tap on Square, Circle, Line, Polygon, Polyline, Text |
annot_free_text | AnnotEdit | Single tap on FreeText |
annot_link | AnnotEdit | Single tap on Link |
annot_signature | AnnotEdit | Single tap on signature (type: Annot.e_Stamp and custom field: Signature.SIGNATURE_ANNOTATION_ID) |
annot_stamper | AnnotEdit | Single tap on Stamp(type: Annot.e_Stamp) |
annot_file_attachment | AnnotEdit | Single tap on FileAttachment |
annot_free_hand | AnnotEdit | Single tap on Ink |
annot_general | AnnotEdit | Single tap on all other annotation types |
annot_edit_text_markup | AnnotEditTextMarkup | Single tap on TextMarkup annotations |
type | AnnotEditTextMarkup | Click quick menu item with id R.id.type |
sig_field_image | DigitalSignature | Single tap on digital image signature |
annot_edit_thickness | DigitalSignature | Click quick menu item with id R.id.thickness |
sig_field_paths | DigitalSignature | Single tap on digital signature, or click on items in quick menu R.menu.annot_edit_thickness |
You can use quick menus in a ToolManager
as well as in your application logic directly.
By the end of this tutorial, you will be able to create your own quick menu.
res/menu
folder. For example, let's create a file called custom.xml
.Group id | Location in quick menu |
---|---|
@id/qm_first_row_group | First row group id: all menu items inside this group will be displayed in the first row of the quick menu. |
@id/qm_second_row_group | Second row group id: all menu items inside this group will be displayed in the second row of the quick menu. |
@id/qm_overflow_row_group | Overflow list group id: all menu items inside this group will be displayed in the overflow menu list of the quick menu. |
Add the following to custom.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@id/qm_first_row_group">
<item android:id="@+id/rectangle"
android:title="@string/tools_qm_rectangle"
android:icon="@drawable/ic_annotation_square_black_24dp" />
</group>
<group android:id="@id/qm_second_row_group">
<item android:id="@+id/line"
android:title="@string/annot_line"
android:icon="@drawable/ic_annotation_line_black_24dp"/>
<item android:id="@+id/free_text"
android:title="@string/tools_qm_free_text"
android:icon="@drawable/ic_annotation_freetext_black_24dp"/>
</group>
<group android:id="@id/qm_overflow_row_group">
<item android:id="@+id/floating_sig"
android:title="@string/tools_qm_signature"/>
<item android:id="@+id/link"
android:title="@string/annot_link"/>
</group>
</menu>
You can also add a sub menu by specifiying it in the menu resource XML. For example, we can add an Oval menu item as a submenu under the Rectangle menu item by changing custom.xml
to:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@id/qm_first_row_group">
<item android:id="@+id/rectangle"
android:title="@string/tools_qm_rectangle"
android:icon="@drawable/ic_annotation_square_black_24dp" >
<!-- Sub menu added to Rectangle menu item -->
<menu>
<group android:id="@id/qm_first_row_group">
<item android:id="@+id/oval"
android:title="@string/annot_circle"/>
</group>
</menu>
</item>
</group>
<group android:id="@id/qm_second_row_group">
<item android:id="@+id/line"
android:title="@string/annot_line"
android:icon="@drawable/ic_annotation_line_black_24dp"/>
<item android:id="@+id/free_text"
android:title="@string/annot_free_text"
android:icon="@drawable/ic_annotation_freetext_black_24dp"/>
</group>
<group android:id="@id/qm_overflow_row_group">
<item android:id="@+id/floating_sig"
android:title="@string/tools_qm_signature"/>
<item android:id="@+id/link"
android:title="@string/annot_link"/>
</group>
</menu>
For more information on defining an XML menu resource, see Defining a Menu in XML.
After adding the menu resource XML, initialize QuickMenu
as follows:
private void initQuickMenu(PDFViewCtrl pdfViewCtrl) {
QuickMenu quickMenu = new QuickMenu(pdfViewCtrl);
quickMenu.initMenuEntries(R.menu.custom);
}
QuickMenu.setAnchor(View)
, then call QuickMenu.Show()
:Tool.onQuickMenuClicked(QuickMenuItem)
in your custom tool to listen for quick menu click events.You can call the following methods to customize the quick menu item icons programmatically before they are initialized:
Method call | Description |
---|---|
QuickMenuItem.setIcon(Drawable) or QuickMenuItem.setIcon(int) | Set the menu item icon |
QuickMenuItem.setColor(int) | Set the icon color |
QuickMenuItem.setOpacity(float) | Set the icon opacity |
Example
private void initQuickMenu(Context context, PDFViewCtrl pdfViewCtrl) {
QuickMenu quickMenu = new QuickMenu(pdfViewCtrl);
quickMenu.inflate(R.menu.custom);
QuickMenuItem noteItem = (QuickMenuItem) quickMenu.getMenu().findItem(R.id.my_quickmenu_item);
// Customize the menu item by setting a custom icon, with a red color and 0.5 opacity
noteItem.setIcon(R.drawable.my_custom_icon);
noteItem.setColor(context.getResources().getColor(R.color.red));
noteItem.setOpacity(0.5f);
// Initialize the quick menu after customizing menu items
quickMenu.initMenuEntries();
}
You can also customize the icon using the android:icon
attribute in your menu resource XML file. Additionally if your Android app supports minimum API level 26, you can set the icon tint color and tint mode by setting the android:iconTint
and android:iconTintMode
attributes.
Example
<item android:id="@+id/rectangle"
android:title="@string/tools_rectangle"
android:icon="@drawable/annotation_square"
android:iconTint="@color/fab_dark_blue"
android:iconTintMode="src_in"/>
QuickMenu
uses R.style.QuickMenuButton
for the buttons style. You can change the style by overriding it in your res/value/style.xml
file.
If you want to change the button style the after quick menu is displayed, you can find the button by calling QuickMenu.findMenuItem(QuickMenuItem)
. Once the button is found, how you set its style programmatically depends on whether it is an ImageButton or a Button.
Example
/**
* Update quick menu appearance item color
* @param color
*/
public void updateQuickMenuStyleColor(int color) {
if (quickMenu == null) {
return;
}
// Find appearance quick menu item
QuickMenuItem menuItem = (QuickMenuItem) quickMenu.getMenu().findItem(R.id.appearance);
if (menuItem != null) {
menuItem.setIconColor(color);
View button = quickMenu.findButtonByMenuItem(menuItem);
if (button != null) {
if (button instanceof ImageButton) {
((ImageButton) button).setColorFilter(color, PorterDuff.Mode.SRC_IN);
} else if (button instanceof Button) {
((Button) button).setTextColor(color);
}
}
}
}
The quick menu background uses drawable/quickmenu_bg_rect.xml
for API 21 and above and drawable/quickmenu_bg_rect_old_api
for API under 21. You can change the background by overriding these drawable files.
There is a hairline divider between the first and second row of the quick menu. If both rows exist, the divider is VISIBLE
by default. You can hide this hairline divider by calling QuickMenu.setDividerVisibility(View.INVISIBLE)
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales