Android: Application-Wide Font-Size Preference

Android: application-wide font-size preference

Here it's how I made it for my app.
In a few words - in Activity.onCreate() you get resource id of style with specific set of font sizes and apply this style to theme of activity. Then with preferences activity you can switch between these sets.

First of all in values/attrs.xml declare attributes for set of font sizes:

<declare-styleable name="FontStyle">
<attr name="font_small" format="dimension" />
<attr name="font_medium" format="dimension" />
<attr name="font_large" format="dimension" />
<attr name="font_xlarge" format="dimension" />
</declare-styleable>

Then in values/styles.xml declare few sets of font sizes:

<style name="FontStyle">
</style>

<style name="FontStyle.Small">
<item name="font_small">14sp</item>
<item name="font_medium">16sp</item>
<item name="font_large">18sp</item>
<item name="font_xlarge">20sp</item>
</style>

<style name="FontStyle.Medium">
<item name="font_small">18sp</item>
<item name="font_medium">20sp</item>
<item name="font_large">22sp</item>
<item name="font_xlarge">24sp</item>
</style>

<style name="FontStyle.Large">
<item name="font_small">26sp</item>
<item name="font_medium">28sp</item>
<item name="font_large">30sp</item>
<item name="font_xlarge">32sp</item>
</style>

Then in onCreate() method of every activity add:

getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true);

where Preferences is a facade to SharedPreferences object:

public class Preferences {
private final static String FONT_STYLE = "FONT_STYLE";

private final Context context;

public Preferences(Context context) {
this.context = context;
}

protected SharedPreferences open() {
return context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
}

protected Editor edit() {
return open().edit();
}

public FontStyle getFontStyle() {
return FontStyle.valueOf(open().getString(FONT_STYLE,
FontStyle.Medium.name()));
}

public void setFontStyle(FontStyle style) {
edit().putString(FONT_STYLE, style.name()).commit();
}
}

and FontStyle is:

public enum FontStyle {
Small(R.style.FontStyle_Small, "Small"),
Medium(R.style.FontStyle_Medium, "Medium"),
Large(R.style.FontStyle_Large, "Large");

private int resId;
private String title;

public int getResId() {
return resId;
}

public String getTitle() {
return title;
}

FontStyle(int resId, String title) {
this.resId = resId;
this.title = title;
}
}

And FontStyle.values() is used as items for Spinner in your PreferencesActivity.
That's how mine looks like:

public class PreferencesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true);
super.onCreate(savedInstanceState);

setContentView(R.layout.preferences);

Preferences prefs = new Preferences(this);

Spinner fontStylesView = (Spinner) findViewById(R.id.font_styles);
FontStylesAdapter adapter = new FontStylesAdapter(this,
R.layout.font_styles_row, FontStyle.values());
fontStylesView.setAdapter(adapter);

fontStylesView.setSelection(prefs.getFontStyle().ordinal());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.preferences, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_done:
onMenuDone();
finish();
return true;
case R.id.menu_cancel:
finish();
return true;
default:
return false;
}
}

private void onMenuDone() {
Preferences prefs = new Preferences(this);

Spinner fontStylesView = (Spinner) findViewById(R.id.font_styles);
prefs.setFontStyle((FontStyle) fontStylesView.getSelectedItem());
}
}

And finally you can use your font size preferences:

<TextView android:textSize="?attr/font_large" />

Or I prefer using styles, in values/styles.xml add:

<style name="Label" parent="@android:style/Widget.TextView">
<item name="android:textSize">?attr/font_medium</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>

<style name="Label.XLarge">
<item name="android:textSize">?attr/font_xlarge</item>
</style>

And you can use it in this way:

<TextView style="@style/Label.XLarge" />

I hope my answer will help you.

How to change the font size of the application from setting preference?

you can define a setting code in your forms and do like this:

 private void loadSettings() {

//load font from global unit named G
G.typeFace = Typeface.createFromAsset(getAssets(), "fonts/BYekan.ttf");

TextView titleText = (TextView) findViewById(R.id.reg_title);
TextView regTitleText = (TextView) findViewById(R.id.reg_title);
TextView bigText = (TextView) findViewById(R.id.textView_big);
TextView button_titlet = (TextView) findViewById(R.id.reg_botton_title);
btnReg = (Button) findViewById(R.id.btn_reg);

titleText.setTypeface(G.typeFace);
bigText.setTypeface(G.typeFace);
regTitleText.setTypeface(G.typeFace);
button_titlet.setTypeface(G.typeFace);
btnReg.setTypeface(G.typeFace);

titleText.setTextSize(20);
bigText.setTextSize(20);
regTitleText.setTextSize(20);
button_titlet.setTextSize(20);
btnReg.setTextSize(20);

//end of fonts

}

how to prevent system font-size changing effects to android application?

If you require your text to remain the same size, you'll have to use dp.

To quote the documentation:

An sp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).

Emphasis mine.

So you're seeing the expected behaviour for using sp as your units for text size.

I don't understand what you mean about using dp taking too long to maintain your app - as far as I can tell, it'll exactly be the same amount of effort? (perhaps less, though it'll likely make it less usable for users with poor eyesight)

Font size of TextView in Android application changes on changing font size from native settings

Actually, Settings font size affects only sizes in sp. So all You need to do - define textSize in dp instead of sp, then settings won't change text size in Your app.

Here's a link to the documentation: Dimensions

However please note that the expected behavior is that the fonts in all apps respect the user's preferences. There are many reasons a user might want to adjust the font sizes and some of them might even be medical - visually impaired users. Using dp instead of sp for text might lead to unwillingly discriminating against some of your app's users.

i.e:

android:textSize="32dp"

android make application sensitive to font size

The font size feature in Spare Parts is a holdover that doesn't have any effect on anything else.

How to change the Font Size in a whole Application programmatically, Android?

The probable solution would be you create a base class which extends TextView, and use this text view class as edit text. Hope you are asking for size in first screen. In any case, u set the text size in the base class. This will solve your problem.

like u create this class in package com.example and class name is BaseTextView, then in xml file instead of <TextView .../> you will write <com.example.BaseTextView ... />

Hope this helps.



Related Topics



Leave a reply



Submit