Open In App

How to Use Magnifier API in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Java




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:

Java




// 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.

Java




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

  • It’s crucial to comprehend distinct magnification behaviors for platform text widgets and to consistently enable the magnifier for your custom text view across the entire Android platform. Think about the following:
  • When a user grips an insertion or selection handle, the magnifier opens right away.
    Vertically, the magnifier is fixed to the middle of the current text line, but always follows the user’s finger with ease in the horizontal direction.
  • The magnifier can only travel horizontally while staying within the left and right edges of the current line. Additionally, because the cursor would no longer be visible inside the magnifier when the user’s touch departs these bounds and the horizontal distance between the touch and the closest bound is greater than half of the original width of the magnifying content, the magnifier is dismissed.
  • If the text font is too big, the magnification will never activate. Text is deemed to be overly large when the height of the material that fits in the magnifier is greater than the difference between the font’s ascent and descent. In this situation, activating the magnifier adds little value.

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.



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads