If I want to set content view then I use:
setContentView(R.layout.test);
But when I set a theme, why do I have to add android to the beginning of R.style.x?
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.test);
The android API define a huge amount of standard resources like theme, drawable, attributes, ...
When you need to refer to one of those resources : you must use android.R
android.R is a reference to the Android R.java file. It is not custom to your app. It contains a reference to every style, resource, anything that you would ever use in an Android app.
On the other hand, R.x (without the android prefix) refers to your local app. It contains specific styles, resources, etc. that have been defined for your app, and is much more specific.
So as to your question of which to use:
Use android.R if you want to reference a standard style. This would be something like android.R.style.holo. It could also be animations, but keep in mind these are not custom, these are the building blocks.
Use R.x when you are declaring something local. This will be something that you have definded, such as R.style.MyTheme or R.layout.MyLayout.
You are not forced to use android.R.style.Theme_Light you can create your theme and use R.style.myTheme.
You use android.R.* when you want to use a layout/image/attribute/etc. already built inside Android without the need to rewrite it again.
You can use android.R.* like R.x everywhere you want (they are the same thing, the difference that R.x is build with your application while android.R is already here). You can remake things, but why do more work when Google already did it for you?
Related
http://androiddrawableexplorer.appspot.com/
I want to be able to use these drawables inside my projects, but my projects are generated at runtime. I only use XML to define new activities, because quite frankly, I hate working with XML from within java. I'm looking for a mathod similar to this. One that doesn't use any XML to 'findByID' the component it needs. Thank you.
Drawable foo = new Drawable(bar);
You can use this :
Drawable d = getResources().getDrawable(id)
So what exactly is it that you are looking for? You want to build activities at runtime without XML? Do you want to just be able to manipulate the android drawables?
Well this is how you use the android drawables:
How to use default Android drawables
For example, when creating list items, I would like to reuse Android's simple_list_item1.xml or simple_list_item2.xml, but I would also like to tweak them.
So, is there any method to inherit the attributes of these views that are declared in xml?
My solution for now is to copy all the attributes in simple_list_item1.xml to a self-defined style, then have the TextView inherit the style. But I hope to get the data directly from android so that if things changes in the future, my views also inherits the changes.
Its not possible to use inheritance in a OOP model. The best way to use this is to copy and paste the source into your own project.
I'm using this library project in my Android application. But I need to make some customization for it.
For example, if I need a EditText instead of the provided TextView, what is the best practice to customize the library for my needs without writing code in the Project Library?
I made this customization by defining in the library's actionbar.xml layout a EditText instead of TextView, but I don't like this approach.
Do you have any guidelines, tutorials that could help me out?
In your special case I would suggest to use original compatibility's-package actionBar.
But to answer your question: you could always extend classes from the Library, which I think is the best practice if the library should kept untouched. Overriding Methods which you want to change their behavior keeps anything clean. You do the exact same every time you extend android-sdk classes, which you cannot change like you want.
In the case of changing layouts I'm not quite shure. I think I can remember that if the lib has an actionbar.xml and you have an actionbar.xml inside your project, too, yours will win. just like an "overriding layouts" feature
I'm developing an app for text reading. I'm trying to increase the options for my users to customize the appearance of the text and the app itself. Creating multiple styles in my resources folder and switching among them at runtime seems simple enough using the view constructors that take a style parameter.
However I'm also considering going one step further and creating a style editor in my application that allows users to have full control over their experience.
I think that doing this using Android styles is basically out of the question, since the style ids are generated at compile time. I'm considering creating custom views that are light wrappers around the views that I need styled, manipulating the AttributeSet in the view constructor to apply my styles.
How should I dynamically create and apply styles to my views?
your approach is quite right as i tried several posts in several places, if you don't do it yourself, it wont be done :-) Android does not support dynamic theme as it seems.
I wanted mine to be downloadable as a plugin, you need, as you say create a wrapper and be able to extract extra parameters from the AtrributeSet OR add an extra HashTable parameter with the attributes you want to override.
I am following an example project "Sky" by Jeff Sharkey using styles in my layout. It is working out quite well, however, I cannot determine how to specify the font type-face using the styles. Without this, I will have to apply the font explicitly to every TextView, EditView, etc., whereas I want to control where and when it is applied throughout my application.
It seems that it is not possible from xml/styles... You can only specify it from java code.
check here if you are interesting.
You can do this by creating your own textview class and use that in your layouts.
Inside your new custom class, set the font you want.
Et voila!