I am trying to use a custom font in a password hint field in Android Studio.
After I set the Edit Text as inputType="textPassword" in XML, the font changes to a different from the one I've chosen. All the other Edit Texts have the correct custom font.
I've tried following this solution in java code (replaced Typeface.DEFAULT with R.font.my_font) but nothing happened
EditText password = (EditText) findViewById(R.id.register_password_text);
password.setTypeface(R.font.my_font);
password.setTransformationMethod(new PasswordTransformationMethod());
I've also tried this solution with no success
Typeface cache = edtPassword.getTypeface();
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edtPassword.setTypeface(cache);
Any ideas on what to try or what might be missing in these solutions?
PS: I'm using montserrat_regular font
Its happening because you are passing the id of the font in
password.setTypeface(R.font.my_font) not the TypeFace Object. You need to pass TypeFace object in setTypeFace() method , so to make TypeFace object from your font resource Id just add the code
Typeface typeface = ResourcesCompat.getFont(this, R.font.my_font);
password.setTypeface(typeface);
I think this should work.
Related
I have some layout already setup font for textView.
android:fontFamily="#font/cookie_regular"
But i want to have more setting option to clear/set default font when i need in Java code.
Do you have any suggestion ?
Call this method when you want to change some textview to a different font.
public void change_font(){
//if or switch statement may be needed or not.
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/THE_FONT_I_HAVE_CREATED_OR_DOWNLOADED.ttf");
//FYI font is a file you can create in your res folder.
myTEXTVIEW.setTypeface(custom_font);
}
Lastly more information about downloadable fonts
https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts
I have a textInputLayout with a TextInputEditText.
I would like the hint text to be in Roboto and the edit field (the text that user is entering) to be of a custom font, but can't seem to get it working. Somehow if I try to set a font on the TextInputEditText it also affects the hint text's font.
Anyone know how to get this issue solved?
*** edit: Found some weird thing. When I set the inputType to textPassword on the TextInputeditText (and have my custom font set on same widget via fontFamily=".." then I do have two different fonts on hint and edit field?!! But unfortunately for any other input type and in my case I need inputType="textEmailAddress" the hint text changes to same font as TextInputEditText
I found the solution:
You need to set typeFace on the TextInputLayout programmatically for the font you want for the hint text.
The font for the TextInputEditText fields can be set directly in either xml or programatically.
Default EditText does not offer a method to set the Typeface of the hint only, but if you set a custom typeface to your EditText, all the text fields are affected.
To obtain what you want, you need to define a custom TypefaceSpan and a SpannableString, as suggested in this topic.
Please have a look at francisco_ssb answer.
I am new to android development, I need to implement Rich text Editor in my Application. So i googled and found some solutions. I am using this solution:
http://code.google.com/p/android-richtexteditor/source/browse/?r=4#svn/trunk/src/net/sgoliver
It supports the Bold, Italic and Underline in it. Now I want to change the font-style of selected text in EditText, How do i do that?
I know how to set the font-style for all the text in EditText. Suggestions will be appriciated!
Copy the font files (*.ttf) to the assets folder of your application.
Consider I have copied the font file "verdana.TTF" to the assets folder, then the following code will set the font to verdana
EditText et=(EditText) findViewById(R.id.editText1);
Typeface tf = Typeface.createFromAsset(getAssets(),"verdana.TTF");
et.setTypeface(tf);
Hope you got the solution
editText.setTypeface(Typeface.SERIF); as like as TextView.
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Comic.ttf");
EditText et = (EditText)findViewById(R.id.et);
et.setTypeface(tf);
you can place ttf file in assets and set font style here
try this code..create font folder like this assets/font and put your font file in it..
edittxt = (EditText) findViewById(R.id.edittxt);
Typeface myTypeface = Typeface.createFromAsset( this.getAssets(), "font/YOURFONT.otf");
edittxt.setTypeface(myTypeface);
I want to change the font face and styles in android. How to do this? Am using emulator. Is this possible to change in emulator. Is there any permissions available to do that. Thanks in Advance.
in android only five fonttype-face
TypeFace
Typeface.DEFAULT
Typeface.MONOSPACE
Typeface.SANS_SERIF
Typeface.DEFAULT_BOLD
Typeface.SERIF
and font-type
Typeface.NORMAL
Typeface.BOLD
Typeface.BOLD_ITALIC
Typeface.ITALIC
you can use like as
textView.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
and other way is you can download any .ttf file and put it in asset folder and use like as this
Typeface type=Typeface.createFromAsset(context.getAssets(),
"DroidSansMono.ttf");
textView.setTypeface(type, null);
You can change the font style and font face in your XML files. For example if you want to change your Button's style and face. Choose your button in xml and follow the below methods :
Button -> Right Click -> Properties -> Typeface and style
Hope this will help you.
If you want to change in code ;
This is good reference;
http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/
what you want?. Do you want to change the font face and styles of text in textview. if yes then use this
Typeface type = Typeface.createFromAsset(this.getAssets(),"your font name");
text.setTypeface(type);
and for styles use these links:
Style-resource
Themes
If you want to use a font which is not default (like "Helvetica") for the text in Android application then put the font file(Helvetic.ttf) in assets folder and do use this code as below:
Typeface tf= Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
TextView mTextView =(TextView) findViewById(R.id.text);
mTextView.setTypeface(tf);
if u want to use custom font jus download .ttf file and put the file into assets folder
and use like this:
TextView tv=(TextView)findViewById(R.id.textView);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/arial.ttf");
tv.setTypeface(tf);
Hi want to change the font type to atext according to spinner selection like arial balck,Thlist etc.any one have idea ? suggest me
You should change the typefaces of your text views. You can specify a variable:
Typeface typeface = Typeface.DEFAULT;
Then when the user chooses another font you reinitialize this variable, using the built-in typefaces, or loading your own from .ttf files, that you can place in assets/fonts directory. Loading own fonts can be done with this call:
typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf");
This should do it.