2

I created a preference for setting (font size). And i define multiple array.xml for types mobiles and tablet (automatic) . I need that set a default font size for my setting (font size) automatically.

someCode.....getString("lstFontSize", "10"<======);I need set font size automatic.
A.A
  • 1,138
  • 1
  • 16
  • 29

4 Answers4

2

You have to create different folder for different default font size like

Mobile:-

res/values/dimens.xml(default)
res/values-ldpi/dimens.xml   (240x320 and nearer resolution)
res/values-mdpi/dimens.xml   (320x480 and nearer resolution)
res/values-hdpi/dimens.xml   (480x800, 540x960 and nearer resolution)
res/values-xhdpi/dimens.xml  (720x1280 - Samsung S3, Micromax Canvas HD)

Tablet:

-For tablet or bigger screen you have to create more specific folder like drawable-

large.res/values-large/dimens.xml    (480x800)
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Santosh Pandey
  • 190
  • 1
  • 1
  • 10
2

Use this :

public static int resSize = 0;
public static int resSizeTablet = 0;
public static int size()
{
    String prf = G.preference.getString("lstFontSize", (resSize != 0) ? String.valueOf(resSize) :String.valueOf(resSizeTablet));
    size = Integer.parseInt(prf);
    return size;
}
public static int DefaultFontSize(Activity act){
    Display display = act.getWindowManager().getDefaultDisplay();
    DisplayMetrics dm=new DisplayMetrics();
    display.getMetrics(dm);
    int p=isTabletDevice();
    if (p == 0) {
        int density=dm.densityDpi;
        switch(density){
        case DisplayMetrics.DENSITY_LOW:
            return resSize = 10;
        case DisplayMetrics.DENSITY_MEDIUM:
            return resSize = 10;
        case DisplayMetrics.DENSITY_HIGH:
            return resSize = 14;
        case DisplayMetrics.DENSITY_XHIGH:
            return resSize = 18;
        case DisplayMetrics.DENSITY_XXHIGH:
            return resSize = 24;
        case DisplayMetrics.DENSITY_XXXHIGH:
            return resSize = 28;
        }
    }
    return 0;

}

public static int isTabletDevice() {

    boolean xlarge = ((G.context.getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == 
                        Configuration.SCREENLAYOUT_SIZE_XLARGE);

    if (xlarge) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) G.context;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_HIGH:
            return resSizeTablet = 24;
        case DisplayMetrics.DENSITY_MEDIUM:
            return resSizeTablet = 20;
        case DisplayMetrics.DENSITY_XHIGH:
            return resSizeTablet = 28;

        default:
            break;
        }
    }
    return 0;
}

Then you can use :

Conver.setTextSize(TypedValue.COMPLEX_UNIT_SP,YourClassName.size());
0

Take a look at this post. and this post. The first post is a duplicate and the other is not but they both are on the same page and most probably something which might help you. In case your whole app is just a bunch of textViews then you can dynamically change the font size of the textViews like this:

TextView myTextView = new TextView(this);
myTextView .setTextSize(TypedValue.COMPLEX_UNIT_SP,14);   

Hopefully I helped.

Community
  • 1
  • 1
Zer0
  • 1,002
  • 1
  • 19
  • 40
0

Define your FontSize in res-->values-->dimens.xml file like

<dimen name="fontSize14">14sp</dimen>

and for tablet or other phone you can set accordingly in their specific folder like res-->value-sw360dp-->dimens.xml

<dimen name="fontSize14">16sp</dimen>

I am setting 16sp in sw360dp folder you can set as you want to

In TextView call android:textSize="@dimen/fontSize14" I hope i'll work for you

Fakhar
  • 3,946
  • 39
  • 35