I am kind of new to Android Studio and I have been having lots of issues with the designer. I format everything in the drag and drop designer and then as soon as I press play and run the project, all the buttons and edit texts just bunches up in the top left corner. I've searched around online and it looks like I have to learn how to use linear layouts or relative layouts and XML. Can someone explain this to me or maybe point me in the direction of a tutorial?
Seems like the root element for the .XML file is a ConstraintLayout. You should read about LinearLayouts and then RelativeLayout to get started. To explain in simple terms, a LL uses a linear heirarchy to stack views vertically or horizontally amongst each other. A RL is used to position views relative to each other, ie, the position of one view depends on position of another view. ConstraintLayout is the latest and most powerful one, which is used to reduce deep nesting of views by defining certain constraints amongst views in the layout.
Also, for future question posts, please include code snippets which you think are causing the problem. It helps a lot. Cheers!
Related
My boss refuses to let me use RelativeLayout. Everything is done using LinearLayout and minimal use of RelativeLayout, such that the layout that I could run with two levels of nesting with RelativeLayout, I now have to do with four using LinearLayout. Any comments on this? What links can share?
In my opinion it would be better to combine these two elements to get the perfect layout.
It depends on the level of nesting indeed. Android suggests to use a minimum Layouts in Layouts as possible, and if you can achieve with one layout, what you want to do with 3, why not?
The app will run faster for it, though with 2-3 nestings you wont see it.
I for example, create a bunch of included layouts, which i reuse in other layouts.
And the level of nesting could get very deep with that
With the LinearLayout we get the clear separation of view or we can say that each layout is separate. With the Linearlayout each UI element us independent of the other. You get the maximum flexibility. It enables to give the vertical and horizontal orientation.
Linearlayout you can control look more and make it almost similar even
on the device change. And also, using LinearLayout is simpler though
starting phase can be quite confusing
From the Docs
A Layout that arranges its children in a single column or a single
row. The direction of the row can be set by calling setOrientation().
You can also specify gravity, which specifies the alignment of all the
child elements by calling setGravity() or specify that specific
children grow to fill up any remaining space in the layout by setting
the weight member of LinearLayout.LayoutParams.
LinearLayout is a view group that aligns all children in a single
direction, vertically or horizontally. You can specify the layout
direction with the android:orientation attribute.
Again it depends upon your need..I also agree with #Lena Bru answer.
What Is A Relative Layout?
After linear layouts, which display controls in a single row or column, relative layouts are one of the more common types of layouts used by Android user interface designers. Much like other layouts, relative layouts can be defined within XML layout resources or programmatically in the application's Java code. The relative layout works much as its name implies: it organizes controls relative to one another, or to the parent control itself.
Get to love the RelativeLayout. It will make your life much easier, when designing for multiple resolutions/densities. If you have an old SDK, update your eclipse plugin. It has graphical snap-lines for RelativeLayouts similar to designing a form in Visual Studio, so you can see what is anchored where. It's really quite good.
google says:
Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint.
I'm building a layout within Eclipse for Android using the RelativeLayout. I lay down a simple stack of buttons one on top of each other. For some reason the button snaps to the first button position even though I clearly place it down on the last stack of buttons. I try everything to move it to the right position eventually I just let the layout builder stick it to the bottom of the screen edge.
When I try this view inside the app, the last button is magically snapped to some random spot usually near the top. Obviously incorrect. As a hunch I tried it again but I left it alone this time, when I laid the last button down again ( which within Eclipse Layout builder its snapped to the first elements position for some crazy reason). Surprisingly within the app itself it appears in the proper location. So this must be a bug with the Layout builder itself.
It looks horrible in Layout builder (almost unusable) and the position of UI elements totally wrong, is this some known bug with Layout Builder + Android or do I need some update or new SDK or something?
There are many reported complaints about the ADK Editor for Eclipse, but one alternative is to try DroidDraw
http://www.droiddraw.org/
The preference of some Android developers is to directly modify the XML. This often provides more flexibility as you can see the exact rules that are defined for your UI.
If you are looking at just stacking Buttons on top of each other, you may want to consider a LinearLayout.
http://developer.android.com/reference/android/widget/LinearLayout.html
This is a layout that arranges its children in a single column or a single row.
Relative layout every "view" is positioned relative to something else. It would help you to understand better if you look at the xml. You will see what each item is aligned on. It is difficult to get this layout right as you see especially when you design in eclipse and run on an avd or real device. Consider placing a linear layout inside the relative layout and your buttons in that. This way your button group is together in a row or column and next to this you could have text or something.hard to explain but group like things together and nest layout inside other layouts.frame layout only one item is viewable at a time,like pages in a book or slide images. I hope I explained this good. I have no idea why you would do buttons that way.absolute layout is x,y positions where you put it is where it stays. Like that's gonna work. Try a different screen density and things are bunched up in the corner.like I said the key is nesting the layouts
I have some buttons, textboxes etc. in my android application, but when i drag them with my mouse in the xml file, their place doesn't change, or changes but they are not placed where i exactly wanted. How can i adjust their positions in the screen?
Thanks
Unfortunatly there is no such thing as absolute positionning in android ( RIP AbsoluteLayout deprecated since years.)
instead you have to position views according to their parents and according to other views in the same parent.
first you have to define wich parent you need ( if you want some viens in a single line go for a LinearLayout. a more custom layout: use a RelativeLayout ...)
then you can drag and drop views inside, but they will always snap a position relative to their parent and/or relative to the other views.
you can of course play with margins.
A list of layout type with some advanced techniques can be found on this page
Hope that helps.
You RelativeLayout as a group layout for your layout so positioning can somewhat easy using mouse.
Best is to arrange them from the xml code. Just Learn about using the Relative layout, LinearLayout and TableLayout
Learn how the XML works. For a LinearLayout, the items come in the order listed. For a RelativeLayout, the items are related by the values of their layout_XXX properties. Then you don't have to worry about the WYSIWYG tool not working.
FYI, the tool bundled with eclipse is extremely buggy. Don't count on whats on there being what's on your phone for anything non-trivial.
Like the others wrote it is easier to edit layout using xml editor. You can read more here http://developer.android.com/guide/topics/ui/declaring-layout.html.
I have seen the difference i.e in the latest android SDK versions the default layout given in the main.xml file is Relative Layout.
What is the reason behind that..Is using relative layout recommended to be used more than linear layout?If yes...Please explain..
Thanks In Advance.
To provide us the more flexibility and freedom in programming of UI.
LinearLayout arranges elements side by side either horizontally or vertically(rows vs columns).
RelativeLayout is a layout manager that helps you arrange your UI elements based on some rule. You can specify thisngs like: align this to parents left edge, place this to the left/right of this elements etc.
As mobile apps are going with much more interactive and complex UIs, RelativeLayout helps in building those UI plus reducing the layout hierarchy considerably with so many tags provided.
This improves the performance of the app too.
I find myself using RelativeLayout more than any other types of layouts in Android XML files. Given that my apps use primarily simple and basic interfaces, it is quite easy to bang out a new RelativeLayout.
Currently I create a RelativeLayout and I assign each component of the layout properties such as Layout Below, or Layout Left, or Layout Right to anchor that component to the correct area of the user's interface.
While it's easy to define new layouts, it's hell trying to move a single component within a layout. Given that I've used components in my layout to space other components, once a component is moved, the layout around that component falls apart. Moving a component means I have to re-do the relationship for all components below that one. Surely I am doing this wrong? There must be a better way?
TL;DR I use RelativeLayouts and the components I put in the UI to space one-another relatively (of course). If I move a component say to the top, the items below it break as the relative relationship breaks. How can I more easily move components around and experiment with my UI without renaming tens of spacial relationships each time?!
This is one of the cases where the tooling is preferable over hand-coding the layout XML. Editing the XML using the Graphical Layout tab helps ease lot of this pain of refactoring RelativeLayouts. It is not the perfect solution, but it does make things a lot easier.
I should point out that these changes have come about in ADT 14 and newer. If you are using any older ADT, I suggest you upgrade. In case you do not know how to use the Graphical Editor to get the most out of your RelativeLayouts, check out this video.
Having said that, the inflexibility of RelativeLayout is the main reason I prefer using nested LinearLayouts over the former - in spite of Google's suggestions to use the former for performance reasons.
As a compromise, I use RelativeLayout only for items in a ListView, and nested LinearLayouts for everything else.
+1 for the question, since I've been looking for the solution myself. However, I think/feel the only way to do this is by declaring attributes like android:layout_alignParentBottom , android:layout_alignParentLeft etc. for the items that can afford to use those attributes, as they align with the parent, which according to my understanding is the layout itself. Otherwise, I don't see any other way with Relative Layouts. As Rafael T says, another way of binding those items to the layout is by using nested Linear Layouts. But then again, even that is a bit tedious. Finally it depends on the programmer who chooses the layout. Personally, I'd choose/prefer Relative Layouts.