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.
Related
I want the same application to be delivered 2 different set of layouts. Ie the functionality is same but the graphics will be different for two different versions of the app. So i want to keep the same code and based of some variables want to decide which layout to be set for each activity. SO for each activity i will define two different layout.
This is my requirement. What is the best way to implement this. I can have an if else in each activity and define which layout to be set. Is that the right and best way. Please give your options on this
Take a look at this answer. It's about accessing a resource file from identifier, ie file name. You can do this with any type of resource (I think).
How to use getResource.getIdentifier() to get Layout?
Basically, you can do an if-else statement and assign the id of the layout you wish to use to a variable then load the layout using the identifier.
Actually there are many ways for ex you can change your layout based upon the orientation i.e landscape or portrait or you can change your layouts using languages for ex- you can create various folders for different languages.
Please explain your requirement briefly and if possible post some code also.
You can follow below links also.
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screendensities.html
Language Specific layout for android
http://www.c-sharpcorner.com/UploadFile/0e8478/supporting-different-languages-layouts-in-an-android-appli/
For ex. I have 2 Activities.
In first: setContentView(R.layout.activity_first);
In second setContentView(R.layout.activity_second);
activity_first.xml contain View with id android:id="#+id/my_view", and activity_second.xml contain another View. To that View I can set the same id (android:id="#+id/my_view") and all works great.
But the way to set equal id's in different xml files correct? May be I miss some google post about that situation?
There is no harm in setting the same ids to different views as long as they are not in the same view. From the developers site,
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).
It is recommended that you use different ids for different layouts. On the long run, when you will have a lot of layouts and a lot of ids it will get very complicated to differentiate them.
There is no problem with same id.It will work properly. But for a good programming this habit is bad.
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.
Is there any way to programmatically select an alternative resource file to use in the app? I have a selection of buttons in my app, and want to use a different set whenever my app is in a certain mode.
Is there any way to achieve this other than manually setting the image resource on every image in code?
You can create a layout filled with exactly the views you want and inflate that at any time. Though maybe I'm not understanding your question... do you wish for R.drawable.myImage to point to 2 different things? The answer to that is no, that's not possible, but it seems like it would be pretty easy to get around this need by creating two (or more) "pointer" arrays that can point to whatever resources you want, and set those as the src for your images.
Declare a two-dimensional array to store the resource constant.
int[][] sets { {R.id.a, R.id.b, R.id.c}, {R.id.d, R.id.e, R.id.f}};
You can choose the set of views by changing the first index of the array set.
I have two Activities that both list items in a table. For the time being both currently use the same layout and then inflate it at runtime.
However, to cleanly and strictly separate the two and also allow me to later maybe have slightly different layouts for each, I would like to have two different layout names but simply alias (is that a verb?) one layout to point to the other.
Can that be done? I tried to define an alias id in the strings.xml file but that only yields errors.
Michael
There is this more straight forward layout aliasing technique as well. You can refer to: https://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters
What if you have one of them simply contain nothing but a single <include> element so that it just includes the other one?
http://developer.android.com/resources/articles/layout-tricks-merge.html