Akshay Patel Akshay Patel - 5 months ago 23
Android Question

How to set TextView text size using ZoomControls in Android Studio?

I want to Dynamically adjust TextView textsize in Android Application.

For this I am setting ClickListener for ZoomControls using following method.

I am getting correct output for zooming in action.

But for zooming out, I am not getting correct output. The Text size increases instead. I don't know where I am going wrong.

Please Help.

public void SetZoomControls()
{
//Zoom refers to ZoomControls
//main_code refers to a TextView
Zoom.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float previous= main_code.getTextSize();
previous++;
main_code.setTextSize(previous);
Toast.makeText(code_display.this, String.valueOf(main_code.getTextSize()),Toast.LENGTH_SHORT).show();

}
});

Zoom.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float previous1= main_code.getTextSize();
main_code.setTextSize(main_code.getTextSize()-1);
Toast.makeText(code_display.this,String.valueOf( previous1),Toast.LENGTH_SHORT).show();
}
});



}

Answer

By using this method setTextSize(int size), the unit type by default is "sp" or "scaled pixels".

You can use setTextSize(int unit, float size) to specify a unit type.The constant values for this can be found in the TypedValue class

TypedValue.COMPLEX_UNIT_PX   //Pixels

TypedValue.COMPLEX_UNIT_SP   //Scaled Pixels
Comments