Alignment of items in row when using TableLayout

Im trying to figure out how to line up items in each row of a table (TableLayout) - ideally I want the center of each cell to line up horizontally. The documentation isn't very clear on what's possible with TableLayout. Each cell has a different view widget so I thought a tabular layout would work well but they are all different sizes and default to top-left alignment inside each cell.

If I can't line them up then Ill need to use a different layout manager


Asked by: Owen513 | Posted: 24-01-2022






Answer 1

Set the layoutGravity of the view in the cell to be center-vertical, center-horizontal, center.

The layoutGravity tell the views parent how it wants to be displayed, so this will cause the table row to center it in the cell.

Answered by: Blake827 | Posted: 25-02-2022



Answer 2

You can use this code to center a view within a table layout

layout.setGravity(Gravity.CENTER);

Answered by: Catherine959 | Posted: 25-02-2022



Similar questions

Need help with Android TableLayout alignment

I'm trying to build a calculator layout using TableLayout, but the last two rows aren't aligning with the rest of the layout. Is there something wrong with my layout XML? What I'm trying to do would be easier to accomplish in HTML (<td> with colspan or rowspan), so should I try converting this into a WebView? Code is as follows: (


android - Alignment issues with TableLayout when adding Buttons

I created a TableLayout for my application. Everything is working fine, but once I add the button image at the bottom of the page the alignment breaks. Here is an image before entering the button image code: After entering the code for the button images:


android - TableLayout alignment with icon

Here is my XML. I want to align the icon to the left and the text should appear right next to the icon, with some space, like you see elsewhere. But the text is showing up in the center. (or right aligned to the 2nd column, in two column layout) <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_scroll" androi...


Android TableLayout Alignment

I have Layout XML Like this. This is inside LinearLayout. <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1" android:background="@android:color/background_dark"> ...


java - Alignment of imageview in tablelayout

This should be easy, how can I align the star in the middle. I'm adding the top header textviews and stars dynamically in code. I tried setting gravity on the imageview did not work. UPDATE: I added iv.setScaleType(ImageView.ScaleType.FIT_START) now star aligns to left


android - Alignment setting in Dynamic TableLayout

I want to add x rows when user press the add button and in one of the row user enters no of types i.e. in edittext then according to that i need to populate that many no.of rows in two columns below that .. these columns alignment is missing.. LAYOUT <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" a...


android - tablelayout tablerow layout alignment

I am totally out of idea why test,test1,test2,test3 is not proper align with first row. First row is refer to test,2012,test,test. I want the following row follow the first row in the table. xml: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" ...


Android TableLayout TableRow Alignment

I am using Table Layout to display data as shown below. What i want to do ? I want to align all content to the largest content. As you can see on the below picture Pilot column has the largest content. I mean what it should look like; ...


tablelayout - Android Button Alignment

I've did my due diligence in searching the net but I can't seem to find a proper solution for the following. I'm using a table layout and a table row and I am putting in 3 buttons with TEXT. Here is a sample of the layout file. <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tlRemoteTable" android:layou...


tablelayout - How can I create a table with borders in Android?

I use a table layout to display data as table, but I want a table with user-defined columns and rows with borders. Suggestions?


layout - android: help creating tablelayout

I have a hard time understanding the layout tutorial. I am not able to align columns on different rows. For example, what does 3dip mean in android:padding="3dip". The documentation says "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters). ". Specifically, I want to create a layout with top and bottom two rows, and the mi...


xml - Buttons to fill width when using TableLayout

I have a table having 2 rows each row having 3 buttons. How can I make the buttons to fill the space equally. In HTML I would give them 33% width. Also do you know any way I can create a view having 4 image buttons in a row as...


tablelayout - android table layout one for both (landscape and portrait view)

I want to create the following layout which should be 100% in landscape form as displaying in portrait form. alt text http://www.freeimagehosting.net/uploads/b12bb68569.png here is my code which i tried but not working <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" a...


tablelayout - android table layout rowspan

I want to build the following layout but it is not working. alt text http://toms-toy.de/rowspan.gif <LinearLayout android:orientation="horizontal"...> <ImageView ...></ImageView> <TableLayout ...> <TableRow..> <ImageView ...></ImageView> ...


tablelayout - Android table with round border

How can I make a table with a round border, similar to the photo below, in Android?


java - How can I get an Android TableLayout to fill the screen?

I'm battling with Android's awful layout system. I'm trying to get a table to fill the screen (simple right?) but it's ridiculously hard. I got it to work somehow in XML like this: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <TableRow android:layo...


layout - Android TableLayout question: get a fixed-width right column?

(Edited: I'm one step further than before) I've been trying to get a simple 2-column TableLayout going where I have a LinearLayout in my left column that I dynamically add elements to which fills up the horizontal space except for the fixed 100px right column. Here's what I have so far which works for me IF the LinearLayout contains more than one element. If it contains only one element, th...


android - How to align Buttons in a TableLayout to different directions?

probably I don't understand the layout properties of TableLayout yet. It doesn't seem to be possible to achieve such a flexible table like in HTML, because there are no cells. My target is it to achieve such a layout: How can I do that? I thought about using a GridView but this doesn't seem to be useful in XML. My efforts loo...


insert an image in a tablelayout without stretching it Android

I'm having some trouble getting pictures inside a tablelayout...actually, each I have 2 columns and when i put a picture in a cell of the table, it's completely stretched and disproportionnated...I can't find how to make it stay it's original inside the cell and let the rest of the space filled by white background for instance.... XML is like that: <TableLayout android:layout_width="fill_parent" android:...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top