tabview column according to screen size - android

I have a table
<TableLayout
android:id="#+id/branchTable"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/EditTextSubHeading"
android:layout_margin="10dp"
android:collapseColumns="30sp" >
</TableLayout>
I want to fit all column according to screen size.Mean if I have a Tab
10.1" and I have 5 column each column should take 10.1" / 5

try android:layout_span="value"
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#attr_android%3alayout_span
you can also try android:layout_column="value"
tutorial http://www.mkyong.com/android/android-tablelayout-example/

Add following code to your tablelayout:
android:stretchColumns="*"
android:shrinkColumns="*"
It means all columns are able to shrink and stretch. Android will handle how to allocate nice space for all columns. You can also specify which columns you want to strech or shrink as well, by setting numbers in the above code.

Related

Android:StretchColumns: Correct arguments?

For the following code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="2" >
Does android:stretchColumns="2" mean that the 3 columns are stretchable, or that column 3 is stretchable?
A TableLayout can specify certain columns as shrinkable or stretchable
by calling setColumnShrinkable() or setColumnStretchable(). If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container.
Have a look here, keep in mind that if your table fits nicely ( no space left to stretch) in the parent object, you would not see any changes no matter what value you put in.
If u want to stretch all Columns u should use android:stretchColumns="*" it works also to android:shrinkColumns.
And this pro tutorial should explain all yours issues
http://www.onlinevideolecture.com/computer-programming/slidenerd/android-programming-course-android-tutorials-for-beginners/index.php?course_id=2369&lecture_no=59
android:stretchColumns="1" will stretch column 1
android:stretchColumns="0,1" will stretch columns 1 and 0.

Android - how to make a TableLayout of buttons fit differently sized screens

I'm developing an Android app using fragments. In two fragments I use a TableLayout to programmatically create a matrix view consisting of buttons. The dimension of the matrix differs for different runs of the app, but typically I have 10-15 rows and 10-15 columns.
I have OddsButton extend Button.
I would like to have this view fill the screen on different devices, but I haven't been able to understand what is the right approach. I can (by hardcoding values for width and height) impact the height and width of the buttons (see below for code), but I'm struggling with fitting it to a Nexus 7 anyway. If I first find the right height to use to make it fit the screen then when I start adding more width the buttons get a bit higher as well and there is no way for me to adjust that back.
Sorry if this question is strange, but I hope that someone can make sense of this. I realize that I might be trying the totally wrong approach for this.
for (int k=1;k<=n2;k=k+1){
OddsButton b = new OddsButton(this.getActivity());
b.setLeg1(no);
b.setLeg2(k);
b.setO(this.getO(no,k));
b.setIndex(this.IndexForCombination(no, k));
b.setMinimumHeight(height);
b.setHeight(height);
b.setMinimumWidth(width);
b.setMinimumHeight(width);
b.setPadding(0, 0, 0, 0);
Table Layout is a bit tricky. Please have a look at the example:
<TableLayout
android:id="#+id/myId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:weightSum="2"
>
<TableRow >
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="bla1"
/>
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="bla2"
/>
</TableRow>
</TableLayout
What is tricky is to first set android:stretchColumns="*" for layout (means stretch all columns) and next android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" (don't ask me why it wors like that, I will never understand some tricky things in Android, but good they work). Btw, weightSum is simply the sum of weight of elements you want to have in one row.
Sure you can have more tablerows, just the number of elements in each should be the same.
(P.S. please don't forget to give points if you like the answer ;)).

Main menu using GridView to occupy whole screen

I want my main menu be a GridView of icons - 6 icons to be specific.
When the phone is in the portrait layout i want them to be in 2 columns, when it is in horizontal layout - 3 columns --- have sorted that out.
What I didnt sort out is how to make them occupy merely the whole screen.
Fitting it to the screen width is not a problem - it is fitting it to the screen height that makes me stumble.
I want it to stretch/shrink my cells depending on the screen size. I have tried counting screen height and dividing it by 3 - but there are certain problems and I dont think that that is the most elegant solution.
So basically how do I occupy my screen (say in a portrait layout) with a gridview of two columns (3 rows) solely? Are there any xml attributes that could help me?
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
/>
I suggest creating the two layouts in xml first, then during the onCreate method you can do the re-sizing of the rows and columns.
For example
if layout is portrait
set column width = screen width / 2
else if layout is landscape
set column width = screen width / 3
Android Dashboard Pattern the answer to my question is somewhere there. Basically I used nested ListViews

Limit the width of buttons in Android

I have two buttons positioned horizontally next to each other and want to limit the width of the buttons when the screen resolution goes up. For mdpi, the two buttons should be equal length and take up the entire width of the screen. If you then switch to a higher resolution, the two buttons can grow in width if necessary but only up to a maximum width of say 100dp. I have not been successful in accomplishing this. The two buttons end up being the same size but they fill the entire width of the screen. I have tried putting the buttons in a table, in just a LinearLayout and have tried using the maxWidth on various combinations but to no avail. If I use a table, I have tried stretching the columns and not stretching them. That didn't help either. Any suggestions?
You say you've tried maxWidth...but you don't show any examples of what you tried, so here's my suggestion, which I'm nearly certain should work for you:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 2"
/>
</LinearLayout>
You haven't put your xml view here. How can we find an error?
Probably, you have used background image and width set to wrapcontent ? Change the source image size to smth small
You can try creating different layouts for different resolutions. This is not the best way of solving your problem, but this will surely work. Good luck!

Android Stretch columns evenly in a TableLayout

I am displaying a table of values in my android application, and would like the columns to be distributed evenly in terms of size , instead of sizing according to content.
Been playing around with stretchColumns but couldn't manage to figure out the right combination, Any Ideas?
I had the same problem - I always only entered one number in android:stretchColumns, but you have to enter all columns that should be stretched. So if you have three columns, you have to write:
android:stretchColumns="0,1,2"
Or write:
android:stretchColumns="*"
Then all columns will have the same size. As a reminder, android:stretchColumns is an attribute for the TableLayout element.
You need to set BOTH android:layout_width="0dip" and android:layout_weight="1" for each view within a table row. I think this works because the weights determine the proportion of the EMPTY SPACE in the row used by the respective views, and since the width of all views is set to 0dip, the whole row is empty space, and hence with equal weights the views are all allocated the same proportion of the WHOLE width.
Use the android:layout_weight for the columns
You control the size of the columns by the size of their contents. If you want them all to be the same size, set that up via android:layout_width in the appropriate cells.
Whoever is still looking for solution, for me:
setting columns both shrinkable and stretchable did the trick.
I had pretty much the same problem as OP.

Categories

Resources