LinearLayout in HorizontalScrollView is not expanding - android

I have this custom HorizontalScrollView that dynamically builds the following layout
<HorizontalScrollView width=match_parent>
<LinearLayout width=match_parent horizontal>
// here TextViews are dinamycally added
</LinearLayout>
</HorizontalScrollView>
so whenever I need to update the TextViews, what is actually done is removeAllViews() in the LinearLayout and they are re-created and re-added.
The TextViews have a padding of a some DP and setLines(1);. When adding them to the LinearLayout, they're being added with LinearLayout.LayoutParams(0, WRAP_CONTENT, 1f) (zero width, but weight=1).
The objective here is that if the TextViews are smaller than the screen width they will expand to fit the screen and if they're bigger than the screen width they will be padding + text lenght + padding and scroll horizontally in the screen.
It almost works except in some edge cases. Examples:
Works: I have 5 TextViews with a couple of short/medium words on each. All layouts nicely and you can scroll through them
Works: I have 2 TextViews with a couple of short/medium words on each. The TextViews expands to fill the screen and the text is centered on them.
Fail: I have 3 TextViews with the following texts: 1 lorem | 43 consectetur | 20 dolor the layout will simply match the screen width and the word "consectetur" will disappear (trying to go to the next line). Similar fails for different size words or numbers.
I tried requestLayout, invalidate, setVisibility(GONE);setVisibility(VISIBLE) updateViewLayout, setFillViewport(true); and trick out the layout weight of the TextViews based on the text lenght. All with no luck.
Does anyone knows how I could make this work without having to re-write the onMeasure of the LinearLayout (which I checked it's a total of 381 lines for the Horizontal version)

Related

How to set multiple scroll views size to change dynamically relative to each other?

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

android GridLayout divider row

I'm trying to implement a tabular layout that has a header and a bunch of rows underneath it. I've chosen the GridLayout (android.support.v7.widget.GridLayout) as there's some requirements for some elements to span multiple columns (but those are of no concern to the question).
My header cells each contain a LinearLayout with a bunch of TextViews, they're dynamically filled in code,for the sake of example, have a look at the image below.
The second row should contain the divider which is a simple view, that should span my header columns (3).
The problem is the width of the divider - if I choose MATCH_PARENT, it will push the GridLayout to fill the whole remaining space to the right. The grid needs to wrap the content and center itself horizontally. It seems to me there's a conflict between the grid's layout (WRAP_CONTENT) and the divider's layout (MATCH_PARENT).
How can I fix the width of the divider without hardcoding it?
http://i61.tinypic.com/2415eg5.png
In red, my LinearLayouts (header), green, the GridLayout itself, the thin blue line at the bottom is the divider.
Thanks,
MO
SOLUTION (as provided below):
I had to set the column weight for the divider to 1, without specifying a width (actually setting it to zero). Because of my specific requirement to handle all of these in code, the solution was to manually instantiate the GridLayout.LayoutParams class and use
ColumnSpec = GridLayout.InvokeSpec(row_index, num_spanned_cols, weight)
Hope this helps others in the future.
If you set the width to 0dp then give it android:layout_weight="1"
(you could give it any weight you want) it should fill all the available space and not push your bounds if I understand what you are asking correctly

Vertically centering overlarge text in Android buttons

Android, Button view. The Button size is 38x38dp, text size is 20dp (just one + character worth of text). Gravity is set to center|center_vertically.
The text is NOT centered vertically. According to Android's internal accounting, the text is too large for the specified button size, so the text is placed with its top aligned with the top padding, and its bottom cut off. That's not what I want; I want the text to be cut evenly on the top and on the bottom. In other words, vertically centered. The plus character, since it takes less than a full-sized character cell, won't suffer from that.
It's Gravity that I set, not Layout gravity. I know the difference.
Any ideas how to make vertical centering work in such conditions, short of overriding draw()?
Try adding android:includeFontPadding="false" to the Button view.
Some have suggested adding android:baselineAligned="false" to the container for similar problems but it didn't do anything for me.

linear layout are not getting divided equally when the text width inside them are changing

I have a linear layout (horizontal) inside which there is 3 linear layouts, each of them have a textview inside them.
Now I wanted to divide these linear layouts equally. So I added weight to them as 1. It works perfect.
But now when the text width inside them are varying they are no more of equal weight :(
Say for any linear layout i removed the text, or wrote a smaller text or varying length text then it is loosing the equal distribution.
What do I do to make the layouts uniformly distributed.
And one more thing i would like to know say the text width increases the max allocated width for these layout (these will be given dynamically may be by doing width = 1), then how do i move the text to next line.
Make sure you set the width to 0dip for each item inside the LinearLayout.

Android LinearLayout fill-the-middle

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.

Categories

Resources