So I've been looking around for awhile now and can't seem to find an answer to my question
I have tried a few things, but nothing seems to work so I'm wondering if what I'm trying to do is even possible.
Basically I have a RelativeLayout at the base of my XML Layout, and within that RelativeLayout I have some nested LinearLayouts, what I want to do is align a widget in the RelativeLayout to a TextView nested within a LinearLayout, so something like below (unnecessary XML markup has been removed so you can see what I'm trying to do easier)
<RelativeLayout>
<LinearLayout>
<TextView
android:id="#+id/TextView1"/>
<TextView
android:id="#+id/TextView2"/>
</LinearLayout>
<TextView
android:layout_alignLeft="#id/TextView2" />
</RelativeLayout>
At this point I've simply given up attempting to do this and removed the linear layouts from my XML and gone strictly with just the RelativeLayout.
However I'd just like to know if what I originally attempted is possible, and if I was just doing it wrong, or because of the way the widget is nested within another layout that it just doesn't work.
Thanks in advance
I'm not sure if this is missing the point (lack of context, but that doesn't matter if I manage to help), but it sounds like you should be able to replace TextView2 with a horizontal LinearLayout containing both TextView2 and the external TextView. But, yes, sticking totally with a RelativeLayout will do a similar thing with more control (I just like nested LinearLayouts, and personally, I'd probably replace the entire RelativeLayout with them, depending on what it's for).
Elements in a RelativeLayout will only align with other elements in the same layout at the same level (siblings).
However, you may be able to achieve something like this with a little work. You could put an ID on the LinearLayout. You could then align the outer TextView with the LinearLayout and use padding and margin attributes (on both TextView2 and the outer TextView) to make it truly line up at display time.
Related
just a simple question, should we always use Constraint Layout for everything? why another layout still used today?
I want to make a simple login layout like this, should I use Linear Layout or Constraint Layout?
usually, I make every layout with Constraint Layout but my friend told me to use Linear Layout for the login, is it wrong if I use Constraint Layout? thanks before!
ConstraintLayout helps you to avoid using nested layouts and it causes better performance. it's not wrong to use it for everything but if you know that you are not going to have nested layouts, for example, three Textviews respectively, you can use LinearLayout. ConstraintLayout needs more XML code than LinearLayout for simple structures, and using LinearLayout is better for these situations.
I would say ConstraintLayout is more versatile and has better performance compared to RelativeLayout, But LinearLayout has its own significance in android you can specify weights of view using LinearLayout and for complex UIs I would say going with ConstarintLayout is a better option because of its performance and in ConstarintLayout you can specify the view positions, heights, widths, margins and paddings based on screen sizes rather than hard coding the values to fixed dp
https://android.jlelse.eu/constraint-layout-performance-870e5f238100
The above link gives more overview of LinearLayout vs ConsrainLayout performances.
No it it's depends on your need, if you want to make complex layout and want to make them responsive then you can.
As far as I understand Layouts in Android I just wanted to ask whether the following statement is true or not ?
Is it true that every RelativeLayout in Android can be programmed with a LinearLayout ?
I personally believe that every RelativeLayout can be also done with a LinearLayout. So why should someone use RelativeLayout rather than a Linear ?
Thank you
Is it true that every RelativeLayout in Android can be programmed with a LinearLayout ?
No.
First, RelativeLayout supports Z-axis ordering (i.e., widgets overlapping other widgets). LinearLayout does not.
Second, only a subset of RelativeLayout structures could be replaced by a single LinearLayout. It is conceivable that you could implement all non-overlapping RelativeLayout structures using many LinearLayout and Space widgets. The result may be substantially more complex, more memory-intensive, and possibly more CPU-intensive.
According with android reference guide:
LinearLayout: A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.
RelativeLayout: Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
For more information: http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
Even if that statement was true, RelativeLayout is simply a better tool for certain use cases - so why don't use it? You might be able to recreate a layout using a LinearLayout, but the result will most likely be inefficient in terms of number of views and rendering times. Additionally, sometimes using RelativeLayout leads to a more simple and intuitive layout description, which means easier maintainability.
Suggest me on this
I have to use some header and body part in android screen design, Can i use plain Linear layout for screen design or can i use relative layout or else both layout combined together.For the header bar im using a gradient image and application runs both in vertical and horizontal orientation.
As of now im using two main linear layouts for the first one im using a height of 40 dp and for the second i just used 0dip is this a correct way of approach or i have change anything.
Don't mix concept of RelativeLayout and LinearLayout. RelativeLayout is preferred because it reduces extra lines as compared to LinearLayout. In RelativeLayout views are placed relative to each other i.e. left, right, top and bottom unlike LinearLayout where you can't place view in respect of some other view. Both have its own advantages. As Weight concept is not supported by RelativeLayout but LinearLayout.
Depending on the complexity of layout both are chosen. One thing to avoid is un-necessary nesting of layouts which reduces performance. I would recommend read concepts of RelativeLayout, LinearLayout and weight first then you will be able to judge which layout to use on your own. Till then use RelativeLayout as it requires minimum number of lines.
You can use Linear-Linear, Linear-Relative or Relative-Relative. Anything you want.
Your question is hard to understand. From what i get, i think your approach is fine. You should let the Screen design (second layout) use "match_parent". It will take up remaining part. For your header layout using "40dp" is fine. I made app with Header, and i used this approach.
If in Header, you are adding images as well as TextView, it is advisable to use RelativeLayout. In the rest part, use however you need it.
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.
Which Android layout would be best suited for this example. Inside each box would be a picture. I am mainly wondering what would be the best parent layout for the entire screen.
I was probably going to use TableLayout for each of the rows of boxes. Then have each column in the TableLayout be a FrameLayout. Then have each FrameLayout contain the picture.
But i am not sure what parent Layout i should use to contain the entire screen.
I would like this to be able to fit in Landscape on the majority of the phones on the market. Maybe it resizes everything to fit those screens in landscape.
I would also like there to be spacing between those TableLayouts.
Any assistance is greatly appreciated.
Thanks.
If I were building that, and the number of images/boxes in each set was static, I would probably use RelativeLayout as the parent and LinearLayout for each of the "rows." Then just use ImageView directly as children of the LinearLayout. RelativeLayout allows you to position things relative to each other or the parent.
Either RelativeLayout or GridView whichever fulfills the exact requirement.