Inspired from this
I have tried the following code to zoom text by pinching. It works fine unless the string is so long and covers the screen. When text covers the whole screen, it does not become possible to zoom out or zoom in by pinching anymore.
I am using the textView inside of a scrollView and then inside of a linearLayout. Without scrollView it works fine but with scrollView the problem remains. I need the scroolView since my lengthy string could be look like several paragraph.
How can I zoom in or out the text even if string is so long and covers the whole screen?
public class MainActivity extends Activity {
TextView textView3;
ScaleGestureDetector scaleGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView3 = (TextView) findViewById(R.id.text_body_story);
textView3.setText("What we are seeing in Eastern Ghouta and elsewhere in Syria are likely war crimes and potentially crimes against humanity");
scaleGestureDetector = new ScaleGestureDetector(this, new CustomOnScaleGestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
public class CustomOnScaleGestureListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float size = textView3.getTextSize();
float factor = detector.getScaleFactor();
float product = size * factor;
if (product >= 160) {
product = 160;
} else if (product <= 30) {
product = 30;
}
textView3.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
return true;
}
}
}
The xml code is-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pinchzoom.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_story_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center_horizontal"
android:text="text_story_name"
android:textAllCaps="true"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text_author_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text_author_name"
android:textAlignment="center"
android:textSize="14sp"
android:textStyle="bold|italic" />
<Space
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#000000"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#000000"
app:srcCompat="?attr/actionModeSplitBackground" />
<TextView
android:id="@+id/text_body_story"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="text_body_story"
android:textColorHighlight="#ffee04"
android:textSize="14sp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>