Android Tablelayout - How to NOT make it fill the parents width? - android

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?

Related

Android TableLayout Wrap Text issues

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!

Need advice on view layout for Google Glass app - a table with varying # rows which should fill the screen (dynamic text size)

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.

Dynamic Android Layout Scaling (How-To?)

I'm stuck and I think wisdom (aka experience) is the only solution.
I need to be able to have boxes on a screen with data inside each box. The number of boxes on the screen needs to be able to change and the boxes need to scale accordingly (as well as the text data inside of them) to allow for the additional boxes.
Example: I might start with only 8 boxes which are evenly spaced and displaying data at a reasonable font scale.
As I add more boxes I need the rest of the boxes to scale down and allow everything on one screen.
I am currently attempting to use a TableLayout, but I'm not certain how to make everything scale correctly. Also, I'm generating everything programmatically by extending the TableLayout. Can someone point me in the right direction. Is there a better layout method that I'm not seeing? I'm sure I'm not the first person to need this, so are there examples available that I'm just not finding by searching?
The eventual solution was to build my own scaling function which took screen pixel dimensions and calculated the appropriate margin offsets to use on a relative layout. It requires a complete redraw each time the scaling factor changes, which is annoyingly resource intensive.

Table layout only as wide as first image?

I'm building an Android app, and using a TableLayout ViewGroup to display my data.
I would like the TableView to occupy the entire width of the screen, however I'm having trouble getting this to work.
What seems to happen, is the table is only as wide as the widest element in the table, in this case, the image that I put in it.
Here's a screenshot to illustrate the problem.
Additionally, here's the XML source code of my layout.
What specifically should I do to have the layout stretch to the entire width of the screen?
Thanks in advance!
You only have 1 column. Therefore, it should be stretchColumns="0".
Creating a single-column TableLayout is pointless and wasteful. Use a vertical LinearLayout, please. You will probably find that it works better with respect to your problem as a side benefit.
Also, do not repeat the xmlns:android="http://schemas.android.com/apk/res/android" -- it only needs to go on the root element.
BTW, columns are indexed starting from 0, so you are stretching a non-existent column in your existing TableLayout.

Two columns of autowrapped text

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.

Categories

Resources