I have a vertical, set height (300px) LinearLayout (LL) with 3 nested LLs. 1 and 3rd are set with android:layout_height="wrap_content" and the middle one with android:layout_height="fill_parent". To my dismay, 3rd LL gets pushed out with 2nd one filling parent layout right to the bottom. How do I achieve desired effect since I want potentially resize the outside container with the middle portion expanding and contracting to accommodate the change
Turned out (Thanks Mark Murphy for the answer) that all I was looking for was to set middle row to
layout_height="0px" and layout_weight="1"
If, after all the wrap_content and fixed-sized items are allocated for
along an axis (horizontal or vertical), there is still room on that axis
left over, LinearLayout then allocates the remaining space to those
widgets with specified weights, in proportion to the weight.
Related
How to actually create a matrix of buttons to cover the entire screen programmatically.
What I tried : I am currently able to create buttons but I want them to occupy the entire screen area, they are just occupying top left part
You can use a TableLayout or LinearLayout too assigning weights in your Buttons.
Let's use a LinearLayout. This can be done both in xml and in your class. Let's create a 2 by 2 matrix of buttons.
Add a LinearLayout with any orientation (let's use horizontal)
Add 2 LinearLayouts inside with opposite orientation of number 1 (in this case we use vertically. Then we set the width=0dp and layout_weight=1 for both to occupy equal spacing)
Now we can add our buttons inside the vertical oriented LinearLayouts and set their height=0dp and layout_weight=1 for them to have equal sizes.
That's it. using the TableLayout is a cheaper way if you have more rows and columns
I have two scroll views in a vertical linear layout.
I want them to be relative to each other so that they fill the entire linear layout and compensate if one cant cover half the screen.
Lets call that scroll views TOP and BOT.
If the screen can display 4 rows and both scroll views have infinite rows, each scroll views should display 2 rows and be able to scroll down to se the rest rows.
If TOP has 1 and BOT infinite rows, BOT should be resized to 3/4 of the linear layout.
If TOP have infinite and Bot has 1 row TOP should still just display 2, i.e. it should never pass the linear layouts vertical center.
Here are some pictures for reference:
my setup with weight set to 0.5/0.5.
result of 0.5/0.5 weight. Notice the gray bar above the BOT title bar. This empty space should be filled by the BOT bar.
if Using fixed size or wrap content the TOP will push the Bot out of view.
How can I have them hugging each other and still set TOP to a maximal height?
Preferable in XML.
Its better to set the weight dynamically. Count the number of items in both views. Set the weight of each view according to the ratios of their number of items. You can refer set weight dynamically for setting weights at run time
I have a TextView that I want to have 1/8 of the whole screen, and dp just works to get half of the height but how do I get the 4 TextView's to each share the bottom half of the screen equally?
Right now they share the height equally, but the ones on the left are wider than the ones on the right.
Inside of your RelativeLayout you need to make a horizontal LinearLayout just for these TextView's. Then to make them all take up an equal amount of space, assign an equal weight to them all with weight="1".
If you want a TextView to take up more or less space than the others, just assign a proportionate weight to it. For example if you have 4 views and want the first one to take up 1/2 of the space and the other 3 to share the rest equally, set the weight of the first view to 3 and the others to 1.
You can't with a relative layout- consider a PercentRelativeLayout
I have a horizontal layout to display 5 icons.
I would like to know if there is a way to display the first one always X dp from margin left and the last one X dp from the margin right and the others with the same space between then.
With LinearLayout the space is always the same even when I change the device , so sometimes my horizontal row became much more bigger than the space that the icons use and with RelativeLayout i just can align 3 items in this way ( alignParentLeft, centerInParent, alignParentRight)
With RelativeLayout I think you can easily put three of your icons at the proper place. For the last two icons, I don't think there's something you can do in your xml. Actually I have met the same problem before.
An approach is (maybe not the best one) to calculate the margins of the two icons in your java code. It's really easy to get the parent view width (in pixels) and you know how much space(a percentage) there should be before/after your views. Just a multiplication and addView with LayoutParam is sufficient.
A LinearLayout with android:width="match_parent", android:layout_marginRight and android:layout_marginLeft within a RelativeLayout should work.
The Linear Layout will be xdp from the left and xdp from the right, with the icons spaced evenly inside.
If you use the Linear Layout, you can use the weight attribute to calculate the correct spacing between the images.
Set each of their weights to 1 so that they take an equal space in the row :
android:layout_weight="1"
I hope that can assist you.
I have 2 textviews per horizontal linearlayout row on two rows. All the views are set as 0dp width and weight 1. They all have the same font and text size etc.
The views on the left side are gravity aligned left and the two on the right are gravity aligned right.
When both textviews text length overflow android always gives precedence to the textview on the right and ellipse the views on the left.
Is there a method that can be used to control which view ellipses when both views on the same row would not fit.
Ideally I want the views on the right to ellipse in favor of those on the left. Or failing that make them ellipse evenly per row.
thanks
i don't think there is a feature of order of the views to manage how they are measured.
you can customize the linearLayout by extending it in order to support this feature , but this is too hardcore for this task .
i would suggest putting the problematic views (those that take too much space and you don't with them to take too much space) into a new layout , and set its width to match_parent .
this way , it should take the rest of the space at the end of the measurements of the other views ,