-1

I am working on book app. I want to add a zoom feature on my text file. here my code:

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textview_data"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Naruto"
            android:textSize="25dp" />
    </ScrollView>

</LinearLayout>

In the mainActivity i applied some mapView Zoom methods to solve the issue but those methods didn't help much.

package com.example.readtextfiles;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

public class ReadTextFileActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textView = (TextView)findViewById(R.id.textview_data);


        String data = readTextFile(this, R.raw.books);
        textView.setText(data);

                }

    public static String readTextFile(Context ctx, int resId)
    {
        InputStream inputStream = ctx.getResources().openRawResource(resId);

        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader bufferedreader = new BufferedReader(inputreader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        try 
        {
            while (( line = bufferedreader.readLine()) != null) 
            {
                stringBuilder.append(line);
                stringBuilder.append('\n');
            }
        } 
        catch (IOException e) 
        {
            return null;
        }
        return stringBuilder.toString();
    }
}

Please suggest some help,

  • possible duplicate of [How to zoom a textview in android?](http://stackoverflow.com/questions/10239891/how-to-zoom-a-textview-in-android) – karvoynistas Oct 20 '14 at 12:36

2 Answers2

0

just add two button : plus and minus

plus:

    count = count +1;

minus :

    count = count -1;

    textView.setTextSize(count );
deepak825
  • 432
  • 2
  • 8
0

You can use this code for pinch zoom for Textview in android: //Take this xml(layout) file:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/mytv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks

            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks
            " />

    </RelativeLayout>

and Use this class for PinchZoomOnTextview,

public class MyTextViewPinchZoomClass extends Activity implements OnTouchListener {

    final static float STEP = 200;
    TextView mytv;
    float mRatio = 1.0f;
    int mBaseDist;
    float mBaseRatio;
    float fontsize = 13;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_file_name_here);

        mytv = (TextView) findViewById(R.id.mytv);
        mytv.setTextSize(mRatio + 13);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            int action = event.getAction();
            int pureaction = action & MotionEvent.ACTION_MASK;
            if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
                mBaseDist = getDistance(event);
                mBaseRatio = mRatio;
            } else {
                float delta = (getDistance(event) - mBaseDist) / STEP;
                float multi = (float) Math.pow(2, delta);
                mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
                mytv.setTextSize(mRatio + 13);
            }
        }
        return true;
    }

    int getDistance(MotionEvent event) {
        int dx = (int) (event.getX(0) - event.getX(1));
        int dy = (int) (event.getY(0) - event.getY(1));
        return (int) (Math.sqrt(dx * dx + dy * dy));
    }

    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19