Causing a 3 row GridView to fill the available vertical space? - android

I have a two column, three row GridView that I would like to have fill the available screen space provided by the surrounding layouts. By setting the layout_height and layout_width to "fill_parent" and the background color to red I can see that the GridView is in fact filling the space -- but the content is not.
When there is extra space, I'd like the GridView to have more verticalSpacing or horizontalSpacing to "fill up" the screen. I don't see an available setting to do that though.
There has to be an elegant way to do this, but I've yet to find it =\

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.

How to set row height of GridView fit to Screen in Android

I want to stretch the row of a GridView to match the height of the screen in Android.
How do I do this?
You are in control over your row heights, by virtue of what you put in
them. Since your cells appear to have more than one widget, they are
presumably wrapped in a LinearLayout or something. Set your
LinearLayouts to be some specific height, and the rows will all be
that height.
Personally, I think you should be resizing your images if you are
going to have text above and below each image on a per-cell basis.
With the Reference commonsware link

Format buttons to fill parent based on dynamically added text

I'm hoping I can get some formatting help. I have a TableLayout. Inside the TableRow, I have 2 RelativeLayouts. Each RelativeLayout has 2 buttons in it that overlap (this is by design). The button text is dynamically added at runtime. This makes the buttons taller if the text is long enough. However, the text in one column (RelativeLayout) is different that the text in other column (RelativeLayout). So, the TableRow is expanded by the taller of the buttons. What I'd like is to have the smaller buttons fill in to the same size as the larger one.
I'm real new to Android development, but I've tried every combination of match_parent/fill_parent/wrap_content that I can think of, but none of them make the buttons stay the same size (that is, the size of the larger one). Some even expand the button to the full height of the screen (which I don't understand either...I'd think that the parent would be the TableRow and that would limit it)...
I appreciate any advice.
Use
button.setEllipsize(TruncateAt.END);
attribute while creating the buttons

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.

Layout buttons so each divides up the space equally

Im using a LinearLayout to put two buttons horizontally side-by-side, but I want to each button to size itself to use 50% of the horizontal space. I thought layout_weight of "1" for each button would do the trick, but maybe my layout_width needs to be changed?
The layout_weight attribute controls how much of the left over space each of your buttons is given. If your buttons take up different amounts of space to start with, then each of them will first be given the space they ask for, and then any remaining space will be divided up between them, meaning that you won't have an exact 50/50 split.
You can get around this behaviour by first setting layout_width="0px" (keeping your layout_weights as they are), and relying on 50% being enough space to display each button.
I would set the layout_width of each button to 0px, then use the layout_weight=1 trick you mentioned.

Categories

Resources