Best android layout for a set of columns - android

On an Android layout, I'd like to have a set of rows, each with two TextViews. The leftmost column of TextViews should be right-aligned, just left of an imaginary centerline down the screen. The rightmost column should be left-aligned.
Examples of this can be seen at http://stuff.greenberg.org/ScopeCalc.htm
What's the best layout to use?

IMO, TableLayout would be a logical choice with appropriate use of colspan/rowspan.

You can also do this using LinearLayout, with the two sub-views of each row each getting 50% of the width.

Using GridLayout you can apply columnspan and rowspan properties to the views inside the grid.

Related

Android LinearLayout multiple columns while preserving addView(view, position) functionality

How do you enable 2 or more columns on a LinearLayout while still preserving the capability to use addView(view, position) to add views at a given position.
Nested layouts aren't an option because in that case, each inner layout will have it's own indexes. I want to be able to use the addView method on the main layout such that the view goes to the appropriate column.
I think you should use tablelayout instead of linearlayout
I'd use 2 separate LinearLayouts, possibly within an outer LinearLayout, and have one LinearLayout be the left column, and the other be the right column. These 2 new LinearLayouts will both have equal android:layout_weight so that they have equal widths in the outer layout. You can then choose whether to add the new view to the left column or the right column by its ID.

Why use android:weightSum?

If weightSum is not specified, Android will just add the weights of the children together. So, is there really a reason to use weightSum? Is there a situation where I shouldn't use it?
Is it more efficient than simply letting android add the weight by itself?
The important word in the reference documentation description is "single": "This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0." In this case, the sum of the weights of the children (in this case, only child) is different to the weightSum.
So you only need to use weightSum when you won't necessarily have the children filling the entire LinearLayout.
For example you can set weightSum = 3 for your layout, and weight = 1 for two children views. In result your views will range 66.6% of all place in layout.
Good luck!

Setting a semi fill_parent layout width in an Android interface

I am trying to create a table row element where the first line contains a title followed by an ImageView that should be all of the way to the right. The TextView should fill the entire width of the phone except for the ImageView at the end.
The solution depends on some details of the circumstances.
If all the rows have the same format
If all the rows have the same column layout, then set android:stretchColumns="0" on the TableLayout. This will make the first column (index 0) stretch to fill any remaining space.
If the header has a different format from the remaining rows
If you have more columns or need a different column layout for the rest of the rows, then you need to do something different. I don't believe any single item can span multiple columns.
If only the positioning is important and you don't really need to span across multiple rows, you can use the android:layout_column attribute on each of the header items. The column number is 0-based, so the first column is 0. The Eclipse layout builder doesn't seem to present this attribute, but it will handle the attribute if you type it into the xml.
If you can't fit your elements inside the same columns the rest of the table uses, then the header doesn't belong in a TableRow. As suggested in another answer, you can use a RelativeLayout instead of the TableRow. Alternately, you could move the header outside of the table.
First, use a RelativeLayout for your row. Use the typical ImageView, give it an id, and use the XML attribute android:layout_alignParentRight="true". Next, use the TextView, use android:layout_toLeftOf="#id/myid" and make sure it's width is "fill_parent".
edit: code tags

How to make a View to fill the space between two views?

I want to layout my views in the following way: [Button] [SomeView] [Button]. I want to set specific sizes for buttons (in mm), and then have the SomeView fill the remaining space between them.
How to achieve this?
Maybe you could set the layout:weight of the [SomeView] to 1 and put all of these views in a linearlayout. Hope it works!

Is there a way in Android XML to make a row of buttons with widths set to widest button?

Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.
The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).
I think you'd have to do this in code.
Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:
Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
Call super.onMeasure()
Iterate child views to find the one with the biggest getMeasuredWidth().
Iterate all other child views calling setLayoutParams() with the widest pixel width.
Call super.onMeasure() again. :)
That should work but I won't stake my reputation on it... happy to help you further if it doesn't.
To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>
As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.
Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.
It'll make them all use the width assigned.
If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.

Categories

Resources