I need to lay out several (for example, two) labels (textviews) as columns. The texts can be longer altogether than one screen line and may not contain line breaks, so they need to be formatted (wrapped) to fit the screen width.
I can arrange labels as columns if they are shorter altogether than a screen line. I can wrap a text to fit the screen if there’s only one column. But if the texts are longer and there are more than one column, I cannot achieve what I want, because the first column always fills the whole screen width.
I tried to do this via xml-markup. I tried a lot of markups using LinearLayout, TableLayout and RelativeLayout. All didn’t work. Maybe, I didn’t use them properly.
I can imagine the ways using code to manually calculate views’ widths, but haven’t tried. Any solution is welcome, but using markup only are preffered.
Related
I am trying to have 3 LinearLayouts ordered horizontally (basically forming three columns) within another LinearLayout where the width of the middle layout can vary depending on it's content.
All columns should be visible at all times filling the viewport from left. The left and irght column will be assigned a max width. So only the size of middle layout varies. If the total width of all columns exceeds the viewport size the middle column must not overlap or push out the other columns. But instead it should use the remaining space.
I tried using layout weights but that would put the right column always on the right side and the middle column would fill up all the space even though it's content would not require that.
When I try to use a RelativeLayout as a container I either end up with all three columns overlapping each other or the first column disappears.
I thought the below code (only schematic for now, as I don't have access to the code atm) should work, but as written above the first LinearLayout does not show up. The last LinearLayout seems to be in place as desired.
<RelativeLayout>
<LinearLayout
android:layout_alignParentStart>
</LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd>
</LinearLayout>
</RelativeLayout>
Does anyone know how I can fix this? Please let me know if you need more detailed code examples etc. I will try to provide them as soon as possible.
I found a few other questions concerning this or similar topics but the solutions always used either layout weights or something like the code snippet above. So far I had no luck with those approaches, maybe because those questions where either for slightly different use cases or a few years old so that the api has changed in the mean time.
Any pointers are greatly appreciated :-)
Yes. You want to defined the center columns with a layout_width="0dp" and a layout_weight="1". The left and right columns will be layout_width="wrap_content".
A LinearLayout should contain the 3 inner "column" LinearLayouts.
I finally found a solution that works.
Using the layout_weight as describe by Jeffrey Blattman alone does only work when the views get large enough to fill the screen.
But as long as the views only fill a part you get gaps between them as the middle view fills up the remaining space. This is something I want to avoid in this case.
For some other reason I had to put my layout into a fragment. Now when I set the dimensions of the fragment to wrap_content the behavior is exactly as I want it. So the views do not get blown up while they are to small but are laid out as if there was no layout_weight defined. But still when growing larger the edge views stay within the screen.
My tablelayout displays a bunch of values that are random and is created programatically.
The issue I am having is that because I don't know the content that'll be displayed on the table I am not able to use setColumnShrinkable(index, true) and therefore since the table is big, some of the columns would get cut off and not show.
I've tried playing with mTableLayout.setShrinkAllColumns(true); but that would result in columns that shouldn't shrink to shrink and thereby making the wrapping of the texts formatted poorly (see image below) and also the height of each column is too large.
I've also tried playing with minWidth of the columns but that doesn't seem to be doing anything.
What would be the best wayt to tackle this issue? Thanks!
I'm having some views that display several tables with data. So far so good.
The problem is, the only two ways I can find to size these views is either with a fixed width, which is not what I'm after because the length of the table entries can vary somewhat.
The other is to not specify a width, in which case the table just fills the parent's width by default. Which is also not what I'm after, because then the tables become difficult to read in landscape mode, when the cells become much wider than their content.
All I want is a table that is as wide as it needs to be based on its content, but not wider (and then I'll have to get it centered, but I think I can figure that one out... then again, that's what I always think before finding my head bumping into a simple problem that I thought should be trivial to solve...)
Is there a way to do this, or do I have to write my own table class for that?
I'm creating a Google Glass app and need to display a simple table. The table will have 2 columns and a varying # of rows.
I'd like the text size to be dynamic based on how many rows are in use. So if you have just 3 rows and the strings in the cells are short (in terms of length) the text size should be larger. If, on the other hand the view updates and now there are 6 rows and/or the string length in the cells is greater, the text size should be reduced.
Plainly put, the text size should be computed so that the text is as large as possible while still fitting the entire table on screen.
Any advice on how to create a layout to achieve this? I found the GridLayout but I think I'll need to dnyamically update it since the # rows can vary. Then there's the text size issue.
This is the kind of problem that is easy on the Web but hard on Android. So one approach might be to put a webview into your app and show an HTML table you generate locally and inject into the webview.
You might have reasons this won't work for you. If you need a more native approach this is a problem that has been solved in native Android apps, basically by measuring the font size to see if it will fit in a space iteratively. You start small and increase the size until it doesn't fit, then use the size before the one that didn't fit.
Here is a thread about that approach:
How to adjust text font size to fit textview
That will adress the difficult issue of text size.
Once that is solved the layouts are easy, many containers will work including GridLayout, TableLayout or even a series of nested LinearLayouts.
I'm trying to create a layout that has a background (shown below) and have 10 check boxes (2 columns of 5 rows) in the white area. I can make this work via trial and error for one size view but how would i get this to keep the same aspect ratios for different screen sizes?
I'm open to any ideas, whether it's multiple layouts for different sizes or something more clever.
As you want to build a table of checkboxes (and associated text labels, I assume), I would suggest you use TableLayout.
Basically wrap your check boxes with LinearLayout and don't give hard coded values for
android:layout_width=""
android:layout_height=""
If you can't, my answer will change. Also TableLayout another approach for your work.