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.
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 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 need to create around 26 buttons for simple task like display alphabets. i can do this by using layout design.
if i create this button at run time will it give more performance(Considering memory, speed,apk size!)?
Important Requirements:
this layout will be used by 4 different activities.
I need to display 26 buttons at a time to user.
These button may contain background
image.
edit: This layout is like pop up window for other four activities. user can press any alphabets in this layout. As soon as alphabets get selected layout will be closed
Since everything is static residing in your assets, it is fine to have everything in xml files.
Still, one can argue that the 'notion' of parsing the xml layout files of your project introduces an overhead to the process of creating the views.
I would go with a well-designed layout defined in xml.
Yikes. While XML is the best practice answer, 26 of anything screams for some dynamic run-time creation, or at least a combination of the two. You're not going to see much difference in processing time or apk size either way - it will come down to code maintenance down the line.
For instance, consider what will happen when you want to change or add a new attribute, say padding, for each of your letter buttons. Do you want to have to manually go change all those XML elements, or think about a clever regex to properly find/replace?
I'd go with a combination of styles, <include> statement, and run time modification for a comprehensive, maintainable solution. First create a single button styled how you think you want all your buttons to look. Extract your "LetterButton" style out to style.xml and use the android:style="#style/LetterButton" attribute on your button instead. This will allow you to change your style in a single file and have it affect all your letter buttons.
Next, extract the button itself into an <include> file. You can do this by right clicking on the GUI version of the button and choosing "Extract include...". Then arrange your <include>-buttons however you need to, perhaps in a <TableLayout>. Make sure you give each one a unique id, like #+id/letter_button_0 up through _25. The text attribute for all these buttons can be anything, you'll set those dynamically later.
Finally, in your onCreate, define an array of ints of the form {R.id.letter_button_0, ...}, and an array of Strings of the form {"A", ...}, and iterate over those, doing a button = findbyId(int), button.setText(String) to put a letter on each of your buttons.
It may seem like more work this way, but you're doing all the heavy lifting creating a smart UI, so that down the line you can change code in a single place (style or include) and all your buttons will be updated.
Strongly recommend XML layouts for best practice and more understandable code. Also, If you are worried about performance for large view, use relative layouts, these are faster to render than other types of layouts such as LinearLayouts.
Showing XML is best practice:
http://developer.android.com/guide/topics/ui/declaring-layout.html
Also to increase performance keep the Buttons as Activity member variables, then they only need to be loaded once from resources.
Hope that helps,
Marc
You should create it at xml file and make visible and invisible as you need.
You could create a layout with having static assets in it & have dynamic text content & for dynamic backgrounds.You can have use the button properties of gone & visible in it.
I want to use a number of ShapeDrawables in my application, which are all similar, but with different colours etc. Obviously I could just define them all in separate xml files, but is there a more efficient way to have one xml definition, instantiate various objects and change the colour either in code or xml? You could perhaps do this by calling mutate() on one ShapeDrawable defined in xml, but this returns a Drawable, rather than a shape drawable.
Maybe use GradientDrawable instead of ShapeDrawable.
Actually it is possible to do what you asked for,
this post shows you how to do it.
I tried it and it works perfectly. I haven't found though how to refer to a single drawable from the list.
It seems Level lists are meant to be used into a single object to represent different states of the object. Alhtough in this case we are using it as a drawable container to avoid having many small files.
Maybe future anrdroid releases will support a dedicated drawable container.