How do I add a pinch to enlarge the text in the viewpager?

I've searched a lot of blogs on pinch to zoom TextView

in ViewPager

. I just found many answers to increase to increase ImageView


How can I add a pinch to zoom in the ViewPager.

Here are the links I read before posting:


protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof TouchImageView) {
        return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
    } else {
        return super.canScroll(v, checkV, dx, x, y);


I am getting error in if (v instanceof TouchImageView)

. the error message TouchImageView

cannot be resolved


source to share

1 answer

Create a private class SimpleOnScaleGestureListener

that extends SimpleOnScaleGestureListener

in your activity like this

public class SimpleOnScaleGestureListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {

    float scaleFactor;

    public boolean onScale(ScaleGestureDetector detector) {

        scaleFactor *= detector.getScaleFactor();
        scaleFactor = Math.max(15.0f, Math.min(scaleFactor,36.0f));
         View view=pager.getFocusedChild().findViewById(  ;
         View article_intro=pager.getFocusedChild().findViewById(  ;

         ViewGroup group = (ViewGroup) view;
         int count=group.getChildCount();

       for(int i=0;i<count;i++){
            View child=group.getChildAt(i);
              if(child instanceof TextView){
                  TextView textView=(TextView)child;

        return true;


Internally, the onScale

function we are doing first gets ScaleFactor

and sets the limits for scaling the text. Here the minimum text is 15 and the maximum will be 36 .
From the next line, we are using scaling for the Textview inside the viewpager.

View view=pager.getFocusedChild().findViewById(;


In this first one, I get the current viewpager by calling pager.getFocusedChild()

, from which I get a linearlayout that contains all those text images that I want to enlarge.

You can pass text IDs right there. At runtime, I am adding text images to this layout, so I follow like this. If you want to scale a mutilated text image at a time that has the same parent, you can follow this example, or you can create multiple View instances by passing the text directly.



Here I am applying scaling to textview

you have to override the touch event detection method with viewpager

public boolean dispatchTouchEvent(MotionEvent ev) {

    return scaleGestureDetector.onTouchEvent(ev);



And in onCreate()

create an instance of ScaleGestureDetector like this:

scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());


Now you can apply smooth scaling in the viewpager.



All Articles