How to add elements on second row when screen is not wide enough to fit all - android-jetpack-compose

[A B C D]
I have a view like this where D should always be aligned to the right.
When the screen is too small to fit all the items I want to have a layout like this:
[ D]
[A B C ]
What would be the best way to achieve this?
I wanted to use https://github.com/google/accompanist/tree/main/flowlayout but this doesn't allow me to keep D aligned to the right of the screen.

Related

Respect labels spacing

Working with a UITableViewController. You can see my prototype cell in the screenshot; It's made up by 2 labels (1 on the left containing quantity X item name, and 1 on the right with the price).
When the left label is too long (or when testing on a 3.5" device), there's no space left between the 2 labels and they enter each other's space. How can I make sure that when they get really close to each other, I show something like "1 X item n..." instead of "1 X item name" so that there's always some space between the 2?
I am using auto-layout by the way (with a spacing constraint between them but it doesn't seem to respect it).
Hope I am making sense!
Ah, just got it. Had to decrease content hugging.

Centering multiple elements in UIView

I have four UIButtons in a parent view, next to each other. The leftmost and the rightmost one will always be smashed into the sides of the parent view, but I'm not sure how I should approach positioning the middle two buttons with constraints.
What I want it to look like:
______________________
[b1] [b2] [b3] [b4]
______________________
With regular constraints on x and y I'm able to place b1 and b2 on the right place, but b2 and b3 requires some magic.
By using constraints on them, telling them to stick to each side, it would look like this in landscape(or on bigger screens):
______________________________
[b1] [b2] [b3] [b4]
______________________________
Without equal spacing. What I need is something like the 'flexible space' which is used for UIBarButton's, but in a regular view. As I can't find anything like that, I'm guessing there's some constraint-magic I need to understand.
I COULD make the buttons big enough so that they are all constrained together (so b2 and b3 are touching, and they all have equal width), but I don't want that. I guess a solution could be to create four ´container´-views in those sizes (big enough to touch each other) and simply place each button in the center of each UIView. But is that really necessary? Feels like such a waste..
You could add UIViews where the three flexible spaces need to be:
–––––––––––––––––––––––––
[b1] * [b2] * [b3] * [b4]
–––––––––––––––––––––––––
Then for each of the spacer views (denoted by *), add constraints such that the trailing and leading spaces are 0 and give them a constant height. Finally, add a another constraint to make all of their widths equal. That way, the spacing between each button will always be equal regardless of the width of the screen. Then you can just make the spacer views transparent so they don't appear in your layout.

Solid solution to flexibly resize UIView based on subviews

Imagine you have four or so views, all width 100, different heights. You have a wrapper view W which holds them all.
A |
B | W
C |
D |
the heights of the small views can change. At that time you want them all to move, float, appropriately, and resize W.
Now, I was just about to write a few lines of code to do this.
So .. (1) you'd have W find all the subviews and list them in order from top to bottom. Then (2) each time there is a change, you'd (3) reposition each of ABCD. the position of each one is the sum of the heights of the items above it, and (4) resize W to the sum of all heights.
Now that's all fine but -- idiots reinvent the wheel!
Am I missing something obvious in iOS? is there already a package everyone uses to do this all the time? Or something built in? What's the situation?
(Note that of course frustratingly, for our Android friends this is built in! And of course any web-html system does this automatically.)
What's the right engineering solution for iOS views here? For the record this is iOS7+ only, no old-fashioned stuffs need be covered, if it makes a difference. Cheers
(1) you'd have W find all the subviews and list them in order from top
to bottom. Then (2) each time there is a change, you'd (3) reposition
each of ABCD. the position of each one is the sum of the heights of
the items above it, and (4) resize W to the sum of all heights.
You can use constraints in Interface Builder for that whole process, no code required. Do this:
set the width of subview A to 100
constrain B, C, and D to match A's width
add vertical spacing constraints between A and B, B and C, and C and D to maintain their relative position
add a vertical spacing constraint between W (the superview, shown in gray) and A
add a vertical spacing constraint between W and D
add leading and trailing space constraints between W and view A
You'll end up with something that looks like this:
The constraints editor in Xcode isn't completely intuitive, but it is easy to use once you understand what you can and can't do with constraints in IB and when you need to use code to set up the constraints.

iOS SDK Triangle ASA or AAS (angle-side-angle or angle-angle-side) formula

I am looking to spin a UILabel 45 degrees (to the right) on it's anchor point (the center of the UILabel) and put it on the right of the table cell so it looks like a diagonal ribbon across the table cell, from the bottom-right of the cell up and toward the left until it hits the top of the cell.
To do this (in a diagonal) I do the following:
Get the height of the table cell. This gives me (in an triangle with sides a,b,c) "c". So, "c" is one leg of the triangle. Because I want it to be a 45 degree triangle that also gives me leg "a" of the triangle.
Now the easy part: I know all three angles of this triangle. "C" (angle across from side c) is 45 degrees, "B" (angle across from side "b") is 90 (cos it is the right top corner of the cell's content view...and we know those are right-angles. Angle "A" (angle across from side "a") is also 45.
Side "b" is the side i am having fits with... What formula do i use to get length of side "b" such that it is wide enough to go (when diagonal) across the height of the cell's contentview from the bottom right until it hits the top of the cell? I know for a 44pt high cell, the length of this UILabel when diagonal should be around 60-ish pts...but the formula is escaping me.
When using a online calculator Triangle Calculator and using the drop-down for angle-angle-side, I feed in the following numbers: A: 45,B: 45, C: 44, and this gives: 62.2254 for the side length...which is fine...but my table cell is not always 44pts high so i need a formula i can put in objective-c. I have read up on the Law of Sines, but my High-School Trig years are SO behind me.
I hope all this is clear. If not, please let me know. Thanks in advance.
If I understand you correctly you will always have triangle with one right angle and two other 45 degrees. If short side (both of them will have same length) of such triangle is X, then long side is square root from 2*X*X

LaTeX stretching tabular/array to fill page width

I recently have had the need to make a math array or a tabular expand to fill the page width.
The effect I am trying to achieve is that for one line on the page, half is aligned at the left, and the other at the right.
For instance, just now, the following has been giving be problems...
$$
\begin{array}{l r}
\phi_{CJ}^* = \Sum_{SV} \psi_{CSJV} = P(S) P(S|V,C,S,J) & \text{(no summation on S or V which are in evidence.)}
\end{array}
$$
The desired effect is that the first cell is aligned on the left margin, and the second is aligned on the right margin. Instead, the array is centred, with no space between the cells.
Many thanks
I wonder if instead of l and r you might consider trying p{width} for your alignment. This works okay for me:
\begin{tabular}{p{0.45\textwidth} p{0.5\textwidth}}
$\phi$$_{\mathrm{CJ}}$^* = \Sum$_{\mathrm{SV}}$ $\psi$$_{\mathrm{CSJV}}$ = P(S) P(S \& V,C,S,J) & (no summation on S or V which are in evidence.) \\
\end{tabular}
p{width} allows you to set the column width and try to fill your page, whereas I think l and r have some other (perhaps goofy) method of deciding how wide to make things.
In your equation you have P(S) P(S & V,C,S,J) and I needed to change that & to a \& to stop it from thinking it was the beginning of another column...
There's a ditty on \eqmakebox on the LaTeX/Tables Wikibook site that sounds like it might help as well, though it's a bit esoteric compared to something common like \tabular... (LINK).

Resources