Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 button for increase and decrease TextView font size

btnZoomin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtview.setTextSize(txtview.getTextSize()+1);
}
});
btnZoomout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtview.setTextSize(txtview.getTextSize()-1);
}
});
but this code doesnt work!!!!
Posted
Updated 29-May-20 7:29am

See the description at http://developer.android.com/reference/android/widget/TextView.html#getTextSize()[^]; the size is in pixels, which is quite a small increases on an Android screen. You need to use a larger value, based on the actual font size.
 
Share this answer
 
Just modify the lines;

txtview.setTextSize(txtview.getTextSize()+1);

with

txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP, txtview.getTextSize() + 1);

and

txtview.setTextSize(txtview.getTextSize()-1);

with

txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP, txtview.getTextSize() - 1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900