How to customize the Toolbar theme of Thumbnails and Search

Product: Native Android SDK

Product Version: 9.2.3

Please give a brief summary of your issue:
Can’t figure out how to customize thumbnail and search toolbar colors.

Please describe your issue and provide steps to reproduce it:
When using the PdfViewCtrlTabHostFragment2 can’t figure out the theming components to add to customize the background color of the toolbar when in the Thumbnail fragment. See screenshots below :slight_smile:

Also, is there a way to dynamically change the toolbar color for those fragments? I am able to dynamically change the general toolbar color by doing this:

class PDFTronFragment : PdfViewCtrlTabHostFragment2() {

    override fun initViews() {
        super.initViews()

        applyWhiteLabelIfNeeded()
    }

    private fun applyWhiteLabelIfNeeded() {
        if (BuildConfig.FLAVOR.isCFA()) {
            val color = activity?.getPrimaryColor() ?: return

            val gd = GradientDrawable()
            gd.gradientType = GradientDrawable.LINEAR_GRADIENT
            gd.gradientRadius = 200f * (resources.displayMetrics?.density ?: 1f)
            gd.colors = intArrayOf(
                color.shadeColor(10),
                color.shadeColor()
            )

            toolbar?.background = gd

            toolbar?.setTitleTextAppearance(context, R.style.PTToolbarTitleAppearance)
        }
    }
}

But when tapping the thumbnail button in the bottom bar the background of the toolbar reverts to the color white :confused:

Can I grab the thumbnail fragment’s toolbar dynamically like I do in the code above by calling getToolbar()?


Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi Seth,

Thanks for contacting us. Could you let us know your high level use case for needing to set the toolbar colors programatically? Typically the Android convention is to set the toolbar color via styles. We do not support setting the colors via dynamic values programatically for all our components.

Thanks,
Andrew.

Hi Andrew, one of our products is an app that our customers can customize for their own customers. With this, we allow them to choose a custom white label color that we apply programatically. We are able to get the toolbar and the mSearchToolbar and set a custom background, but we have not been able to do the same for the thumbnail view.
toolbar?.background = gd
mSearchToolbar?.background = gd

We are hoping to do this for the mThumbFragment as well.

Hey @ama!

I wanted to ditto what @jenny.brown says here. We allow our clients to white label the app for their clients. We do not know the white labeling colors until runtime and styles only allows for known colors by compile time. This is why we are trying to change the toolbar colors at runtime once we get the hex values from our servers.

Hopefully this helps you understand our usecase.

Thanks,

Seth

Thanks for the additional information. In this case I suggest customizing our components by extending them directly.

For example you can extend and customize our ThumbnailsViewFragment and use it in your PDFTronFragment. I’ve created a sample in the custom-toolbar-thumbnail branch here: GitHub - PDFTron/pdftron-android-samples at custom-toolbar-thumbnail

Could you give this sample a try and let us know if this will work for you?

1 Like

This works after a small tweak.

I found that I had to comment/remove the line:

super.onPageThumbnailOptionSelected(thumbnailEditMode, checkedItem)

Is this okay practice to do @Branden_Fung? This would make it if you guys make updates I won’t be referencing them :frowning:

Yes it’s okay to comment out super.onPageThumbnailOptionSelected(thumbnailEditMode, checkedItem), since this overridden method is just referencing the the source code for super.onPageThumbnailOptionSelected.

I see what you mean, alternatively I could add some API to allow you to inject a custom ThumbnailsViewFragment in your custom PdfViewCtrlTabHostFragment2. However, you will need to update to a newer snapshot version of the SDK. Would this be an option for you?

@Branden_Fung That would be great if you could create an api to inject out own custom class that extends the ThumbnailsViewFragment. We have no problem updating which PDFTron SDK snapshot we are referencing

Hi,

Thanks for getting back to us about the API to create your own ThumbnailViewFragment class. Could you let us know if this item is blocking you, or any timelines you may have?

Thanks,
Andrew.

Hi Seth,

We have released a new snapshot beta version of our SDK : Version 9.3.0-beta04
Please see this guide on how to use our snapshot version.

Once you have updated to the latest version you will have access to the new function getThumbnailViewFragment()

Please see sample code below:
Create custom ThumbnailsViewFragment:

public class CustomThumbnailViewFragment extends ThumbnailsViewFragment {
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mToolbar.setBackgroundColor(Color.GREEN);
    }
}

Create custom PdfViewCtrlTabHostFragment2 class and override getThumbnailViewFragment

public class MyPdfViewCtrlTabHostFragment2 extends PdfViewCtrlTabHostFragment2 {
    protected ThumbnailsViewFragment getThumbnailViewFragment() {
        return CustomThumbnailViewFragment.newInstance();
    }
}

Use your PdfViewCtrlTabHostFragment2 as follows

PdfViewCtrlTabHostFragment2 fragment =
                ViewerBuilder2.withUri(fileUri).build(this, MyPdfViewCtrlTabHostFragment2.class);

Please let us know if this solution works for you.

Best,
Eamon

1 Like