How to span and stretch columns in Android - android

I'm relatively new to Android and what I want to achieve is a table/grid type layout with multiple rows. Column 2 onwards should always be aligned horizontally or for rows with less columns for them to stretch to the width of the container. i.e. in the example below the textbox on line 1 stretches across to fill column 2 and 3 (e.g. on my android phone the width of the screen)
-------------------------------------
|label | textbox............ |
|longer label | textbox.. | button |
|-----------------------------------|
Kind of thing. I tried with a table, which works, but I cannot span columns. All I could find was android:layout_span on some website but that doesn't seem to be available (well, to Eclipse anyway).
Maybe table isn't the right layout, but that's all I could find to allow me to align columns. I'd prefer to have it all in the layout XML rather than coding it.

Did you try having a few horizontal linear layout within one vertical linear layout ? It might make your XML file a little bit longer, but it may also solve the issue.
Rule of thumb: Don't trust Eclipse's interpretation of the interface. It's not always right.

Related

Prevent views being pushed off screen

I am trying to have 3 LinearLayouts ordered horizontally (basically forming three columns) within another LinearLayout where the width of the middle layout can vary depending on it's content.
All columns should be visible at all times filling the viewport from left. The left and irght column will be assigned a max width. So only the size of middle layout varies. If the total width of all columns exceeds the viewport size the middle column must not overlap or push out the other columns. But instead it should use the remaining space.
I tried using layout weights but that would put the right column always on the right side and the middle column would fill up all the space even though it's content would not require that.
When I try to use a RelativeLayout as a container I either end up with all three columns overlapping each other or the first column disappears.
I thought the below code (only schematic for now, as I don't have access to the code atm) should work, but as written above the first LinearLayout does not show up. The last LinearLayout seems to be in place as desired.
<RelativeLayout>
<LinearLayout
android:layout_alignParentStart>
</LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd>
</LinearLayout>
</RelativeLayout>
Does anyone know how I can fix this? Please let me know if you need more detailed code examples etc. I will try to provide them as soon as possible.
I found a few other questions concerning this or similar topics but the solutions always used either layout weights or something like the code snippet above. So far I had no luck with those approaches, maybe because those questions where either for slightly different use cases or a few years old so that the api has changed in the mean time.
Any pointers are greatly appreciated :-)
Yes. You want to defined the center columns with a layout_width="0dp" and a layout_weight="1". The left and right columns will be layout_width="wrap_content".
A LinearLayout should contain the 3 inner "column" LinearLayouts.
I finally found a solution that works.
Using the layout_weight as describe by Jeffrey Blattman alone does only work when the views get large enough to fill the screen.
But as long as the views only fill a part you get gaps between them as the middle view fills up the remaining space. This is something I want to avoid in this case.
For some other reason I had to put my layout into a fragment. Now when I set the dimensions of the fragment to wrap_content the behavior is exactly as I want it. So the views do not get blown up while they are to small but are laid out as if there was no layout_weight defined. But still when growing larger the edge views stay within the screen.

Dynamically aligned Variable Sized cells in a Layout

What I have to do is draw variable size cells from left to right. Since the data is from server, its length can't be determined (thus the need for variable sized cells.). I have tried the gridviews, tablelayouts but they align them in an order. What I want is something like this:
a sample http://img18.imageshack.us/img18/4136/samplejy.png
the most important thing is they are kind of aligned in a column (though one column can have further multiple columns) but there is no row as such..the cells are in some kind of array which I have to align from left to right and not from top to bottom.. If anyone has tried anything like this it will be very helpful.. thanks

Two columns of autowrapped text

I need to lay out several (for example, two) labels (textviews) as columns. The texts can be longer altogether than one screen line and may not contain line breaks, so they need to be formatted (wrapped) to fit the screen width.
I can arrange labels as columns if they are shorter altogether than a screen line. I can wrap a text to fit the screen if there’s only one column. But if the texts are longer and there are more than one column, I cannot achieve what I want, because the first column always fills the whole screen width.
I tried to do this via xml-markup. I tried a lot of markups using LinearLayout, TableLayout and RelativeLayout. All didn’t work. Maybe, I didn’t use them properly.
I can imagine the ways using code to manually calculate views’ widths, but haven’t tried. Any solution is welcome, but using markup only are preffered.

Relative stacking with Linear Layout in Android?

I have 6 images I want to display as 2 rows with 3 images in each. I'm using nested LinearLayouts to achieve this, and it works well except for one thing:
The height of the largest image dictates the size of the linear layout, meaning there is empty space a lot of the time. In other words, my problem is as follows:
I keep getting the layout shown on the left, and I want the layout shown on the right.
I am aware that you can just use GridView, but that will still prevent the exact layout shown on the right, so I'm at a loss really. Many thanks.
Instead of 2 rows of three columns, you need 3 columns of 2 rows. LinearLayouts would be fine, just to be sure set the Gravity of the individual cells to Gravity.TOP.
You could equally achieve the whole grid using RelativeLayout instead of Linear. Each of your bottom row would just need android:layout_below and android:layout_alignLeft set to be the ImageView above it.

How can I vary the height of some keys within a row?

I'm creating a custom keyboard layout. The SDK allows changing the width of keys in a row (as in ThickButtons), but ideally I'd like to be able to vary both the height and width of keys within a row (and still have the keys occupy all the available space.)
Another way of looking at this is that I want to allow some keys to be in more than one contiguous row. Any ideas would help. Thank you.
Will all key "widths" and "heights" be a multiple of the smallest key width/height? If so, you can nest horizontal and vertical LinearLayouts as necessary to describe the keyboard, using layout_weight to make your double-wide or double-high keys.
For example, if I look at the keypad to my workstation, it is essentially a 5x4 grid of keys, with a double-high "+" and "enter" key in the right-most column and a double-wide 0 in the bottom-left position (fairly standard configuration, refer to your own in this description). I can make vertical linear layouts for each of the columns "on top of" the 0 {numlock,7,4,1} and {/,8,5,2}. These can next be put into a horizontal linear layout to group all 8 keys. This assembly can be combined with the "0" button in another vertical layout, with weight "4" to the 8 key assembly and "1" to the 0 key. Let's call this full assembly of 9 keys assembly A. Now you can make vertical linear layouts for {*,9,6,3} and {-,+,enter}. In the second of these, give weightings of 2 to each of the + and enter key. Finally use a horizontal linear layout to group assembly A with each of the other 2 vertical layouts.

Categories

Resources