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"));
Related
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);
I have a simple app with jokes. There are buttons where I set the text with this code:
bt_barjokes.setText(getString(R.string.bar_jokes)+ " ("+barjokes.size()+")");
Works perfectly and it gives the correct title and size of the used Arraylist: Bar Jokes (55)
The problem is I want to change the color and font size of (55):
("+barjokes.size()+")")
I have tried several of the suggested solutions in this forum but can't get it to work.
You can use ForegroundColorSpan to change text color, and AbsoluteSizeSpan or RelativeSizeSpan to change font size.
You can look #xizzhu 's answer, its ok additionally you can use SpannableString or Html.fromHtml
setText(Html.fromHtml("<font color=#343434>" +"<small>"+ "Little Black Text" +"</small>"+
"</font>"+"  "+"<font color=#123456>" +"<big>" +"Big Blue Text" +"</big>"+ "</font>"));
#Edit : If you wanna use this in your button add this line in xml like: android:textAllCaps="false"
You can use : <small> or <big> , tested in Toolbar:
and in Button, its looking like :
I have an app where I want the user to be able to select a font. I've already downloaded fonts in the assets folder of the project.
Now I want to merge all the fonts in a ListView.
Then I want to set an onClickListener and want to get the selected font.
I want each line of the ListView to display the font name and the right font for that name.
You can get your fonts with this way
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"font.ttf");
So whenever user clicked on an item of ListView store name of the font in a shared preferences then you can get selected font with this :
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
PreferenceManager.getDefaultSharedPreferences(context).getString("selected_font_name"));
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.
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..