How to change the font language in android - java

In my app i want to convert my english font into hindi, for that i did localization. that is not supported for lower versions(i.e 2.3). Then i downloaded "shusha.ttf" file and added to assets/fonts folder. Then i set the typeface for a textviews. Like this i am able to convert the english font to hindi. but when i get the text from the text view it is not showing in Hindi(getting the english font). i am using that text to send the mail through gmail.
If any body have idea about this please give me a suggestion.
This is my Code
t = ((TextView)findViewById(R.id.textView1));
t2 = ((TextView)findViewById(R.id.textView2));
Typeface Hindi = Typeface.createFromAsset(getAssets(), "fonts/shusha.ttf");
t.setTypeface(Hindi);
t.setTextSize(20);
String hello=" Hyderabad, Andhra Pradesh, India ";
t.setText(hello);
String lang=t.getText().toString().trim();
t2.setText(lang);

Instead of using and playing with font, use UNICODE characters for hindi words. I had done it for a project of mine and it works great.

You have to change the font of the text of text view.
First put the font in your asset folder.
Then in your java file add code like this to change your font:
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),
"shusha.ttf");
TextView digital= (TextView) findViewById(R.id.your_textView);
digital.setTypeface(myTypeface);
Put shusha.ttf in your asset folder of your project.
This will might help you.

Create styles in values/styles like,
<style name="MyStyle" parent="#android:style/Theme">
<item name="typeface">font_in_assest.ttf</item>
</style>
Here, font_in_assest.ttf is the font you placed in your assests/fonts.
Then assign the style for you view,
<TextView
android:id="#+id/title"
style="#style/MyStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
It will be helpful for you..

Related

How to apply two different font to a TextView?

My case is same as Android - three sentences, two styles, one TextView. The only difference is that I want to set a Custom Font on the whole text. I have included roboto Font as an assets in my project and want to apply that font to the TextView with first part of the text will be roboto_LIGHT and middle part roboto_NORMAL and remaining part roboto_LIGHT again. Any suggestions how it can be done ?
Text needed in following format. Custom text with different styles and single textview.
You can use some html tags like <b> for bold with the following code:
textView.setText(Html.fromHtml("Login to the <b>Web Wallet</b> on PC or MAC"));
Then you can define your custom font family as in How to create custom font family with bold font
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="#font/roboto_LIGHT" />
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="#font/roboto_NORMAL" />
</font-family>
The weight 700 is the font for bold style. And then you should apply that created font family to the TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontFamily="#font/my_custom_font"/>
Or you can add 3 TextViews in a ConstraintLayout and give them some constraints to be in the positions you want otherwise.

Java: How to add a png image to the middle of a sentence

It should look like this:
I'm specifically using android studio. I currently have a textview and my text in the strings.xml file, but I understand there are other ways of doing the code. I have several paragraphs to write, so I figured this would be the best place to insert the text. I will need to include an image, a button, and links straight into the sentences I'm writing. (Any advice for each of those would be helpful) But, my first issue is that I don't know how to add the image into the middle of the sentence. All relevant posts are incomplete or outdated.
I would suggest if you wish to display large amount of text then use something like WebView with some html template engine.
To achieve this you need to use setCompoundDrawablesWithIntrinsicBounds() method of TextView. Heopfully this will help you.
Or you can use **SpannableStringBuilder ** for this to get. I hope this will help you.
UPDATED: Here sample is available. I am attaching link as well. It will help you in your implementation
https://stackoverflow.com/questions/15352496/how-to-add-image-in-a-textview-text
I have implemented this type of code in my app but I was used dependency which I describe here...
Implement this dependency in your build.gradle file
implementation 'org.sufficientlysecure:html-textview:4.0'
and then write this line in your build.gradle(Root level) or on the settings.gradle file
repositories {
jcenter()
}
Layout.xml
<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="#+id/textView"
android:textAppearance="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="13dp" />
In your Activity.java file in OnCreate Method
TextView textView;
textView = (TextView) itemView.findViewById(R.id.textView);
textView.setHtml("<b>Hello</b> this is my Text <span/> <img src="yourimage.jpg"> <span/> Another Text, new HtmlHttpImageGetter(myHolder.question));

Edit text view with image and text wrapped around image

I am building a rich text editor.
I have implemented text formatting like bold italic etc and also paragraph formatting like blockQuote. Now I would like to add images in editor and text should wrap around it.
I have implemented all these using SpannableString() and Spanned() and StyleSpan().
I can add image to a line using ImageSpan(), but that add it inline and its just there in place of a character
, what I want is to insert it in paragraph and rest of text should wrap around it. I am able to add image at the beginning of text by following code.. but I cannot align it center and right.
SpannableString string = new SpannableString("Text with icon and padding");
string.setSpan(new IconMarginSpan(bitmap, 30), 0, string.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
How to do it ? any example? or what procedure to follow?
you can fulfill your requirement like below:
you can set your image in imageview and your text in Html.fromHtml("here put your text")
gradle file:
compile 'com.github.deano2390:FlowTextView:2.0.5'
your XML layout:
<uk.co.deanwild.flowtextview.FlowTextView
android:id="#+id/ftv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="400dip"
android:padding="10dip"
android:src="#drawable/android2"/>
</uk.co.deanwild.flowtextview.FlowTextView>
your java file:
FlowTextView flowTextView = (FlowTextView) findViewById(R.id.ftv);
Spanned html = Html.fromHtml("here put your text");
flowTextView.setText(html);
you may get help by this link https://github.com/deano2390/FlowTextView also
I hope it help you..!
As per details given by you, It seems you might be using native EditText or you are trying with your custom EditText. Please provide this details to get exact solution of your query.
Apart from that, To build Rich Text Editor with the features mentioned by you (like Bold, Italic, Quote and Images etc), you can use this library RichEditor for Android: https://github.com/wasabeef/richeditor-android. It provides many features which may interest you.
Try with:
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);

Changing the fonts with "Century Gothic" in Android

I want to change the font to Century Gothic, I had some explanations and fixing but I got nothing, Im a newbie :) .. like
1 Typeface font = Typeface.createFromAsset(getAssets(), "Century-Gothic.ttf");
2 TextView tv=(TextView) findViewById(R.id.textView0);
3 tv.setTypeface(font);
Please help?
To use custom font for android app follow this steps
1- Add the custom font inside assets folder
2- Define the font
Typeface myfont = Typeface.createFromAsset(context,"myfont.ttf");
Now you can use myfont for textview
mytextview.setTypeface(myfont);
This works for me in android studio 3.1.3:
Copy the font inside font folder.
Select the font on fontFamily attributes.

Java, Changing fonts and text colour.

This is more of a general question, as I am still a beginning in the language of Java. I would like to know how can I make the font or text different? As well as its colour. An example for this is the MSN live messenger, for PC and Mac. In the chatbox, you can change the font and colour of the text, and it is also viewable by others, how can I make it like that for android?
In the assets folder of your project, create a new folder named fonts. Put .ttf files in there. (You can download some here
) These are files that contain fonts. Then, just to give you an idea of how to apply these fonts, here is an example of applying a font to a text view...
myTextView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/frquad.ttf"));
As for color, use this method...
myTextView.setTextColor();
Hopefully this has been of help.
// Font path
String fontPath = "fonts/fontname.ttf";
// text view label
TextView txt = (TextView) findViewById(R.id.txt);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txt.setTypeface(tf);
Typeface typeFace = Typeface.createFromAsset(
context.getAssets(),
"fonts/" + "abcd.ttf");
textView.setTypeface(typeFace);
change the color:
radio2.setTextColor(Color.parseColor("#bcbddc"));

Categories

Resources