Sharing some XML between layouts - android

I want to make something that is a vertical LinearLayout on the phone version of an app, a horizontal one for tablets.
The view in question is located inside a large XML file that specifies the entire view.
The naive way to do this is to just copy and paste the file from res/layout to res/layout-sw600dp. I did this and it works. However this means I know have to maintain two files if I want to make other UX changes. This is inefficient.
What is the easiest way to abstract away the LinearLayout in question?
I have looked at Custom Components but it goes farther than I would like to go. I do not want to have to make code changes for this, it should be self-contained in XML changes.

You can reuse layouts using < include > and < merge> tags.
Doing that, you can have different containers in sw600dp and layout folders including the same common layout.
Official doc here

Related

How do I pass information from an XML layout to its Activity class?

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.

Dynamic or xml layout? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am little confused about the best way how should I design my application. I have used XML to define the position of objects. But now when I'm developing application with more complex design I'm starting to think dynamic define of object can be more practical and suits all screen resolutions. Basically I would like to combine imageViews, ImageSwitcher, TextSwitcher etc... Now I used XML code and it has more than 270 lines and also it is very chaotic, difficult to suit to all screen resolutions and every small change will crash my app. I would like to use percentage for set positions of objects, set scale of objects etc. Should I go on with XML or is it better if I start to use programmatically way to set positions of objects. I was looking for some books or tutorial about dynamic designing app but I always found only easy tutorials can be also done in XML. Has someone good hint for some book or tutorial about dynamic designing? Thanks a lot.
Now I used XML code and it has more than 270 lines
If you have that many lines in one xml file then I think you need to reconsider your design. Use Fragments, ViewPager, ListView, etc.. depending on your needs. But I don't know how you can have that many lines in one layout file.
difficult to suit to all screen resolutions
I'm not sure how it is any easier doing the layouts programmatically. It really isn't that hard to support multiple screens. Use dp, layout_weight with LinearLayout when necessary, and other various tricks. See this part of the docs about supporting different screens. For the most part, you just create different layout folders (3-6 maybe depending on what your need)
Should I go on with XML or is it better if I start to use programmatically way to set positions of objects
It is typically easier to do your layouts through the xml. There are certain properties that need to be set in xml (can't think of any off-hand but they exist). I would only suggest doing it programattically when absolutely necessary. There have been times when I spent a bunch of time creating a layout programmatically then realized there was a much easier way to do it in xml.
The main reason, that I know of, to create a layout/view programmatically is if you don't know the number of/ what kind of Views will be used. Say, for example, if a user can press a Button to add another field to contacts. Even then you can do it in xml if you know the amount of Views available.
Make sure to go through the Design Sections of the Android Docs and you can Google design patterns, principles, etc... and find a lot of information.
Speaking from experience - the best way to create layouts is using XML. This should not break your app every time. This sounds like you are not following best practices. One of the major drives behind using XML rather than programmatic view creation includes supporting different screen sizes and resolutions, and simplifying the use of Fragments.
That aside, there was a time about a year ago that I asked myself the same question. The result was an open source library for creating layouts using Java or XML (interchangeably), and supports customizing sizes based on the current display, using percentages, etc. It is called AbLE (Annotation-based Layout Engine), and is available for download here: https://github.com/phil-brown/AbLE .
I do not currently use this library (I use XML instead), and do not currently maintain it.

Should I hide optional views or add them at runtime?

Suppose I have a ViewGroup that can contain several optional children views, say a VideoView, an ImageView and a few optional buttons.
Are significant resources wasted if I include all possible children views in the layout file but set all of them to visibility gone by default, but change visibility as appropriate at runtime?
Is it better just to add the views at runtime as needed? Is there another approach that would be more sensible? Fragments?
I prefer to create them all and hide them. I've noticed that a most of the built-in Android layouts I've looked at do the same. Personally, I think it cuts down on NullPointerExceptions and the checks needed to avoid them.
The resources saved by not creating a handful of views in the layout file should normally not make a large difference for the runtime resource consumption of your application, except for cases where they contain huge images or other very heavy ressources.
On the other hand, having those views in the layout file (and hiding them)
makes them much more readable than creating them in Java code
leads to them being checked by Android Lint.
That's why I always suggest to have them in the layout.

which one is better? -Creating buttons at Run time or design time in android

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.

How to use combination of Layouts to get widgets at desired position

we are confused with designing interface file for android,we have made user interface with Absolute layout and used "dip" instead of "px" (for different screen size issue) ,but it looks like,Absolute layout has been depreciated,and developer.android, recommends not to use Absolute layout
So other option we have is to use
1.Linear layout
2.Frame layout
3.Table Layout
But we are unable to bring Combination of above layouts so,that we can make button/widgets to proper position we need
How do we make widgets to desired position of screen with above said layouts and making them compatible with different screen sizes we have in android devices
would like to get a generic answer about same,
Tools used are: DrawDroid
The best layout to use if you were considering an AbsoluteLayout is a RelativeLayout.
Check out the Android Common Layout Objects page and its explanation of how to use a RelativeLayout.
Without more information on what you are trying to do, expanding this answer anymore is useless.

Categories

Resources