Apply multiple style on TextView - android

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.

Related

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!

How to get type face style of a custom font in android?

After using custom fonts, I can't get type face style (BOLD, ITALIC, BOLD_ITALIC) of a text view by :
textView.getTypeface.getStyle();
In fact setting new type face style doesn't apply to textView really, although we can see the style is changing visually. I set type face style like this:
Typeface font = Typeface.createFromAsset(getAssets(), "sampleFont.ttf");
textView.setTypeface(font, Typeface.BOLD);
But,
textView.getTypeface.getStyle();
returns 0 (NORMAL) instead of 1 (BOLD)!
All these happen when I trying to use a regular font.
Typeface also has the methods isBold() and isItalic() since API level 1.
Or try this
if(textView.getTypeface()!=null){
if(textView.getTypeface().getStyle()==Typeface.BOLD || textView.getTypeface().getStyle()==Typeface.ITALIC){
//your code goes here
}
}

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.

Programmatically set the Typeface and set text style in xml not working

I have a textview defined in xml, now i am setting the Typeface programmatically like this:
textView.setTypeface(APP_FONT_REGULAR); // which is custom font in assests (.ttf file).
now i have set android:textStyle="bold" in xml
but it's not applying bold?
what is the reason i need to apply my own Typface and set it to bold also.
i need to apply my own Typface and set it to bold also.
You can set appearance when you call setTypeface() like this
textView.setTypeface(APP_FONT_REGULAR, Typeface.BOLD);
it solve your problem. Check it out.

Android set Roboto font with bold, italic, regular,... (something like custom font family)

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);
}

Categories

Resources