Android textview font based on language - android

Is there a general method for handling font like, if it is Chinese I use a custom font for it, if English then I fall back to the default font? Thanks.

Here is my 2 cents. You can use Calligraphy library to change the custom font easily either for whole app or for specific TextView as well.
If you want to do it for whole app, then one solution I can think of is:
In your app you can listen to the Broadcast of ACTION_LOCALE_CHANGED.
Inside the listener check to see whether the locale is Locale.SIMPLIFIED_CHINESE or Locale.TRADITIONAL_CHINESE, then change the app's font.
If you want to programatically change font of lets say partial text then you can use CalligraphyTypefaceSpan as mentioned Multiple Typeface's per TextView / Spannables section:
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append("Hello!") // Bold this
.append("I use Calligraphy"); // Default TextView font.
// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new
CalligraphyTypefaceSpan(TypefaceUtils.load(getAssets(), "fonts/Roboto-Bold.ttf"));
// Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic.
sBuilder.setSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(sBuilder, TextView.BufferType.SPANNABLE);
Hope it helps.
P.S: I am not affiliated to the library in any way. I am a happy user of it.

Related

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

Android textview different style for bold text

I have a long string, with multiple bold words inside the string.
The styling of the non bold sentences are done inside the TextView XML part and this is looking fine.
But i want to make the bold words bigger and thicker.
Is this possible to change this with XML?
for example:
<string name="long_text">This should be a <b>long</b> text with different <b>styling</b></string>
The bold words are supposed to be 1.5 times bigger
Or should i do this in the Java code?
yes you have to do this in java because there is no way to as per your requirement,
you can use TextAppearanceSpan in java code with Spannable
if i am getting you right than try the following..
textview.setText(Html.fromHtml(getResources().getString(R.string.long_text)));
Mark as right if it works for you .

Change attributes to a selected part of text in EditText

I've searched something around here but nothing came up.
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part.
I've already tried with the spannable thing, from another question:
TextView myTV = (TextView)findViewById(R.id.test);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
I assume that I have to assign it to some onClick method..Also my problems are those two number, I should put some "selectedText" there instead I think.
Is this even possible?
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part
If you want the user to "change attributes... of a selected part", my recently-updated RichEditText offers that. I do not have color or text size going yet, though, as those will need a toolbar (rather than my current action mode support).
Also my problems are those two number, I should put some "selectedText" there instead I think
You are probably looking for getSelectionStart() and getSelectionEnd().

How to return to default font in TextView

I know how to change the font of a TextView to my own font:
catTextView = (TextView)findViewById(R.id.CatText);
catTextView.setTypeface(Typeface.createFromAsset(getAssets(), "my_font_in_assets"));
However at a point during the app execution, I would like to set it back to the default font. I don't want to hardcode the font (write "Droid Sans") since the default font could change in future versions of Android (I think it actually did in ICS).
Is there any way to get the default font name of a TextView, and set it back to this after changing it?
Also, can I be sure the default font will always be able to show Russian or Japanese characters?
Thanks!
Use setTypeface(Typeface.SANS_SERIF) on a TextView
Have you tried calling getTypeface() in your TextView before you change it and store the result somewhere?
http://developer.android.com/reference/android/widget/TextView.html#getTypeface()
Use this:
textView.setTypeface(null);
You can restore default typeface using
Typeface.defaultFromStyle(R.style.YourTheme);

Multi Formatted TextView

I'm trying to have the layout of the comments on a progam in the following form:
I want the USER_NAME in Bold, and the DATE in Enphasis (The red part should be normal, I don't want it red). My problem comes with the red part. I cant get the cooment to be displayed like this using layouts, I've been able to get:
and
Is there any way to get the comment working like the one I'm showing. I've thought about a multi formatted textView, is it posible??
If getting the comment like this is not possible, just say it's imposible.
One possible solution is to put two TextViews into a Linear layout one after another and then use SpannableStringBuilder to assign the formatted text to the first view.
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(text);
sb.setSpan(createBoldSpan(), 0, lengthOf(userName),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
firstTextView.setText(sb, TextView.BufferType.NORMAL);
createBoldSpan should return TextAppearanceSpan
private TextAppearanceSpan userNameSpanInBold(String userName) {
return new TextAppearanceSpan(...);
}
please follow API docs for TextAppearanceSpan - you can create one using custom style.
You could use a WebView with loadData if HTML markup would be good, or alternatively delve into the realm of Spannable text.
I've not worked with Spannable text myself, but essentially it allows you to attach attributes (e.g. styles & colors) to parts of text in a TextView.
The relevant method is TextView.setText(CharSequence text, TextView.BufferType type) with BufferType of SPANNABLE. The documentation I've seen is.... confusing.

Categories

Resources