Open In App

How to Use Magnifier API in Android?

You may have encountered a magnified effect when you have scrolled over text view in recent versions of Android, and have wanted to integrate the same functionality in your app as well. In this article, we will learn how to work towards implementing the same in your app. The magnifier widget, which is accessible in Android 9 (API level 28) and higher, is a fictitious magnifying glass that shows an enlarged version of a selected View through an overlay window that serves as the lens. The functionality enhances the text input and selection user experience by allowing users to precisely position the cursor or selection handles while using the magnifier by displaying the magnified text in a pane that moves along with their fingers. 

Use Cases of the Magnifier API

Since the magnifier APIs are not restricted to text, this widget can be used for a variety of purposes, like magnifying difficult-to-read location names on maps and reading small print. There is existing integration between the magnifier and platform widgets like TextView, EditText, and WebView. Across applications, it offers uniform text manipulation. Depending on the context of your application, you can utilize the widget, which has a straightforward API, to magnify any View.



How to Add Magnifier API in Android App?

Follow the simple steps below to add the magnifier API to your android app.

Step #1: Calling the Magnifier View onto an object



We will first add the magnifier object on the class in which we want the magnification to happen, like:




TextView gfgView = findViewById(R.id.gfgView);
Magnifier gfgMagnification = new Magnifier(gfgView);
gfgMagnification.show(view.getWidth() / 3, view.getHeight() / 2);

Step #2: Changing the background

To change your magnification background with a different theme, we will use the functions of this API:




// Use the color in the Parse Color Here.
gfgTextView.setBackgroundColor(PARSE_COLOR_HERE);

The content of the magnifier is outdated since a portion of the view with the previous background is still visible, assuming the background color can be seen within the magnifier. 

GeekTip #1: Use the update() function to update the content.

Step #3: Dismissing the Magnified View

When you have done away with your magnification, you can use this method to dismiss the view and go back.




gfgMagnification.dismiss();

GeekTip#2: The coordinates supplied to display() are forced into the view’s viewable area if they require copying external content.

Additional Things to Think About When Enlarging Text

Conclusion

This is how to use the magnification API in your app, you can use them to add new experiences to your app, and also create fun ways for users to interact with your content. You can leverage this added functionality in your application and support the newer magnification standards, which the older zoom is incapable of doing.


Article Tags :