Let the user change the font size
in my dimens.xml I am:
<dimen name="text_small">16sp</dimen>
<dimen name="text_normal">18sp</dimen>
<dimen name="text_medium">20sp</dimen>
<dimen name="text_big">22sp</dimen>
now I would like to let the user select the font size in the settings snippet. Let's say for example:
- Small -2sp
- Normal + 0sp
- Medium + 2sp
- Big + 4sp
So, for example, if the user selects Large, I would like this font size to be:
<dimen name="text_small">20sp</dimen>
<dimen name="text_normal">22sp</dimen>
<dimen name="text_medium">24sp</dimen>
<dimen name="text_big">26sp</dimen>
Is there a way to do something like:
Application start:
if (sizeUser.equals("Big")) {
text_small=24sp
.....
}
etc.?
source to share
Rather than calling setTextSize
for each TextView in each Control, I would create a custom class that extends the TextView and then contains logic to set the text size to setTextSize
.
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setTextSize (int unit, float size){
switch(USER_SET_SIZE){
case SMALL:
setTextSize(TypedValue.COMPLEX_UNIT_SP, -2);
break;
case MEDIUM:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 2);
break;
case LARGE:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 4);
break;
case NORMAL:
default:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 0);
break;
}
}
}
Or, if you are using multiple views and want to control the text size of each one, I would recommend using themes, then change the theme based on the font size. Just call setTheme()
your activity onCreate()
.
Your theme files should look like this:
<style name="NormalSizeTheme" parent="@style/MyTheme">
<item name="android:textSize">0sp</item>
</style>
source to share