Android TableLayout in a ListView, mysterious column shrinking - android

I am using TableLayout within a ListView. The data I'm presenting is tabular in nature and the TableLayout seems like a good way to ensure that the columns line up as desired. This approach worked well most of the time - see below.
Desired view:
But occasionally the Views representing the columns wrapped their text content as shown below.
After searching around for a while, I came across the following discussion on the Android Google Group. In response, I set android:stretchColumns="*" in the TableLayout and voila, the contained TextViews stopped mysteriously wrapping their text.
Here's a partial content of the layout's XML, with the change highlighted with a comment:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"> <!-- stretchColumns needed to prevent
TableLayout from unilaterally deciding to shrink the column
width and thereby causing text to wrap. -->
While I'm now getting the desired result, I'm a bit confused as to why this "fixed" my problem. The TableLayout documentation states that "The width of a column is defined by the row with the widest cell in that column." But if that's true, why did the columns apparently shrink in size in the first place causing the text to wrap?
Does anyone have a good explanation?

Containers query their children for how much space they want, and this process continues recursively. Any dynamically sized layout framework is going to work this way. From the inside out, children report up to their parents their desired size, and then on a second pass the parents report back to their children how much space is available to them. When they're not included in the stretchColumns list (either explicitly by position, or implicitly by wildcard as in the example), they ask for the bare minimum which in the case of TextView is the minimum space required to display the text wrapped. When they're included in the stretchColumns list, they report the width required to display the entire contents, and if it fits they don't get wrapped. Anyone who requests more space than is available to them gets wrapped text. I imagine the priority is given in order from least space required up to the most so if you have stretchColumns="*" the largest blocks of text are getting wrapped first (if necessary) and everyone else gets to be displayed normally.

Related

Inconsistent object placement inside CardView with RecyclerView

EDIT:
ADDITIONAL DETAILS: Strangely enough, it only gets buggy if there are only <=3 elements. More than that, because every element can be scrolled away, then the layout will "fix" itself upon reshowing.
ADDITIONAL DETAILS: Link to the demo video that showing the problem.
Youtube Video - https://youtu.be/zlwi_Bz-HQo
So below screenshot, was taken from the same run from an android studio.
In the screenshot, I have 2 exact same card view (with dummy data straight from the .xml, so, I assign no data from java file).
The problem
If you guys look at the bottom right of each CardView, there's a
text view called "read more".
Weirdly enough, even if they're identical, it's placed differently.
Btw, it's actually 3 identical cards. On the first run, the top "read
more" also incorrectly placed, but it auto-corrected itself when I
completely scroll it down and back to the top.
The second problem is the 5x4 dots on the top of the card. It's differently placed from what it's seen from the editor.
(The placement is accurate on editor)
Any idea how to handle this irregularity? Thanks.
Btw, I'm not sure if you guys need the code, but just in case, here it is on Pastebin (to shorten the post length).
visit_note_timeline.xml (the cardview)
VisitNoteAdapter.java (In case you're wondering, "visit note" is just a dummy empty class)
MainActivity.java
ratings_previews.xml (the 5x4 white dots on top right of the card, under more button)
activity_main.xml
content_main.xml
Use match_parent for RecyclerView.
Fix like this:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view_timeline"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
If parent view's width is wrap_content, parent view's width can not be determined until child view's width is determed.
I don't know the result of setting layout_width="wrap_content" for parent view and layout_width="match_parent" for child view.
I guess the result may be wrong layout.
In your layout, the same situation is happend.
RecyclerView has layout_width="wrap_content" and CardView has layout_width="match_parent".

Linear Layout baselinealigned warning on android

i am getting "Set android:baselineAligned="false" on this element for better performance" while using LinearLayout, I know its regarding performance,but i dont know exactly why it is,please clarify me
If you are looking for a visual explanation like me, then you might find this useful.
When baselineAlign is enabled(i.e if it is set to true), then all the text in that line will be aligned to have the same baseline.
Note: By default, baselineAligned is set to true. (i.e. baselineAligned=true)
When you make baselineAligned=false, all it needs to do is to add new elements to the linear layout and be done with it. The app need not worry about where the baseline of other elements in the layout is.
See the image below for more clarity
android:baselineAligned/setBaselineAligned(boolean): When set to false,
prevents the layout from aligning its children's baselines.
So can take example with linear layout with horizontal child views having multiple TextView with different text size or different views like button there basealignment would be different and you cannot adjust it to have same basealignment if you set it to false
Reference
Update:
By setting android:baselineAligned="false" , you're preventing the extra work your app's layout has to do in order to Align its children's baselines; which can obviously increase the performance. (Less unnecessary operations on UI => Better performance) as mentioned here

combining wrap_content on parent and fill_parent on child

Setting two or more elements of a linear layout the same height seems to be a great problem.
I want to set four buttons in a row to the same height.
android:layout_height="wrap_content" does it for the moment but when the text on one of the buttons is longer than one line this button is increased and therefore bigger than the other ones. Due to different localisations I don't know, when and which button may have a second line.
So my idea is, to set the parent linearlayout to android:layout_height="wrap_content" and all (!) child heights to android:layout_height="fill_parent".
This works (all buttons have the same size), but I'm not sure if this causes any other problems? Because it the parent gets it's height from the childs and vice-versa.
In theory what you are describing should not work ("Because it the parent gets it's height from the childs and vice-versa".) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is therefore perfectly valid and will work just fine.
That doesn't make sense :(
Why don't you use android:singleLine="true" and some ellipsode?

how to set height of textview according to its content -[android]

i have a message,its content is not fixed.
users message should be displayed in this textview. when i give a static message it displays in multiline, but when i use this through my code it shows only online.
It do not get wrapped.
i use inflater in code, used getter/setter to set its text
pls help,
thanks
You can set the lines of text to display with android:lines, or set a minimum and maximum number of lines with android:minLines and android:maxLines.
android:ellipsize would kick in only with a limited size, so if the text view is to grow to its content, it would not be needed, unless somehow constrained by the layout.
I also see you are using both layout_weight and layout_below/toRightOf. The first is available only in LinearLayout, the latter in RelativeLayout, so one of them should go, depending on which layout you are actually using.

Dynamically adjust the height of group headers in an ExpandableListView

I have an ExpandableListView (ELV) with the groups having LinearLayout. I have set the height of the group to some value (38dip in this case, equivalent to two lines of text). If the group heading is long and would take more than 2 lines, it is not shown properly in the ELV item - some part of the view gets scrolled. On the other hand, if I change android:layout_height to "wrap_content" in the LinearLayout, the groups always show all the lines. But the line widths are variable, i.e., short titles show up with only 1 line and long titles show up with 2, 3 or 4 lines. That looks ugly. I would like to implement the height to be something like max("38dip", "wrap_content"). Is there a way to do this?
Even programmatically, I do not seem to be getting the actual height of the group if I set android:layout_height to "wrap_content". Any suggestions there?
Could not solve the problem directly. Added the following to the xml layout file of the group:
android:paddingTop="7dip"
android:paddingBottom="7dip"
That makes the layout looking much less congested which is what I wanted to do in the first place. Moving on - but would be interested if someone else have a better solution.

Categories

Resources