-4

I am writing a code for pinch zoom for textview . Since I am using soo many textviews . I have to use scrollview. If I use scrollview pinch zoom is not working . Otherwise my code is working fine without scrollview. Is there any method to use pinch zoom along with scrollview

2 Answers2

1

You can create an AlertDialog, with a custom view (which can have whatever you want in it).

Here is one (of many) links on how to do this: How to implement a custom AlertDialog View

Community
  • 1
  • 1
Booger
  • 18,579
  • 7
  • 55
  • 72
0

To have a ScrollView with pinch to zoom TextView, you have to capture the touch events on the ScrollView and assign the pinch zoom if 2 fingers are detected like so.

Java class

import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;

public class MyExampleClass extends AppCompatActivity {

    private NestedScrollView myScrollView;
    private TextView myTextView;
    private ScaleGestureDetector scaleGestureDetector;
    private ArrayList<TextView> textViews = new ArrayList<TextView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_example_xml);

        /* instantiation */

        myTextView = findViewById(R.id.ourTextView1);
        myScrollView = findViewById(R.id.ourScrollview);
        scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());

        /* set a custom onTouchListener to the scrollview */

        myScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                /* if 2 fingers are touching the screen, start text resizing */

                if (event.getPointerCount() > 1) {
                    myScrollView.requestDisallowInterceptTouchEvent(true);
                    scaleGestureDetector.onTouchEvent(event);
                    return true;
                }

                /* otherwise pass the touch event to the scrollView for scrolling */

                return false;
            }
        });

    /* add all out TextViews to an ArrayList */

    textViews.add(findViewById(R.id.ourTextView1));
    textViews.add(findViewById(R.id.ourTextView2));
    }

    /* inner class for handling pinch zoom */

    public class simpleOnScaleGestureListener extends
            ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            /* get current text size */

            float size = myTextView.getTextSize();
            Log.d("TextSizeStart", String.valueOf(size));

            /* get how much user pinched */

            float factor = detector.getScaleFactor();
            Log.d("Factor", String.valueOf(factor));

            /* calculate how much to resize to */

            float product = size * factor;
            Log.d("TextSize", String.valueOf(product));

            /* set max size. if product equals more than 140, set product to 140 */

            if (product > 140) {
                product = 140;
            }

            /* set min size. if product equals less than 60, set product to 60 */

            if (product < 60) {
                product = 60;
            }

            /* set textSize in all textViews in arraylist to new size */

            for (TextView v: textViews) {
                v.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
            }

            /* set size to new textSize */

            size = myTextView.getTextSize();
            Log.d("TextSizeEnd", String.valueOf(size));
            return true;
        }
    }
}

xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ourScrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MyExampleClass">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <TextView
        android:id="@+id/ourTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="11dp"
        android:textSize="30sp"
        android:text="@string/long_text" />

    <TextView
        android:id="@+id/ourTextView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="11dp"
        android:textSize="30sp"
        android:text="@string/long_text" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>