Aligning three views in Android layout - android

I have three views that are intended to look as follows:
+-----++-------------------------+
| 1 || |
+-----+| 3 |
+-----+| |
| 2 || |
+-----++-------------------------+
So far, so good. However...
Sometimes (3) is very small, and I want it centred in the vertical space used for (1) and (2).
+-----+
| 1 |+-------------------------+
+-----+| 3 |
+-----+| |
| 2 |+-------------------------+
+-----+
Other times, (3) is large, and I want (1) to align with the top (3), and for (2) to align with the bottom of (3):
+-----++-------------------------+
| 1 || |
+-----+| |
| 3 |
| |
+-----+| |
| 2 || |
+-----++-------------------------+
I have tried:
an outer Relative layout: (1) and (2) overlap in the "small-3" case
a linear layout containing (1) and (2) (with and without weights): the alignment at top/bottom does not work in the "big-3".
an outer linear layout (with various height settings): I can not get case (2) and (3) to work with the same settings.
To give a little context, (1) and (2) are buttons and (3) is a text block of varying size.
At this point I assume I am missing some very basic setting (or widget) that will make this work as intended.
Note: I have not included source code because there have, literally, been over a dozen different configurations tried and none worked.

Just asking the question helped...pretty sure the answer is to use ConstraintLayout.

Use ConstraintLayout and use the design tab instead of writing the code yourself when using the ConstraintLayout, its pretty easy to achieve what you want. Dont use RelativeLayout or LinearLayout for this.

Related

Android TextView intricate alignment

community.
The desired effect is to have TextView that:
has left-aligned text
is glued to the right to its right-aligned sibling control
adjusts to the available horizontal space e.g. wraps its text if needed
1/ More than the optimal TextView + ImageView width:
|---Empty-space---|---TextView---|--Image--|
| |1. Aa | |
| |2. Bbb cccc | [---] |
| |3. Ddd eeee ff| |
|-----------------|--------------|---------|
| | | |
. . . .
. . . .
. . . .
2/ Less than optimal TextView + ImageView width:
the TextView wraps its text
no empty space to the left of the TextView due to low available horizontal space
||-TextView-|--Image--|
||1. Aa | |
||2. Bbb | |
||cccc | [---] |
||3. Ddd | |
||eeee ff | |
||----------|---------|
|| | |
.. . .
.. . .
.. . .
Asking the question after trying numerous things with linear and constraint layouts.
Satisfying conditions #1 and #2 from above is easily achieved by setting the width of the TextView to wrap_content, its textAlignment="textStart" (by default) and the gravity (not layout_gravity) for the whole LinearLayout to end. The right alignment for both controls - TextView and ImageView, when using a ConstraintLayout, is achieved by gluing the ImageView to the right and then setting the constraintHorizontal_bias="1.0" for the TextView.
The problem comes when the last condition (#3) should be satisfied as well - make the TextView's text wrappable. This is achievable by setting the TextView's width="0dp" and layout_weight="1" (in case of LinearLayout) or just setting width="0dp" for the ConstraintLayout scenario.
Any suggestions are appreciated and welcome.

Is it possible to create TouchDelegates for multiple children?

I have a LinearLayout that contains multiple ImageButtons and TextViews. Something like this:
+---------+-----------+-------------------+-------+ <- LinearLayout
| i | i | text here | i |
+---------+-----------+-------------------+-------+
These items are hard to tap. I want to increase their tappable area. It seems as if TouchDelegate is the proper way to do this.
However, view.getParent().setTouchDelegate(new TouchDelegate(r, child)) is a 1:1 mapping, not a 1:Many mapping. So how would one solve this problem when a View has multiple children that each need to be tappable beyond their bounds?

Android line wrapping trim extra space

I have an Android TextView which I'm trying to display some multi-line text. I want the first line to be centered, and all of the other lines to be centered as well, but left-aligned with the first line. When wrapping the text, the TextView goes as wide as the view will allow, even though the actual text stopped a while ago. I would like to trim this extra padding space.
Here's a rudimentary ASCII thing that kinda shows what I'm talking about.
| FirstLineOfText |
| SecondLine |
| |
| |
But what I'm getting is more like
| FirstLineOfText |
| SecondLine |
| |
Try using two textviews. One for the first line and the second for the others in relative layout. Then align the second one to left using android:layout_alignLeft = "idOfFirstTextView".

Android: while screen in landscape, recalculate the size of image

I am new to android and currently have project on hand. I did search some solution from Google and StackOverflow. I really have no clue how to do it.
In the project i did specify the size of holder image for display well during listview rendering image. Besides that i did calculate the size of item in listview in order to expand the listview in scrollView. But when it comes to landscape, i want the size that i specified to be recalculate, so the child of listview (such as textView) can expand. And the image I specified in portrait can expand (full screen) in landscape as well.
Please help! What steps i need to take for re-calculating it? or is there any other solution?
Below is the example of my layout, the size of image in landscape is same as in portrait ,i.e., as i specified it. So what are the functions that i can call once i change the view mode? Thanks.
___________________
|...................| ________________________________
|. .| | .................... |
|.Image Full Screen.| | . . |
|. .| | . Image could not . |
|...................| | . full screen . |
| | | .................... |
| Portrait mode | | |
| | | Land scape mode |
| | | |
| | |________________________________|
|___________________|

How to lay out 3 buttons above 4 buttons, centered?

I need to lay out a centered row of 3 buttons above a centered row of 4 buttons:
+------+ +------+ +------+
| | | | | |
+------+ +------+ +------+
+------+ +------+ +------+ +------+
| | | | | | | |
+------+ +------+ +------+ +------+
All the buttons are the same size and the inter-button gaps should be the same on both rows. I can do this easily with nested LinearLayouts, but I'd like to do this without nesting layout views. (Aside from all the advice to avoid nested layouts, I need to traverse the buttons in code and it's a lot easier with a flat layout.)
I can do this with a RelativeLayout if the rows have the same number of buttons, but I can't figure it out when the button counts differ. Is there a way to use one of the stock layout views (it seems silly to write a custom layout view for this) to do a flat layout?
It seems like this can't be done in single layout. You can't use relative layout because it doesn't support gravity and you need views center aligned.
Custom layout is a perfect solution for this case. It's not that hard really. I experienced a lot simpler cases that couldn't be done using single standard layout.

Categories

Resources