0

I am having a requirement to create a app that has one of function to display the text file . In this there are two button to Zoom in and Zoom out . How should we do this using android native coding?

Balaji Sabdhar M
  • 418
  • 1
  • 6
  • 26

1 Answers1

1

I've found an answer that does this using Pinch to zoom: https://stackoverflow.com/a/14306988/1683141

I don't know if it is necesairy for you to actually have zoom in/out buttons or if pinch to zoom will do:

public class MainActivity extends Activity {

    TextView scaleGesture;
    ScaleGestureDetector scaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scaleGesture = (TextView)findViewById(R.id.article);
        scaleGesture.setText("this is some text");
        scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        scaleGestureDetector.onTouchEvent(event);
        return true;
    }

    public class simpleOnScaleGestureListener extends
            SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            float size = scaleGesture.getTextSize();
            Log.d("TextSizeStart", String.valueOf(size));

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


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

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

Also, here is for dynamically setting textview layout attributes (so you can also use it for font size): https://stackoverflow.com/a/4632208/1683141

    myTextView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
            //highlight the TextView
            myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
            myTextView.setBackgroundResource(R.color.highlightedTextViewColor);
        }
    });

Ofcourse, you'll need the folowing style references in style.xml

   <?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>

</resources>

And add the folowing to the "strings.xml" file like this:

 <color name="highlightedTextViewColor">#000088</color>
 <color name="normalTextViewColor">#000044</color>
Community
  • 1
  • 1
Mdlc
  • 7,128
  • 12
  • 55
  • 98