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
Related
My Android app has two layouts, one for <= normal, and one for >= large. I can re-use my corresponding activity 99.9% in both cases, but I need it to know different bits of information for the two layouts. I could of course write a giant if expression, but I'd rather let Android work its magic, since it's already doing it by loading the two different layouts.
So, can I embed pieces of information in the two XML files and retrieve them in my activity class? Or am I completely off the map and the right approach is completely different?
Sure you can, just in the values directory define values for each size and retrieve them dynamically in your program.
/res/values-xxx
-> a.xml
/res/values-yyy
-> a.xml
...
here is an example:
<resources>
<integer name="maximum">100</integer>
...
</resources>
in your program just put:
int max = getContext().getResources().getInteger(R.integer.maximum);
for each size android will magically do the job and give you the correct value!
If you're willing to go the custom View route, then yes, you can. What you have to do is create custom attributes and apply them to your custom views which are parsed when they are created. Here is a thread that goes in to a great bit of detail about it.
The Views themselves don't have to be special. You can say, have a view called a PropertyView view which extends FrameLayout and has a method called getProperty(). You then put the property in the XML like so:
<com.example.ProperyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:property="My Custom Property"/>
Then you would parse it using the methods described in that link.
EDIT:
Alternatively, there are other elements in the XML landscape that can be put in to buckets similar to how Layouts are. Any folder in the /res folder can have the same buckets that the Layouts can. That includes anything in the values, drawables, raw, or xml folders. You can reference these in your Layouts and the Android system will pick which ones you want. More example for how dimens work.
If you are using values to differentiate between the two layouts, then you can have different values that overload depending on screen size. See this answer.
You could do the same sort of thing with the layouts directory to create different layouts, but then use common subsections using the < include > tag to make the different views based on common sections.
Or a combination of the two. If you the want to change the behaivoir of the Activity/Fragment, you could key that on the presence of missing or present screen widgets.
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.
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.