A clearer way to get all the Typeface. And other little fixes.

This commit is contained in:
Rashiq Ahmad 2013-12-16 20:48:35 +01:00
parent cef231059d
commit fd6b229a7c
4 changed files with 74 additions and 18 deletions

BIN
assets/Parabaik.ttf Normal file

Binary file not shown.

View File

@ -133,19 +133,7 @@ public class KiwixMobileActivity extends FragmentActivity implements ActionBar.T
// Reset the Locale and change the font of all TextViews and its subclasses, if necessary
private void handleLocaleCheck() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String language = prefs.getString("pref_language_chooser", "");
if (language.isEmpty()) {
return;
}
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
LanguageUtils.handleLocaleChange(this);
new LanguageUtils(this).changeFont(getLayoutInflater());
}

View File

@ -1,6 +1,7 @@
package org.kiwix.kiwixmobile;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.EditTextPreference;
@ -8,6 +9,7 @@ import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.util.Log;
import java.util.Locale;
@ -44,10 +46,12 @@ public class KiwixSettings extends Activity {
prefList.setDefaultValue(prefList.getEntryValues()[0]);
String summary = prefList.getValue();
if (summary == null) {
prefList.setValue((String) prefList.getEntryValues()[0]);
summary = prefList.getValue();
}
prefList.setSummary(prefList.getEntries()[prefList.findIndexOfValue(summary)]);
prefList.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@ -69,6 +73,8 @@ public class KiwixSettings extends Activity {
LanguageUtils languageUtils = new LanguageUtils(getActivity());
languageList.setTitle(Locale.getDefault().getDisplayLanguage());
languageList.setEntries(languageUtils.getValues().toArray(new String[0]));
languageList.setEntryValues(languageUtils.getKeys().toArray(new String[0]));
@ -79,10 +85,13 @@ public class KiwixSettings extends Activity {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (!newValue.equals(Locale.getDefault().toString())) {
LanguageUtils.handleLocaleChange(getActivity(), newValue.toString());
// Request a restart when the user returns to the Activity, that called this Activity
setResult(RESULT_RESTART);
finish();
startActivity(new Intent(getActivity(), KiwixSettings.class));
}
return true;
}
});

View File

@ -2,12 +2,13 @@ package org.kiwix.kiwixmobile;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
@ -17,6 +18,7 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -43,6 +45,27 @@ public class LanguageUtils {
sortLanguageList();
}
public static void handleLocaleChange(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String language = prefs.getString("pref_language_chooser", "");
if (language.isEmpty()) {
return;
}
handleLocaleChange(context, language);
}
public static void handleLocaleChange(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
// Read the language codes, that are supported in this app from the locales.txt file
private void getLanguageCodesFromAssets() {
@ -126,6 +149,11 @@ public class LanguageUtils {
}
}
private boolean isLanguageSet() {
return false;
}
// Get a list of all the language names
public List<String> getValues() {
@ -177,8 +205,14 @@ public class LanguageUtils {
final View view = inflater.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
((TextView) view).setTypeface(Typeface.createFromAsset(
mContext.getAssets(), "DejaVuSansCondensed.ttf"));
TextView textView = ((TextView) view);
// Set the custom typeface
textView.setTypeface(Typeface.createFromAsset(
mContext.getAssets(), getTypeface()));
// Reduce the text size
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getTextSize() - 3f);
}
});
@ -192,6 +226,25 @@ public class LanguageUtils {
}
return null;
}
// This method will determine which font will be applied to the not-supported-locale.
// You can define exceptions to the default DejaVu font in the 'exceptions' Hashmap:
// Key: the language code; Value: the name of the font.
// The font has to be placed in the assets folder.
private String getTypeface() {
// Define the exceptions to the rule
HashMap<String, String> exceptions = new HashMap<String, String>();
exceptions.put("my", "Parabaik.ttf");
// Check, if an exception applies to our current locale
if (exceptions.containsKey(Locale.getDefault().getLanguage())) {
return exceptions.get(Locale.getDefault().getLanguage());
}
// Return the default font
return "DejaVuSansCondensed.ttf";
}
}
private class LanguageContainer {
@ -205,7 +258,13 @@ public class LanguageUtils {
// possible incompatibilities, since not all language names are available in all languages.
private LanguageContainer(String languageCode) {
mLanguageCode = languageCode;
mLanguageName = new Locale(languageCode).getDisplayLanguage(new Locale("en"));
mLanguageName = new Locale(languageCode).getDisplayLanguage();
// Use the English name of the language, if the language name is not
// available in the current Locale
if (mLanguageName.length() == 2) {
mLanguageName = new Locale(languageCode).getDisplayLanguage(new Locale("en"));
}
}
public String getLanguageCode() {