I have a method in my Android app that takes a dynamically set font size and returns a proportionally smaller font size. The value it gets from getTextSize() is for headlines and the smaller out value is for body text. Currently it’s written as:
public int getSmallerTextSize(){
int textSize = (int)Math.round(getTextSize() * 0.8);
if(textSize > 20){
textSize = 20;
}else if(textSize < 10){
textSize = 10;
}
return textSize;
}
I want to find a shorter and less clunky way to express this. One option is:
public int getSmallerTextSize(){
int textSize = (int)Math.round(getTextSize() * 0.8);
textSize = textSize > 10 ? textSize : 10;
textSize = textSize > 20 ? 20 : textSize;
return textSize;
}
But again: a lot of code for something so simple. Can someone suggest an elegant preferrably one-liner of code to express this?
Just use the following expression: