Adding new attribute flags to an existing attribute in Android XML - android

When using the XML to style your fonts, you can use either of these typeface flags on the android:textStyle attribute: bold, italic, normal.
I'm mapping this attribute with my custom font, so bold corresponds with my bold font, etc. Now for example I have a semi-bold style that I also want to use with this attribute, but currently the default textStyle attribute can only accept those 3 values. Is there any way I could make a new custom flag under that attribute? For example I want to be able to do this android:textStyle="semibold".
I can use other unused flag in place for the style I want to use but it's very unintuitive to write android:textStyle="italic" and have it show a semi-bold font.

Related

Theme/Style/Text appearance, which one takes precedent?

As my title, when I'm using a theme, style or text appearance, which one takes precedent?
Is there any way I can use all three of them? Thanks!
According to the docs, the priorities are as follows:
Applying character- or paragraph-level styling via text spans to
TextView-derived classes
Applying attributes programmatically
Applying individual attributes directly to a View
Applying a style to a View
Default styling
Applying a theme to a collection of Views, an activity, or your
entire app
Applying certain View-specific styling, such as setting a
TextAppearance on a TextView

How do I add bold to a custom font family then use it for strings?

I read on Stack Overflow how to create a custom bold font and a regular one:
How to create custom font family with bold font
But I'm struggling to understand how to implement this in my strings. Ordinarily you simply use <b></b> to make text bold, but this doesn't seem to work with my custom fonts. All the text remains the same.
I know that you have tried adding the bolded font to a font family, but it hasn't worked. You can try one of the ways below. It will work even if you do import a TTF font.
One way you can do it is in strings.xml
You can create a new string and then add the b to bold it like this:
<string name="my_string"><b> My String</b></string>
You will need to create one for each string that you want to bold.
Then in your layout file, you can set the android:text to one of the strings from strings.xml and you can set the android:fontFamilyto the TTF font.
You can also bold it programmatically using the SpannableString in Java. Check this article to see how to bold programmatically.
Hope it helps!

Setting font in Android

In android Using setTypeface method we can set the font to the control of our wish programmatically, but i want to know is there a way we can avoid this and set the font in layout XML file itself?
I Just want to specify the path of the file & font should get updated automatically.
You can use Calligraphy library where you can specify font in XML itself.
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>
Yes. You need to extend the view you want to set its font in xml and then make an custom attribute (as enum) to be accessed from xml and call it for example "customFont".
Then in the extended view java code get the "customFont" value and change the font programaticaly.
Then you can use this custom view and set its font through xml ;)
Its possible for some limited font becuase you must define an enum for xml and see in java code wich font was selected and then set it in java code set the selected font.
Though you can set system font in xml without all these. And my explanation was for your custom fonts ;)

How to set font weight as light, regular in Android

I have 3 text views.
I need to set their weight as Light, Regular and Condensed.
Can someone help me on how to achieve this in Android?
Use android:textStyle on a TextView to set the text style like bold, italic or normal.
Here is an example of a TextView with a bold text style:
<TextView
android:id="#+id/hello_world"
android:text="hello world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
If you want to use light or condensed, you will have to change your font. You can do it like this:
android:fontFamily="sans-serif-light"
For more information about fonts, please also look at the following answer Valid values for android:fontFamily and what they map to?
You can only show a TextView as Light, Regular and Condensed if the font that you're referencing allows those styles.
For example, if you import a font into your project and it doesn't have Light or Condensed, I don't think it's (dare I say) possible to make the text appear with that style in your TextView. If you are importing your own font, work with it programmatically e.g. In your activity declare a global TextView:
private TextView tv;
Then in onCreate(), reference the fontfile you save in /assets:
Typeface someFontName-condensed = Typeface.createFromAsset(getAssets(), "assets/NameOfFont-condensed.ttf");
tv = (TextView) findViewById(R.id.myTextViewId);
tv.setTypeface(someFontName-condensed);
Notice that I imported the condensed version of my font file (as .ttf), not as a packaged font .ttc. You can bring in various style versions of a font, but you should bring them in as individual .ttf files instead of one packaged file because you will want to reference the specific style .ttf.
However, if you're using a system font that allows you to reference various styles from your xml, see what they say here:
Valid values for android:fontFamily and what they map to?
As they say, for something like Roboto, you'll use the fontFamily.

Android find language and set Fontface in android

Json array will contain list of string data. Each elements of the JsonArray in different language.How to find that language and set fontface in android.
For example ["tamil-font data","english - font - data","Hindi data"] like this.
About finding the language, I would recommend to add an indicator to the JSON.
About font face, make sure you have all font that you need:
2.1 Place the font to assets folder.
2.2 Any View that inherits from TextView can change its font, for example:
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/epimodem.ttf");
tv.setTypeface(face);
2.3 if you need to change the text style linke bold or italic use in the TextView's XML attribute:
android:textStyle="italic" android:typeface="serif"
Tip: If need the same font more than one one time, reuse the Typface variable to avoid overhead.

Categories

Resources