Which is faster? Layout from xml or from code [duplicate] - android

This question already has an answer here:
Should I inflate a layout or programmatically create it?
(1 answer)
Closed 9 years ago.
I've simple dialog with only one ImageView, and im using it frequently in my app, and Im wondering what is faster to load and showup, create layout in XML or make the same thing in code.
EDIT
I ask about theoretical knowledge in this point, and I cant find any useful info about that on Android Dev sites.

Theoretically in my opinion, coding the layout is faster than inflating it from xml, because inflaters need to map the layout by it self, and link id's to each other, but in code you will give the viewgroup (lets say) the reference of the view it self, there is no need to lookup it in the xml.
don't forget the step of converting the xml into java elements (views)

No difference, but if you have only one imageview make this layout in code

For performance, there is no noticeable difference.
But for maintainability you want to separate functionality and layout, hence it is recommended to use the layout XMLs.

No one is necessarily faster.
It will be the easiest to define the layouts in xml though, and the xml option is there as a way not to have to define buttons and ui components in java.
Also, if you define views in java, you will end up adding them to your blank xml layout's viewgroup
( ei. adding your button to the layout)

Related

Android XML Layout or Java Coded

I am starting to delve into Android Development and there is a lot of material online. The question is... What are the pro's and con's against the drag and drop XML design method vs coding the view manually? The only reason I ask on here is because online the views are mixed and they don't really back up what they're defending.
If I use the drag and drop method will I have issues further onto my development adventures? That is the thing that worries me the most... I don't want to learn the drag and drop method and then editting the XML to cater for my needs and then be handicapped by it.
For the beginner(s), I highly recommend not to use Drag and drop. We need to understand XML, to be comfortable with android widget. Understanding XML will come handy in future when creating custom styles and themes.
Here are few pointers before you dive in android XML layout
Try sticking with match_parent and wrap_content while defining android:layout_height or android:layout_width if possible
Make sure you have good understanding of RelativeLayout, LinearLayout and FrameLayout and how its child views are arranged.
Forget about ConstraintLayout, AppbarLayout and similar advance layout at current.
Try exploring TextView, EditText, Button, ImageView and ProressBar as far as possible.(This are most common widgets/views)
Try avoiding any tutorial related to ListView, its deprecated. Try using RecyclerView instead, it is one of the important widget that would be used in regular basis.

How to set up a layout using code and not XML

I'm learning to develop android apps and in the process I realized that there two ways to get a job done. Using xml or normal code. Suppose I want to change the position of a button, I'll be doing it in xml using align left/align centre etc., This will be done in the XML file. If I want to achieve the same through code, where should I place the code ? Inside which class ?
There are two aspects to your question that I understand.
1. Creating a whole layout file dynamically (without XML).
2. Creating a layout through XML and changing the components positions and properties dynamically through your activity file.
Now, it's upto the developer what he wishes to choose.
To help you further, please view this video link posted by the Android team.
It's all about layouts and includes how to layout apps using Java, not XML. However, you are warned that the android team wants you to use XML.
The code will be placed in the same class as the class where you reference your xml code. Do a read up in your android docs for insight.

Android GUI XML vs Code

I have a short question according to creating GUIs in android. What way is better xml or coding?
I always read that xml is way better but imagine you have a scrollview.
Inside the scrollview is a relativelayout. Inside that there shall be several rows with an ImageView next to a TextView next to a RadioButton. The number of rows can vary.
Is it really better to make lets say 50 Views in the xml or a loop in code where these views are created?
Each has its pros and cons. Just to name a few:
XML
pros -> fast GUI development, keep code clean
cons -> static
Dynamic (code)
pros -> able to react to runtime conditions
cons -> more code, which means poorer maintainability and potentially buggier
If you need to add components dynamically, only way is go with code (or) mixed approach (define layout in XML and add components in code). If your components are static XML may be best.
Dynamic content is, of course, added dynamically. So your example would require some java code. You should only add the dynamic part programmatically though, so you'd still use an xml document for the static parts (it's very unusual for a layout to be completely dynamic).
If you have a fixed number of views then yes, I'd write 50 of them in xml rather than with a loop. I guess you're wondering about code duplication and, as far as I know, you'll get some when using xml.
(One way to minimize code duplication within xmls' is with the usage of styles and themes)
I agree with the above. XML is a better approach to this even when you require dynamic updates you can still use XML bits and pieces to render the content. your code will be based on XML elements but XML files will be independent. hence if you break a funcitonality in the code you know that its your business logic thats broken not the UI part, which will make it easier to develop and find problems easily.
Why you do not use a ListView instead of a ScrollView.
It will be simplier to implement and performances must be better with it.
Create a XML file with a ListView and in your activity implements your own adapter to instanciate the rows.
You can find a lot of tutorials on internet talking about that, I'm sure you will find what you need !
Good luck.

Android images and text via Java programming, not xml layout

please is possible to make some activity with images, text etc. via some .java file (Java programming) without xml? - xml use only for strings of text, for multilanguage....
Thanks for help.
Yeah it is possible and we call it Dynamic Layouts. But i think you should stick to XML because it is easier to use as compared to programmed layouts. I know XML layout could be an headache sometime but once you'll be habitual to these XML layouts stuff then you won't find it hard any more. And if there is any other reason behind why you asked this question then yes it is possible as Android SDK provides classes for each and every layout element with the same name as used in XML file.
For ex. you can create a TextView like this in an activity:-
TextView textView = new TextView(this);
now you can call any function on it which would work similar to TextView's properties used in XML file for ex. for android:text property you can use :-
textView.setText("I am a TextView");
And if you want to know more about these Dynamic layouts then you should spend some time on developer.android.com.
I hope my answer will be useful for you.
Anything that can be done using the xml files can also be done using code. You can create your views manually, add TextView and ImageView to a LinearLayout (or another ViewGroup), etc.
The android developer website contains all the documentation to do this.

XML parser for dynamic layout (dynamically loaded skins)

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

Categories

Resources