Can I change the language of the Android app?
2 answers
This is the way to change the font of the font using xml.
-
Define a new name:
xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
("com.nound.test" is the package name as defined in Manifest.xml) -
Define a custom attribute in the /res/values/attrs.xml file.
<resources> <declare-styleable name="TypefacedTextView"> <attr name="textStyle" format="string"/> </declare-styleable> </resources>
-
Java class that extends TextView ... as follows Public class TypefacedTextView extends TextView {
public TypefacedTextView(Context context, AttributeSet attrs) { super(context, attrs); //Typeface.createFromAsset doesn't work in the layout editor. Skipping... if (isInEditMode()) { return; } TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView); //String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface); String fontNameForBold = styledAttrs.getString(R.styleable.TypefacedTextView_textStyle); styledAttrs.recycle(); if (fontNameForBold!=null && fontNameForBold.equals("bold")) { Typeface typeface_bold = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma_bold.ttf"); setTypeface(typeface_bold); }else{ Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma.ttf"); setTypeface(typeface); } }
-
Here is the layout code where you want to change the font of the font. in this "com.nound.test.ui" is the above (3-dot) java file package name. TypefacedTextView is the name of the class.
<com.nound.test.ui.TypefacedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Custom fonts in XML are easy_bold font"
your_namespace:textStyle="bold" />
<com.nound.test.ui.TypefacedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Custom fonts in XML are easy_regular font"
/>
it's enough to do it ...
0
source to share