5

in my dimens.xml I've:

<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 a settings fragment. Let's say for example:

  1. Small -2sp
  2. Normal +0sp
  3. Medium +2sp
  4. Big +4sp

So for example if the user select "Big" I would like that font size will 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
.....
}

and so on?

helloimyourmind
  • 994
  • 4
  • 14
  • 30

2 Answers2

1

Rather than have to call setTextSize for every TextView in every Activity I'd create a custom class that extends TextView and then contains the logic for setting the text size in its 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 I'd recommend using themes then change your theme based on your font size. Simply call setTheme() in your Activity's onCreate() first.

Your theme files should look like this

<style name="NormalSizeTheme" parent="@style/MyTheme">
    <item name="android:textSize">0sp</item>
</style>
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
  • I would like to use another approach because my text size are used by a lot of differente control, I simply want to say to the app, to use another file dimension at start or something like that. – helloimyourmind Jun 24 '15 at 19:19
  • With your edit I can't use 4 different font size, you are setting android:textSize, while I need to set my 4 attributes that I use in android:textSize. – helloimyourmind Jun 24 '15 at 23:49
  • You will have multiple theme files, they will all look like that but with the value changed depending on font size you are using. You then switch between them by calling setTheme – Chris Stillwell Jun 25 '15 at 00:16
  • Maybe you don't understand my situation: 1 xml dimens file with 4 textSize, 3 fragment xml layout file with view that sometimes uses textSizeSmall, sometimes the normal one and so on. So I can't set "android:textSize" because in the same fragment I have views that uses all of the 4 size. – helloimyourmind Jun 26 '15 at 01:28
0

You don't need to use dimens.xml. Instead, use setTextSize

TextView tv = findViewById(R.id.textView);

if ("Big".equals(sizeUser) {
    tv.setTextSize(26);
}
mattfred
  • 2,689
  • 1
  • 22
  • 38
  • 1
    I would like to use another approach because my text size are used by a lot of differente control, I simply want to say to the app, to use another file dimension at start or something like that. – helloimyourmind Jun 24 '15 at 19:20