Setting font in Android - android

In android Using setTypeface method we can set the font to the control of our wish programmatically, but i want to know is there a way we can avoid this and set the font in layout XML file itself?
I Just want to specify the path of the file & font should get updated automatically.

You can use Calligraphy library where you can specify font in XML itself.
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>

Yes. You need to extend the view you want to set its font in xml and then make an custom attribute (as enum) to be accessed from xml and call it for example "customFont".
Then in the extended view java code get the "customFont" value and change the font programaticaly.
Then you can use this custom view and set its font through xml ;)
Its possible for some limited font becuase you must define an enum for xml and see in java code wich font was selected and then set it in java code set the selected font.
Though you can set system font in xml without all these. And my explanation was for your custom fonts ;)

Related

android - How to change the font of the Switch in xml?

I can change the textview's fonts with using android:fontFamily="#font/test2"
but this is not working on switches, so i created a custom style includes font but still no action. Also I tried with android:textAppearance="#font/test2"
still no action. I can change the font just programmatically. How can I change with using xml attributes?
android:fontFamily atribute in xml is not available for API levels below 16.
For API below 16 use app namespace.

Can you set graphical layout preview-only text on a TextView?

I have a styled TextView whose real text is populated dynamically at runtime. The Graphical Layout view is very useful for getting a feel on how this component works with others in terms of look and feel, etc. There is no sensible default to this text field and I wish it to be blank before being populated. If I don't specify any text in the TextView declaration then the TextView is blank. I can set the text manually using:
<TextView
...
android:text="Preview text"/>
and then switch to the Graphical Layout. However, I must remember to remove this or risk it being shipped in my production version.
Is there a way to specify text which is only seen in the Graphical Layout preview but not applicable at runtime?
EDIT: I'm using Eclipse ADT.
Yes you can with the design tools extension attributes in Android Studio.
See this page https://developer.android.com/studio/write/tool-attributes.html
Basically you define the tools namespace
xmlns:tools="http://schemas.android.com/tools"
Then use it to set your placeholder text.
<EditText
tools:text="John Doe"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This actually works with most (if not all xml attributes).
e.g
tools:visibility="gone"
would set the preview visibility to "gone" but the runtime visibility would be unchanged.
I don't believe there is, the only possible way is when you declare your TextView, you say after, tv.setText(""); this way you will always find it blank at runtime

Android find language and set Fontface in android

Json array will contain list of string data. Each elements of the JsonArray in different language.How to find that language and set fontface in android.
For example ["tamil-font data","english - font - data","Hindi data"] like this.
About finding the language, I would recommend to add an indicator to the JSON.
About font face, make sure you have all font that you need:
2.1 Place the font to assets folder.
2.2 Any View that inherits from TextView can change its font, for example:
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/epimodem.ttf");
tv.setTypeface(face);
2.3 if you need to change the text style linke bold or italic use in the TextView's XML attribute:
android:textStyle="italic" android:typeface="serif"
Tip: If need the same font more than one one time, reuse the Typface variable to avoid overhead.

How to view default android simple_spinner_dropdown_item style

Often i like default android style defined for some widget.
Often i like to just slightly modify this default style, not create my own style from a scratch.
Now, i use android.R.layout.simple_spinner_dropdown_item as dropDownViewResource on my spinner adapter - like this:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I can open this layout xml file and copy its content to modify it. But the most important part of that xml file is attribute style, it looks like this: style="?android:attr/spinnerDropDownItemStyle"
Now, my question is: how do i find/view/copy this style spinnerDropDownItemStyle, so i can for example just change background color.
Thanx for your help
here you can find style spinnerDropDownItemStyle:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
But, if you looking for change background of Spinner, you'll need to search for 9 patch images:
https://developer.android.com/studio/write/draw9patch.html
And here a good example:
http://www.gersic.com/blog.php?id=57
Ty
My suggestion to you is . you can make a seprate custom layout and put it on your spinner setDropDownResource()
adapter.setDropDownViewResource(android.R.layout.CUSTOM_LAYOUT);
Now you can easily make a style for your layout

Custom Font on Android

I want to use a custom font in an Android app but using custom fonts seems to be a pain?!
I can setup a custom font programmatically by adding it to each TextView -> lots of boilerplate code. I can create a new class CustomTextView extends TextView and add setTypeface in the constructor -> no xml context aware help for attributes anymore.
Is there another way to setup a custom font? By XML, by style, by theme?
Thanks in advance!
P.S. Is there a possibility to set an underline by XML?
use this code for change font textview..
TextView newfont;
newfont=(TextView) findViewById(R.id.textView1);
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/DejaVuSerif-Italic.ttf");
newfont.setTypeface(font);
newfont.setText("This is the new font Text");
download DejaVuSerif-Italic.ttf file from web and put in fonts folder in assets folder.
You can include custom fonts under assets/fonts and then using the code from
https://github.com/browep/AndroidCustomFontWidgets/
you can specify the font in your XML, e.g.
<com.github.browep.customfonts.view.FontableTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is in a custom font"
app:font="MyFont-Bold.otf" />
The code supports FontableTextView and FontableButton, but it's quite easy to extend to support other widget types if you require.

Categories

Resources