I would like to use Roboto font as default font for every TextViews, EditTexts, Buttons, etc in my app.
I've put the ttf file in the fonts folder inside assets folder. Now I would like to edit the app style, in order to use that font.
So, that's what I've done.
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface">ROBOTO-REGULAR.TTF</item>
</style>
But the compiler returns this error.
String types not allowed (at 'android:typeface' with value 'ROBOTO-
REGULAR.TTF').
Is it possible to define via XML a default font for the app? In this case, what is wrong?
Thank you in advance.
You can't do it the way you want. The android:typeface attribute is an enum and has a fixed number of values. It doesn't take a filename.
What you can do is implement a custom TextView (plus custom Button and EditText inheriting from their respective classes) that reads a custom attribute and loads the font file that the attribute points to.
Bear in mind that Roboto is meant to be used from Honeycomb onwards (or was it ICS?). It does look a bit out of place on older devices, where Droid Sans is the system-wide default.
Related
Going to create android app, but as client requirement.
I have to come up with custom style buttons and radio button.
Review the attached image
How can I make the button and input field same as 'bullet point 1'.
How to use custom Arabic font style.
(i.e https://fonts.google.com/specimen/Cairo)
For bullet point 1, I would suggest you to use 9-patch images with stretchable area on top & bottom lines & keeping the circular arc on left/right edges of same radius.
I can try to create same 9-patch image for you, but it's gonna take time :) (give me the color codes you want to use in)
For using Arabic fonts, please go through this Android Developer link Android Custom Fonts.
There are plenty of answers out there on how to use custom fonts, so you can easily find any one.
Here's how you can easily use that Cairo fonts types:
Download the Cairo font zip file, unzip it & put all .ttf or .otf files inside your Project -> app -> src -> main -> res -> font folder
Add below lines into your styles.xml file under res->values folder:
<style name="cairo_semi_bold">
<item name="android:fontFamily">#font/Cairo-SemiBold</item>
</style>
<style name="cairo_light">
<item name="android:fontFamily">#font/Cairo-Light</item>
</style>
Now you can apply these fonts either in XML or in .java files like this:
Typeface boldFont = getResources().getFont(R.font.cairo_semi_bold);
textView1.setTypeface(boldFont);
Typeface lightFont = getResources().getFont(R.font.cairo_light);
textView2.setTypeface(lightFont);
for changing the font you can use Calligraphy library here!
it's so easy to use
inside your xml fontPath="your font"
refer to the link for the complete guide
I want to change the textsize inside a style at run time. Can anyone suggest me how to achieve it. Here is the style "TitleView" in which textsize attribute is defined. I want to change it from 20sp to any other value at run time.
<style name="TitleView">
<item name="android:textSize">20sp</item>
</style>
I want to do it because I've 4 types of textview with different textsize. and In my app user can choose the textsize. So I've changed the text size of other view in relative to the user entered textsize.
Thanks in advance.
You cannot change style attribute at run time.
If you are just trying to change the text size just do:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
this will change your text size and you can do this at any time you want. It is not necessary to change your style attribute. For setting text size at runtime see this
Edit: The poster has altered the original question. This answer is no longer directly applicable.
This cannot be done. The styles along with all the other resource values are all compiled into R.java at compile time.
Instead you could you create two styles and switch the style at runtime using setTextAppearance
In android developer examples, blogs, etc, I have seen multiple prefixes for the values of style attributes. Can someone please describe when to use what? For example, what is the difference between these 3 cases?
parent="#android:style/TextAppearance"
parent="TextAppearance.AppCompat"
parent="#style/TextAppearance.AppCompat"
What is the difference between these 2 cases?
<item name="colorControlNormal">#color/white</item>
<item name="android:colorControlNormal">#color/white</item>
Without any prefix references the style named on the same file.
#Style references your style.xml project file.
#android:style references defined Android API style.
More about Style Resource and Style and Themes.
About colorControlNormal vs android:colorControlNormal is the same explanation. If you use controlColorNormal you are defining the color applied to framework controls in their normal state in you app. If you use android:colorControlNormal you are overwriting the the color default applied to framework controls by the system.
You can think of # as signaling that a named resource is coming up.
#type/name identifies a resource of type type (string, color, layout, style etc) with name name defined in the app is coming up.
#+id/name identifies an id resource called name that will be created if it doesn't already exist (whereas #id simply refers to an id that already exists).
#android:type/name means that the named resource is part of the Android platform is coming up (it's not defined in the app -- it's provided by the device).
For style parents, the #style is optional. You can refer to styles directly by name. It's redundant because you can't derive a style from anything other than another style anyway.
I was looking for how to highlight a selected item in a list when displaying a contextual action bar for the selection, and the solution I found was to set the android:background attribute of my row layout xml to "?android:attr/activatedBackgroundIndicator".
How does setting this work though?
what is the mechanism involved?
what do the syntax elements like "?", "attr", "activatedBackgroundIndicator" mean?
where is the meaning of "activatedBackgroundIndicator" defined?
If you are in a forensic mood here is how to dig and find out what is going on.
android:background="?android:attr/activatedBackgroundIndicator"?
Intuitively this means set the background to some drawable.
But lets decompose this further to see how we get to our mysterious drawable.
To be precise it means "set the background attribute to what the attribute "activatedBackgroundIndicator" refers to in the current theme.
If you understand "refers to in the current theme" part, you have basically understood everything that is going on behind the covers.
Basically, activatedBackgroundIndicator is not an actual drawable but a reference to a drawable. So where is "activateBackgroundIndictor" attribute actually defined?
Its defined in your sdk directory in a file name attrs.xml. For example:
path_to_android_sdk/platforms/android-17/data/res/values/attrs.xml
If you open that file, you will the declaration as follows:
<attr name="activatedBackgroundIndicator" format="reference" />
attrs.xml is where you declare all the attributes that you are later going to use in your view xml. Note we are declaring the attribute and its type and not actually assigning a value here.
The actual value is assigned in themes.xml. This file is located at:
path_to_android_sdk/platforms/android-17/data/res/values/themes.xml
If you open that file, you will see the multiple definitions depending on what theme you are using. For example, here are the definitions for themes name Theme, Theme.Light, Theme.Holo, Theme.Holo.Light respectively:
<item name="activatedBackgroundIndicator">#android:drawable/activated_background</item>
<item name="activatedBackgroundIndicator">#android:drawable/activated_background_light</item>
<item name="activatedBackgroundIndicator">#android:drawable/activated_background_holo_dark</item>
<item name="activatedBackgroundIndicator">#android:drawable/activated_background_holo_light</item>
Now we have our mysterious drawables. If you pick the first one, it is defined in the drawable folder at:
path_to_android_sdk/platforms/android-17/data/res/drawable/activated_background.xml
If you open that file you will see the definition of the drawable which is important to understanding what is going on.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#android:drawable/list_selector_background_selected" />
<item android:drawable="#color/transparent" />
</selector>
Here we are defining a drawable with two states - default state is just transparent background and if the state is "state_activated" then our drawable is "list_selector_background_selected".
see this link for background information on on drawables and states.
"list_selector_background_selected" is a 9 patch png file that is located in the drawable-hdpi folder.
Now you can see why we defined activatedBackgroundIndicator as a reference rather than linking directly to the drawable file - it allows you to pick the right drawable depending on your theme.
I wondered this as well at one point. A large amount of the Android resources seem to be like a black-box and can't see them directly. I may be missing them someplace, but I can't find them in the SDK source code. Here is what I do know.
android:background will take a drawable.
The syntax is in the style
Must be a reference to another resource, in the form "#[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name"
In this case the ? signifies to look at a theme in package android and it is of type attr where the name is activatedBackgroundIndicator.
You should be able to access this in the code-behind with android.R.attr.activatedBackgroundIndicator as well.
A list of Android attr properties can be found at R.attr
activatedBackgroundIndicator is a defined drawable in Android 3.0+ as
Drawable used as a background for activated items.
It's basically just a standard item defined in the OS. I can't seem to find in in the Android source, but here is a link to the documentation. activatedBackgroundIndicator
This is a form of attaching a value from a theme. The value is technically not known during resource compilation because the theme values may not be known at that point. Instead the value is resolved at runtime based on the actual theme taken from (most commonly) ContextThemeWrapper.
This provides a way of reusing resource values. I'm not talking performance-wise here, but rather organization and maintenance-wise. The attribute acts as it were a variable with the promise that it will hold an actual value at runtime.
This approach also allows for greater customization - instead of hardcoding the value of e.g. window background drawable it gets the actual drawable from a theme, supplying a chosen attribute as the key. This lets you override the value for that attribute. You simply need to:
Create your own theme (which is just a fancy name for a "style" resource), most commonly deriving from one of default themes.
Supply your own value for the attribute in question.
The platform will automatically use your value provided that you have specified your theme for an activity or application. You do this like described in the question. The general syntax of theme-attribute references is described here: Referencing style attributes. You will also find an example and description of the whole mechanism there.
Edit
One thing that should be noted is the actual attribute names and their existence in various platform versions. It's fairly common for new attributes to be introduced in next platform versions - for example some were added in version 3.0 for the purpose of ActionBar styling.
You should treat attribute names as part of the API - in other words, they are part of the contract you are allowed to use. This is very similar to classes and their signatures - you use LocationManager class for the purpose of obtaining last device location because you know from some source (tutorials, reference, official guides, etc.) what's the purpose of this class. Similarly, the attribute names and their purpose are (sometimes well, sometimes miserably) defined in the Android Platform documentation.
Update: There is a more detailed version available from the API Guide so I'd like to quote it.
A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional. For instance:`
Original Answer:
numan salati already offered an perfect answer but it have not addressed the "?" syntax. Here's a quote from API Guide Accessing Resources
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional. For instance:
?[<package_name>:][<resource_type>/]<resource_name>
I have an android application that I'm working on with a custom theme applied to it in the android manifest on the main activity itself. This activity creates a listview, which has the style applied to it.
If I create a custom layout for that listview, including a separate xml file for the rows themselves, and I apply styling directly to those layouts, does this style overwrite the overall style for the application? I'm trying to get a grasp on the hierarchy of events as far as how styling and themeing works.
The way I'm GUESSING it works in my example is: apply style for row, referenced by row layout xml > apply style for overall listview, referenced by listview layout xml > apply style from custom theme, referenced by style xml referenced by android manifest
Am I right? Or am I approaching this incorrectly. (just for confusions sake, by ">" I mean has a greater importance than)
If you've specified the same attributes in multiple places, the list below determines which attributes are ultimately applied. The list is ordered from highest precedence to lowest:
Applying character- or paragraph-level styling via text spans to TextView-derived classes
Applying attributes programmatically
Applying individual attributes directly to a View
Applying a style to a View
Default styling
Applying a theme to a collection of Views, an activity, or your entire app
Applying certain View-specific styling, such as setting a TextAppearance on a TextView
Hope I am understand your question right here...
The styles you define in styles.xml will always overwrite the styles coming from the theme currently used by android.
But this only works for the attributes you overwrite.
If you leave an attribute untouched, android will provide the style for it, and sometimes this comes bite you in the butt :)
This system is best described like this:
A textview requires an attribute example
<item name="android:textColor">#00FF00</item>
Android will first look in the original layout.
If not found, it will look into your custom styles.
If not found, it will look into android styles.
Hope this helps.
The standard themes have lines like which define the ListView style:
<item name="listViewStyle">#android:style/Widget.ListView</item>
In your own theme you can do a
<item name="listViewStyle">#style/MyOwnListView</item>
Something that is not defined in the ListView style (own or default) will be what is defined in the theme if you have defined it there.