2

I'm working on rendering a text on textview. I'm trying to add two buttons to zoom in and out the text inside text view and the code goes like this

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chapter_view);
        String chapter;
        chapter=getIntent().getExtras().getString("ChapterName");
        final StringBuffer buffer = new StringBuffer();
        int id= getResources().getIdentifier(chapter,"raw",getPackageName());     
        try{
            DataInputStream dataIO= new DataInputStream(getResources().openRawResource(id));
            String strLine= null;
            while((strLine = dataIO.readLine())!=null){
                buffer.append(strLine);
                buffer.append("\n");
            }
            dataIO.close();
        }catch(Exception e){

        }
        final TextView tv=(TextView) findViewById(R.id.chapter);
        tv.setText(buffer.toString());

        Button zoomIn = (Button) findViewById(R.id.zoomin);
        zoomIn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                textSize = tv.getTextSize();
                tv.setTextSize((float) (textSize+0.25));   

            }
        });

        Button zoomOut = (Button) findViewById(R.id.zoomout);
        zoomOut.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                textSize = tv.getTextSize();
                tv.setTextSize((float) (textSize-0.25));  
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.chapter_view, menu);
        return true;
    }
}

But the problem I'm getting is the even upon clicking zoom out button, it still increasing the font size of the text. Please help me out in this. Also once I close one chapter and opens another, the text size will reset to its default value. Are there any solutions regarding this. I'm thinking of using the namevalue pair for this solution.

user2064667
  • 124
  • 1
  • 12

4 Answers4

1

The problem is that the unit passed to setTextSize(float size) is in scaled pixels, whereas getTextSize() reports in pixels. Try using setTextSize(int unit, float size) instead, setting unit to TypedValue.COMPLEX_UNIT_PX.

Aert
  • 1,989
  • 2
  • 15
  • 17
  • And the tricky thing is that this behaviour will depends on screen density (sp to px ratio changes), which is probably why other users don't notice the issue with your code when running it on their device. – Aert Mar 11 '13 at 09:11
1

I've tried a sample at my machine. Check the code below. You have to place the variable at the right place with the scope.

public class MainActivity extends Activity {
    float textSize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView)findViewById(R.id.textView1);
        textSize = tv.getTextSize();

        Button btnPlus = (Button)findViewById(R.id.button1);
        btnPlus.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView tv = (TextView)findViewById(R.id.textView1);
                Log.v("TextSizeP", String.valueOf(textSize));
                textSize = (float) (textSize+0.25);
                tv.setTextSize(textSize);
                Log.v("TextSizeP", String.valueOf(textSize));
            }
        });

        Button btnMinus = (Button)findViewById(R.id.button2);
        btnMinus.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView tv = (TextView)findViewById(R.id.textView1);
                Log.v("TextSize", String.valueOf(textSize));
                textSize = (float) (textSize-0.25);
                tv.setTextSize(textSize);
                Log.v("TextSize", String.valueOf((float) (textSize-0.25)));
            }
        });
    }

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

}

For example I have my textSize variable declared at the class level. And the textSize variable is increased and decreased when the relevant button is clicked.

This works fine for me.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
0

As for the size that returns to its default , you will have to use shared preferences to save the value of the last text size .

As for the text size , why don't you increment the value one by one , for instance tv.setTextSize(textsize++) when zoomOut is clicked or tv.setTextSize(textsize--) when zoomIN is clicked

mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
0
public class MainActivity extends Activity {

TextView tv;

Button in,out;

static float textSize;

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

    tv = (TextView) findViewById(R.id.textView1);
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
    textSize = tv.getTextSize();

    in = (Button) findViewById(R.id.zoomin);
    out = (Button) findViewById(R.id.zoomout);

    in.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            textSize = (float)(textSize + 2);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);

        }
    });

    out.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            textSize = (float)(textSize - 2);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);

        }
    });

}

@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;
}

}
SKK
  • 5,261
  • 3
  • 27
  • 39
  • Try my edited code. I found the reason for such behavior here: http://stackoverflow.com/a/3687385/1567588 – SKK Mar 11 '13 at 09:26