Just start developing with android and think instead of reading a book a webinar could be better because a webinar could also teach me short ways and how an android developer thinks when writing the code but now got a problem
<CheckedTextView
android:id="#android:id/text1"
android:checkMark="?android:attr/listChoiseIndicatorMultiple"
</>
I dont understand the above code up to now see lots of different id definitions some of them was for resources and start with #resource/name, and some of those id definitions was like #+id/name just for creating a new id for the component but this time it is using android:id/text1 and I dont understand why it is using it in that manner
Besides, the checkMark thing make me confuse more what are all those ?android:attr/listChoiseIndicatorMultiple means?
Could you please explain me and show me some resource where can I find all those magic attributes so I can cope next time by myself and hope someday can answer other newbie questions
Thanks a lot in advance, and all comment will be appreciated.
Well, reading the docs has always been helpful to me:
Android Developer Site
XML Layout specific docs
#android:id/text1 is just a format used when the id has been previously defined. When you put a + in there that means the framework should create the resource id if it doesn't already exist.
It's normal to use #+id/thisid when defining a new view in a layout, and then use #id/thisid to reference the aforementioned view from another part of the layout (say, in a RelativeLayout where you need to tell one widget to be below another).
A question mark before the ID indicates that you want to access a style attribute that's defined in a style theme, rather than hard-coding the attribute.
#android:id/text1 basically this is used when you create any android component like button, layout, textviews etc.
but when you need any external component which is general for different platform like any color, image etc then you can declare it as #resource/name.
actually there is nothing different just keep one thing in mind that in #android:id/text1, id will simply work as an class name will contains other objects like textview, imageview or any other.
now if you declare #resource/name then in that also instead of id class name will be resource. actually when you will use it in java then these(#android:id/text1) will be converted into object hierarchy.
Related
Can i use the same id names in in some other layout in a different xml file???
coz i have done dat that and maybe because of that m getting a nullpointer exception and my activity for that xml does not start...
Absolutely. You can use the same ID's for the same Widget type in any number of layouts that you desire. Just not the same ID in the same Layout XML. Without getting into the debate whether this is recommended or not of course.
What confuses several developers and I confess, when I started, it did confuse me too. When casting a Widget in the Activity, for example, I was often stumped me why I had just one matching ID when I had the same in a few different layouts.
The only thing you need to take care of is, for example, consider this scenarior:
I have two XML's named, say layout_1.xml and layout_2.xml and 2 corresponding Activities, named say, Act1 and Act2. Now, both have the same Widget, say the same TextView with the same ID in both XML's. (Although I am using this example, this is not hypothetical. This is actually how it is in an application of mine). This is quite contradictory to what NullPointer says in his comment. I think he meant you can't use the same ID in the same XML.
<TextView
android:id="#+id/txtFromName"
style="#style/UserName"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:ellipsize="marquee"
android:gravity="top|left|center"
</TextView>
Now, when I am casting this in my code, see what shows up in the suggestions when I start typing the findViewById(R.id.txtFrom...)
Why the NPE is perhaps when you use the same ID's in multiple XML files is probably due to some confusion on what the ID is. Keep the corresponding XML open and check the ID when casting and you should be good to go. If it still persists, I would suggest updating your post with your XML code, the Java code and the LogCat crash report.
I keep track of that by literally copying the ID and using the same name as an instance of the Widget when casting it. But, that's just my way I suppose.
In some android classes you can set a custom layout. As far as my understanding goes, these custom layouts need certain android:ids to work.
For example, for the ListActivity a ListView with #android:id/list has to be provided and this is specified in the documentation.
What about other views? For example, I was checking the API Demos and came across
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="#string/title_child_preference"
android:summary="#string/summary_child_preference" />
There's a layout specified for the preference. That layout seems to be preference_child.xml, in which there's #+android:id/title and #+android:id/summary, which I assume the view will use to provide the title and the summary, but is this documented anywhere?
How do I know what resource IDs I have to use so that everything automagically works?
AFAIK, there's no trick to find out but to know the documentation or open the android's id.xml and look for references.
P.S.
Your code demonstrates how to use a specific attribute out of an entire style (the use of "?"), it has nothing to do with ID's
For business reasons, I have to capture the ID's of Views in my application. Since I don't want the ids to change over multiple deployments, I have created ids.xml and public.xml as described here How to add id to public.xml?
I have about 200 ids on the app, and I need to capture only about 150 of those. My question is, should I refer to all 200 ids in my ids.xml and public.xml OR just refer to the ones I need (the 150) would be good enough?
Thank you.
These IDs are dynamic, and therefor you cannot capture them reliably (they most likely will change with each compile).
Maybe you can create custom attributes to use instead (like here: http://kevindion.com/2011/01/custom-xml-attributes-for-android-widgets/)
Bottom line, it is a bad practice to capture the ids.
I also think there is a 'Tag' attribute you can use in layout files, so you may want to try that as well.
I am wondering is there a way to organize my widget's android:id . My app has a couple of Activies and couple of layout. It is hard to keep track of all the names of buttons and textviews. My IDE would spring up a list of all the R.id.xxx from previous layouts . Is there a way to sort them like with directory or periods, ie android:id="#+id/abc.efg" or android:id="#+id/abc/efg" . Sort of like sub structuring them or nesting them.
A simple way I keep track is by changing the "id" prefix to something else
ex.
A layout for ActivityOne might have layout IDs as
android:id="#+activity1/textview"
And "TestActivity" could be
android:id="#+test/textview"
I am trying to always use some convention on the id naming. For example use type-of-component prefixes: *btn_somethig* for all Buttons, *et_something* for all EditText and so on... When you're looking for a particular ID, just fill-in first the type of the component.
AFAIK no. I always go by a naming convention based on what I'm looking for. Usually it's, type_of_id_type_of_object_name. So a layout could be layout_relative_layout_main_panel. A sub-view like a TextView would be view_text_view_text1 or something. The detail is app-specific though.
I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.
I found some tutorials using SAXparser:
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html
and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?
I found a similar but unsatisfactorily answered question here:
How to create a layout file programmatically
which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)
Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.
Thanks!
I had similar requirements and tried the same approach - it does not work.
Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html
Update:
Since OP needs to load XML layouts created at runtime:
Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.
DexClassLoader can be then used to load classes inside apk.
well, android makes the hard work for you, but no all the the work....
first that all you have to forget about parsing xml layouts... instead you can make skeletons layout, that manages his inner childs position, size, etc... and later inflate that 'skeleton' xml with LayoutInflater and obtain a View instance...
When you have that View instance then you can do what you want with it, applying the users preferences like backgrouds, foregrounds colors, position, sizes, etc...
maybe i dont understand your question but you can get any view inflated from a xml resource at compile-time and later apply other style or set another propertys
It seems it is impossible to load the layout & change the skin dynamically according to the doc :
Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
http://developer.android.com/reference/android/view/LayoutInflater.html