as the title means, I want my tablelayout to evenly assign each column's width and the child of each column be centered inside the column. Setting android:stretchColumns="*" also, however, streches the child inside each column.
any ideas for hacking this issue?
using horizontal Linearlayout to imitate a tablerow is acceptable, but I also have no idea how to get a Linearlayout to arrange its children evenly :(
pls help,
thanks in advance.
No hack needed. :-)
Simply set the layout_width of the children to wrap_content instead of fill_parent, or to a fixed value. Set layout_gravity to center or center_horizontal.
EDIT: Code added.
Try this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:shrinkColumns="*" android:stretchColumns="*">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="center"
/>
</TableRow>
</TableLayout>
Related
I have an xml layout which serves as an item in a ListView. The thing is: when it's added there, the item's width isn't stretched to match_parent, it wraps content instead. It is used in a ListView, so its width needs to be match_parent. I've been reading about it all over stackoverflow, trying every solution availible, but I have had zero result: layout_weight set to 1 does nothing to my layout, stretching columns has given me the best result in terms of width, but in terms of layout itself it failed miserably.
Is there anything that can be done here?
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<TableRow
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<CheckBox
android:id="#+id/trackCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:clickable="false"
android:duplicateParentState="true" />
<TextView
android:id="#+id/trackNumberText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="00."
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/trackTitleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="Title"
android:textColor="#android:color/black"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/trackArtistText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="Artist" />
</TableRow>
</TableLayout>
Add android:stretchColumns="*" property in your TableLayout
android:stretchColumns
The zero-based index of the columns to stretch. The column indices
must be separated by a comma: 1, 2, 5. Illegal and duplicate indices
are ignored. You can stretch all columns by using the value "*"
instead. Note that a column can be marked stretchable and shrinkable
at the same time.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:descendantFocusability="blocksDescendants">
.....
</TableLayout>
Hope this will help.
I think what you want is tableLayout.setStretchAllColumns(true);. Unfortunately you cannot set that in xml, perhaps place it in your adapter.
None of the mentioned solutions have helped me, so trying to solve this for 3 days I found a solution myself. Unfortunately, the solution is programmatical, the following code is to be added to your custom adapter:
trackCheckBox.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
trackNumberText.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int width = context.getResources().getDisplayMetrics().widthPixels - trackCheckBox.getMeasuredWidth() - trackNumberText.getMeasuredWidth();
trackArtistText.setWidth(width);
trackTitleText.setWidth(width);
I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
The easiest way is to change your LinearLayout to a RelativeLayout.
You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"
See here why gravity won't work
You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference
The docs show you available properties.
android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.
Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.
Use it with or without the android:gravity in the second textview and try .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
If LinearLayout is Vertical, you can put only an object per line.
You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews
ex.
<LinearLayout vertical>
<LinearLayout horizontal>
<textview 1></>
<textview 2></>
</LinearLayout>
</LinearLayout>
I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!
I have a TableLayout inside a RelativeLayout with android:layout_centerInParent="true" as one of its attributes, but it is not being centered vertically.
Here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/line_drawing_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/activity_linking_tl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<TableRow
android:id="#+id/table_row_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp" >
<com.xx.myview
android:id="#+id/id_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.xx.myview
android:id="#+id/id_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<ImageButton
android:id="#+id/line_drawing_b_restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:padding="22dp"
android:src="#drawable/ic_restart" >
</ImageButton>
<!-- There are more views/widgets here -->
</RelativeLayout>
How would I solve this?
Are you sure that the RelativeLayout is taking up as much space as you think? It might be that the TableLayout is centered, but that the RelativeLayout is wrapping content, so it ends up being centered but in a smaller container than you expected. It's hard for me to gauge without seeing the RelativeLayout portion of the XML.
One way to check your layouts is to use the developer option "show layout bounds":
It will give you a better idea of how your layouts are being constructed. If you are running an older phone without this developer option, I would just start setting flat colors for View backgrounds (e.g. android:background="#F00").
Try setting parent RelativeLayout width and height to match_parent.
and add this to your TableLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
The problem was that I had android:layout_below="xxx" as well (for some reason not shown in the code I posted), which overrode the centering I was trying to achieve. Removing this solved it. Obvious really.
Here is the issue.
I'm creating a TableLayout containing a some ImageButtons.
Each row has 3 ImageButtons but their width does not equal the width of the table.
Also the same applies for their height.
All I want to do is center the ImageButtons within the cell they are in.
The
auto:StretchColumns="*"
stretches the ImageButtons (cell data), even I have specified their size in the XML file.
Also, are there any options to do the same for the rows?
Something like calculating the margins of the cells automatically.
Have you tried setting the android:layout_gravity="center" for the ImageButtons?
Alternatively, you could wrap each ImageButton in a LinearLayout like this:
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageButton android:layout_gravity="center" ...other attributes... />
</LinearLayout>
Children of the Table Layout do not have to be only TableRow. The Table Layout supports children of all types including LinearLayout, TextView, ImageButton, etc.
Do not wrap your image button in a TableRow. Insert it as a row.
<TableRow>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example Table Row" />
</TableRow>
<ImageButton
android:id="#+id/ib1"
android:layout_width="fill_parent" <---! The Parent Is The Table Layout !--->
android:layout_height="wrap_content"
android:background="#drawable/coolBGImage" />
<TableRow>
BLAH BLAH BLAH
</TableRow>
To put 3 ImageButtons in one row use a LinearLayout with android:orientation="horizontal"
</TableRow>
<LinearLayout
android:id="#+id/LL1"
android:layout_width="fill_parent" <---! The Parent is the TableLayout !--->
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/IB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/coolBGImage1"
android:weight="1" />
Add the next two image buttons with the same weight to make the buttons divide the row evenly.
</LinearLayout>
In Android I'm trying to use this xml to get a TableLayout next to a LinearLayout. But I can't get it working. The TableLayout always takes up the full width of the screen. How can I get it to share the screen width with the LinearLayout?
<LinearLayout android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content" android:orientation="horizontal">
<TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5">
<TableRow>
<TextView android:text="Name1:" />
<TextView android:text="Value1" />
</TableRow>
<TableRow>
<TextView android:text="Name2:" />
<TextView android:text="Value2" />
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5"></LinearLayout>
</LinearLayout>
Like stated above, don't put the table width as 0dp. I would make the root element a Relative layout then state that the linear layout should be to the right of the table layout.
this says that your layout width of the linear layout is 0dp. make it wrap content and have the table layout with weight: 1 > linear: 0 and table layout set to fill parent.