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.
Related
I want to apply multiple text-style on textview programmatically in android.
val myView : TextView = findViewById(R.id.date_title)
// now I want to apply - Bold & italic style on myView at same time like we can do in XML file e.g android:textStyle="bold|italic"
myView.setTypeface(null, Typeface.BOLD)
myView.setTypeface(null, Typeface.ITALIC)
But as result I get only italic name how I will get both at same time
You can follow this link : How to set the font style to bold, italic and underlined in an Android TextView?
Or you can try this myView.setTypeface(null, Typeface.BOLD_ITALIC);
or appy directly in XML file as well:
android:textStyle="bold|italic"
you can as well use attributes ,in this use text-style attributes and go inside there will be checkbox for normal, bold, italic. If you want to that font is bold as well as italic then check it and make it true .It will work easily.
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.
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!
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.
I know ho to set custom font programmatically inside Android app.
Is there any way to load typeface for custom font (assets) and Android framework will use proper file based on bold, italic and so on?
For example now I'm trying to set Roboto font to some TextView
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);
It works ok. But since I set TextView inside xml layout to bold , text is not bolded
<TextView
android:id="#+id/my_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:textStyle="bold"
android:gravity="center"
android:text="#string/my_text"
android:textColor="#color/my_foreground"
android:textSize="24dp" />
How to load typeface from assets properly that this will work?
textView.setTypeface(typeface, Typeface.BOLD);
Inside my assets dir there is only one "font family"
Roboto-Black.ttf
Roboto-BlackItalic.ttf
Roboto-Bold.ttf
Roboto-BoldCondensed.ttf
Roboto-BoldCondensedItalic.ttf
Roboto-BoldItalic.ttf
Roboto-Condensed.ttf
Roboto-CondensedItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Medium.ttf
Roboto-MediumItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf
How to load all that fonts inside one typeface/family?
Not sure if it's a good idea to post a link, but anyway...
Here is my blog post on this topic, it has a class that solves this problem fully.
http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/
I don't know of a way to treat different typeface variants of a single font as one family, but typefaces tend to have large file sizes so you probably wouldn't want to import all those typefaces into your app anyway. Instead, you could use just the Medium font and then set bold and italic attributes for that.
For example, if you have android:textStyle="bold" set in your XML layout, you can do this in your code to keep the bold style:
Typeface currentTypeFace = textView.getTypeface();
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) {
textView.setTypeface(tf, Typeface.BOLD);
} else {
textView.setTypeface(tf);
}