Now, am trying to be a good Android developer and put all my textsize and other size specifications, and all the styles (like, say, button color, etc) in XML files. Am trying to read up on these. In a Stackoverflow thread, I read about dimens.xml. So I created a dimens.xml under each values- directory (like values-ldpi, values-mdpi, blah blah) and put one dimens.xml in each. It works fine. Now, I want to put some style information in the XML, like, a button color. Should I put the attribute in the same file i.e. dimens.xml? I read in one thread that dimens.xml is only for dimensions, and not for style related info. In another thread, I read that the name of the XML doesn't matter. Now, what is a good practice? Keep style.xml and dimens.xml separate ? Or put everything in one xml?
And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?
Keep style.xml and dimens.xml separate ?
As soon as style.xml already exists for reasons like these (button colors, text color, themes, etc.) then I believe that you should keep them separate.
And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?
I would do the (2). (Although I can't blame anyone who would do the (1).). As far as I know there's no best practice guidance on that. I do it the (2) way basically because I see dimen.xml as a place to host pixel-related values. But this is my preference.
Related
Hy,
I am using the next background colour #android:color/transparent for a ImageButton in the layout.xml. Could or should I externalize it in the colors.xml resources file?
Thanks
You could but it depends on your needs.
If you use that color in many different parts of your application it's best to place it in an external xml file. Sometimes you need to change the style of the whole application and it's easier to change the definition of that color instead of changing the color in many different places. If you're only using it in one single place then it is OK to leave it out of the external file.
EDIT:
<color name="my_background_color">#android:color/transparent</color>
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
Currently, am setting the background of a TextView using code like this:
textView.setBackgroundResource(R.drawable.rect_with_border_grey);
I then came to know about dimens.xml usage and how you can set through that file. Is it possible to set the background through ? i.e. I want to do the above code line through XML. Any help please?
I wouldn't do this using layout xml file as #Opiatefuchs pointed out (in the comment below). B'cos, this textview's background will change depending on the user setting in the App dynamically.
Make a style like
<style name="MyTextStyle">
Do everything you want to do with your TextView here.
</style>
and then assign that style to your textview in xml like this
style="#style/MyTextStyle"
It will work.
dimen.xml is only use for giving dimension only. for setting background you need to do it with either layout xml file or from java.
Its up-to the requirement of setting background.
I finally found that you cannot set the background of a TextView via element (i.e. XML) in Android. You need to use layout XML. But, in my case, I cannot use layout since the background will change dynamically based on user choice in the App.
create dimens.xml file like this:
<resources>
<dimen name="paddingTop">10dp</dimen>
<dimen name="paddingRight">20dp</dimen>
...
</resources>
then use it in your layout xml files like this:
android:layout_marginTop="#dimen/paddingTop"
android:layout_marginTop="#dimen/paddingRight"
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.
Let's say I just added two standard widgets -- CheckBox and CheckedTextView. The first one has nice, clear padding despite that fact I didn't set any, and there is no padding set in .xml file. The other one comes without any padding.
Now, I could get the value (fixed) of the padding from CheckBox by trial&error. But my question is how to set it in kind of dynamic fashion -- i.e. if in Android 7.0 padding for CheckBox will be "20sp" and I set "10sp" (because it is now 10sp -- I am making this up) then my two widgets would be with different paddings.
And I would like to have a consistent padding. So how to set something like "?android/default_padding" for padding?
Clarification: I am interested in using the system default padding, not hardcoding the same value made up by me over all widgets.
I looked it up for you, in API Level 10 (and also on every other Android platform) Android uses 9-Patch images with prefdefined paddings (there is no padding declared in the Selector), e.g. I mesured the checkbox and it as this pasddings: left, right: 6dp; top, bottom: 12dp. And the default button has a padding of 10dp; so there is no default padding as far as I can tell. But 10dp is good in most cases. Also, it just really depends on the screensize of your device. You will have to declare your own prefered padding like Daniel suggested. E.g: In your Values file 10dp and in values-large maybe 15.
Edit:
Here is the default checkbox for mdpi on Android 2.3.3:
You could create in your resources an xml file called dimens.xml, and the add something like this:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<dimen name="default_padding">10dp</dimen>
</resources>
Then you call from your layout something like this:
android:padding="#dimen/default_padding"
I think this is a consistent way of working :)
Good Luck!!!