Try the Compose way
Jetpack Compose is the recommended UI toolkit for Android. Learn how to use text in Compose.

Available in Android 9 (API level 28) and higher, the magnifier widget is a
virtual magnifying glass that displays an enlarged copy of a View
through an
overlay pane that represents the lens. The feature improves the text insertion
and selection user experience. When applying the magnifier to text, a user can
precisely position the cursor or the selection handles by viewing the magnified
text in a pane that follows their finger.
Figure 1 shows how the magnifier facilitates selecting text. The magnifier APIs aren't tied to text, and you can use this widget in a variety of use cases, such as reading small text or enlarging hard-to-see place names on maps.
The magnifier is already integrated with platform widgets such as TextView
,
EditText
, and WebView
. It provides consistent text manipulation across
applications. The widget comes with a simple API and can be used to magnify any
View
depending on your application's context.
You can use the magnifier programmatically on an arbitrary view as follows:
val view: View = findViewById(R.id.view) val magnifier = Magnifier.Builder(view).build() magnifier.show(view.width / 2.0f, view.height / 2.0f)
View view = findViewById(R.id.view); Magnifier magnifier = new Magnifier.Builder(view).build(); magnifier.show(view.getWidth() / 2, view.getHeight() / 2);
Assuming the view hierarchy has the first layout, the magnifier displays on the screen and contains a region centered on the given coordinates within the view. The pane appears above the center point of the content being copied. The magnifier persists indefinitely until the user dismisses it.
The following code snippet shows how to change the background of the magnified view:
view.setBackgroundColor(...)
view.setBackgroundColor(...);
Assuming the background color is visible within the magnifier, the magnifier's
content is stale, as a region of the view with the old background still
displays. To refresh the content, use the
update()
method, as follows:
view.post { magnifier.update() }
view.post(magnifier::update);
When finished, close the magnifier by calling the
dismiss()
method:
magnifier.dismiss()
magnifier.dismiss();
A common use case for the magnifier is to let the user enlarge a view region by touching it, as shown in figure 2.
You can do this by updating the magnifier according to the touch events received by the view, as follows:
imageView.setOnTouchListener { v, event -> when (event.actionMasked) { MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { val viewPosition = IntArray(2) v.getLocationOnScreen(viewPosition) magnifier.show(event.rawX - viewPosition[0], event.rawY - viewPosition[1]) } MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> { magnifier.dismiss() } } true }
imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: // Fall through. case MotionEvent.ACTION_MOVE: { final int[] viewPosition = new int[2]; v.getLocationOnScreen(viewPosition); magnifier.show(event.getRawX() - viewPosition[0], event.getRawY() - viewPosition[1]); break; } case MotionEvent.ACTION_CANCEL: // Fall through. case MotionEvent.ACTION_UP: { magnifier.dismiss(); } } return true; } });
For the platform text widgets, it's important to understand specific magnifier behaviors and to enable the magnifier for your custom text view consistently across the Android platform. Consider the following:
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-06-02 UTC.