Android: Linear vs. table layout for a reusable UI component - android

I'm trying to define a reusable layout element I want to include in another layout. But I'm not sure what's the best way to archive the layout I want.
The reusable component should look like this.
It should be separated into 3 areas. Each area will get a separate background image.The both areas on the left should each contain 2 TextViews whereas the right area should contain 3 TextViews. The whole component should fill the width of the screen.
I've already started to build the layout but I'm still not sure whether to do it by nesting multiple LinearLayouts or by using a TableLayout.
Which way should I go? Or would you suggest a totally different approach?

Have a RelativeLayout or LinearLayout as parent. Inside it take a LinearLayout for left and RelativeLayout for right.
On left take a TableLayout .this should give the required layout.

Related

Android Relative layout Usage

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.

Layout realigned by dynamic text content

i'm trying to layout a fairly basic screen. it's just a details view screen after a list item click.
initially i'm looking to acheive a side by side stacked label value type of screen.
for example, where to the left of colon is static text and to the right of colon being dynamic values based on row clicked.
first name: john
last name: doe
last login date: yesterday
additional info: blah
i've started with a relative layout with a bunch of text views in it all positioned accordingly to acheive something like what i was looking for.
The problem arises when the dynamic data being displayed grows and stretches its enclosing text view. Of course the static labels don't grow and everything gets mis aligned...
I'm wondering if there is a different way of tackling this sort of layout...
i was shying away from stacking a bunch of horizontal linear layouts inside a vertical linear layout.
is table layout the way to go? i've read that "they usually aren’t the best tool for doing so, as they are derived from LinearLayout and not the most efficient of layout controls".
Yes there is. Use a listview with a layout predefined in xml. Use a simplelistadapter and pass it the dataset that you want to populate it with.
Edit
Here is a great tutorial:
http://www.vogella.com/articles/AndroidListView/article.html
For a simple form like you want to create I'd suggest using a TableLayout. It is simple to use. As you said the alternative to a TableLayout would be horizontal LinearLayouts in a parent vertical LinearLayout. Using a TableLayout will also automatically align the right side dynamic content for you. Everything on the right side will be treated as a column so if one resizes, they all resize to match.
In your particular scenario I would believe that the TableLayout would work well (although I have also heard similar issues of efficiency/performance). As long as the entire Viewgroup of this Activity isn't complex, I don't think the performance will be too noticeable.
If you are attempting to make the RelativeLayout version work, perhaps you can try this: Have all of your static labels are aligned to the left using android:layout_alignParentLeft and have each aligned to the top of the dynamic TextView they are corresponding to using android:layout_alignTop. This should keep the static TextViews aligned to the left while aligned to the dynamic view relative to it.
Now that those views are aligned, we can horizontally align the dynamic views to the longest static TextView using android:layout_toRightOf. From there, all the remaining dynamic views can also android:layout_alignLeft to this anchor dynamic TextView, or also align to the longest static TextView in the same manner that the anchor was. This solves the horizontal alignment of all the dynamic TextViews.
Finally, we can set that each dynamic TextView falls under the next, since the dynamic TextViews are our determining our vertical location within this RelativeLayout. Each view can use android:layout_alignBelow to chain the fields to align vertically.
I believe this should work for you and I can edit this post later in the day if you would like a sample of code.

Android Layouts, Table, Relative and Linear, i am confused in their differences

I have read many articles regarding layout, but I am still quitely confused. My questions are:
When to use relative layout? Example?
When to use table layout and why we can't use it instead of relative layout?
When to use linear layout?
I just need brief answers.
When use which layout?
I think It depends on your UI, and most important thing that how you create optimized layout.
From definition : -
LinearLayout – designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout – designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
TableLayout – designed to organize child View controls into rows and columns. Individual View controls are added within each row of the table using a TableRow layout View (which is basically a horizontally oriented LinearLayout) for each row of the table.
References :
Creating Efficient Layouts
Common Layout Objects
And most important Hierarchy Viewer
at first there is some confusion about these layouts but as you start playing with these three layouts u will get idea where to use what.. I worked on relative-layout the most.
Consider i want to use a widget always at bottom of screen then with table or linear layout this is not possible always.. without feeling screen other two can not make item at bottom but relative can do.use of any type of layout depends on your screen requirements.
I started out using relativelayout. But recently I've switched to using mostly linearlayout.
The reason is kind of hard to explain, but take this as an example: Say I want a layout that has two images centered in the middle of the screen. Both images should take up 1/4 of the screen width and 1/4 of the screen height. This is impossible to do with relativelayout assuming you want it to work exactly the same on all devices. But you can do this with Linearlayout. By creating vertical and horizontal parents, you can create "boxes". To accomplish this you must learn about weigthsum and weigth. Parent layouts should have the weigthsum attribute and children should have the weight attribute.
Anyway, my point: Relativelayout is easy to use but it's also deceptive. You may think that your layout will look exactly alike on all device, but most likely, they won't look alike. The reason for this is:
With relativelayout you must define size with either dp or px(assuming you don't fill parent or wrap content).
Different devices have different aspect ratios.
I hoped that helped in terms of understanding relative and linearlayout.

Layout usage in Android

I am new to android.
As far as I know, there are different layouts like the liner layout, the table layout, and some others. I would like to know in which case what layout would be suitable.
I am unable to categorize the exact differences between the layouts.
Also, can someone please tell me what the specific meanings of the attributes like fill_parent, wrap_parent are?
What type of layout to use really depends on how the layout you are trying to build looks. If you have a simple layout of items one after another, a LinearLayout usually is the best choice. It's easy to work with, but the drawback is that you can't really customize the layout very much. All your views will just end up in a horizontal or vertical list. A RelativeLayout gives you a better way of adding Views that are right/left/top/bottom aligned compared to its parent view. And last but not least, the TableLayout is really great if you are building a grid of views.
All layouts have specific uses. Read android.developers.com, fill parent means your view will cover whole screen area, wrap-content covers that much area that is occupied by the layout's child views. Match - parent takes area equal to its parent area.But using match parent is not good.

Aligning components at desired positions

Seeking help to design a layout as shown here:
http://docs.google.com/Doc?docid=0AQhgDtGvE2HgZGZ6cmtua185MTd0eGdyZmc&hl=en
The major challenge I face is aligning the components at desired positions. Please refer the three buttons(icons) and the way they are positioned.
Literally, going nuts, thinking how to position those exactly at the desired places.
Any help is much appreciated.
Regards,
Rony
Since you used the Android category, I'm assuming that you're trying to recreate this iPhone layout in Android.
The three buttons would probably be best laid as follows.
Your main layout container would probably be a RelativeLayout, so you can dock things to the top and bottom and lay everything else out in relation to one of its sibling elements. The three button icons (and I'm assuming you're referring to the circular buttons and not the tab bar buttons at the very bottom) would be in a LinearLayout centered within its parent (probably want to use gravity=center_horizontal on the main outer layout) and the individual items would have an equal left and right margin parameters to get the desired spacing (layout_marginLeft, layout_marginRight). You could also make the LinearLayout container of the buttons flush (layout_width=fill_parent) and using android:weight attribute on the outer buttons laying them out towards the center and using a lower weight on the center item. I'd favor the first option, personally.
If you're trying to create relatively complex layouts and any of the above doesn't make sense, go back and read the docs. Layout in Android is very powerful, but you really have to understand the available tools to take advantage of it.

Categories

Resources