Android TableLayout stretches cell contents - android

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>

Related

Android: ImageView next to TextView not showing

I have a TextView and ImageView in a ListView row, positioned next to each other. However, the ImageView doesn't show up at all, and doesn't register clicks either. This is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:text="text"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerVertical="true"
android:padding="10dp" />
<ImageView
android:id="#+id/imageView"
android:clickable="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/textView"
android:src="#drawable/ic_action"/>
</RelativeLayout>
The problem seems to lie in the layout_toRightOf line, if I remove it, the ImageView is shown, but in the wrong place. But I don't understand why it's causing a problem. What am I missing?
The issue is that the TextView is pushing the ImageView off the screen.
You can fix this using a LinearLayout and android:layout_weight
eg:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:text="text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_centerVertical="true"
android:padding="10dp" />
<ImageView
android:id="#+id/imageView"
android:clickable="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/textView"
android:src="#drawable/ic_action"/>
</LinearLayout>
More info on the layout_weight attribute:
This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.
You need to use LinearLayout with weight.. if you set fixed width and the size of the phone is small, it will either stretch out of the screen.
//do linearlayout with orientation horizontal
<LinearLayout
...
orientation = "horizontal"
...
>
<TextView
....
android:layout_width="0dp"
android:layout_weight="1"
...
/>
<Button
....
android:layout_width="wrap_content"
...
/>
</LinearLayout>
Play with android:layout_weight, you will understand

ImageViews and Buttons Android Apps

I'm trying to build an android application and I've created a screen with a table layout and added to the first table row an image view and two large texts, to the second row I've added one button.
When I change the text of the button it automatically changes the size of the image view above to match the size of the button.
Here is the code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ChildInfoActivity" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/child_info_image_descreption" />
<TextView
android:id="#+id/childInfoNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_text_view"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/childInfoHasArrivedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/has_arrived_text_view"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/more_info_button"
android:layout_weight="1" />
</TableRow>
why is that?
The reason is because when using TableLayout, each TableRow will have same size for each same column. Since the ImageView and Button are both on column 0, everything that affects Button's width will also affect ImageView's width.
If you permit the Button to span over many columns, add android:layout_span="2" to the Button. Else, use different layout, such as LinearLayout or RelativeLayout.
Because your row item, which holds imageView and textViews, has "wrap_content" value as height. So probably when your texts' heights getting bigger than your imageviews it increases its parent height as well.
You may try to put imageView and textViews inside a relativeLayout and wrap textViews with imageViews height and then put that relativeLayout into row item. It will be nested, but i think still it is better than given exact size because that way it wouldn't be seen so good on some devices...
Your buttons have
android:layout_width="wrap_content". If you set
android:layout_width="100dp"
then your width won't be dynamic.
remove android:layout_gravity="center_vertical" and as u have given added android:layout_weight="1" to button add this line to other components also

Android: How do I split TableLayout row similar to "rowspan"

I have a table layout and i want to split a cell into two rows
below is my code
<TableRow android:layout_marginBottom="10dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:textSize="17dp"
android:text="#string/cardiovascular_problems" />
<Spinner android:id="#+id/cardiovascular_spinner"
android:layout_width="200dp"
android:layout_height="50dp" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:textSize="17dp"
android:text="#string/if_yes_give_details" />
</TableRow>
In this row I would like to have 2 cells. the 1st cell has a text view
the second cell i want to split it into two rows
the 1st row has a spinner and text view and the 2nd row has an edit text
Is there a way to do this other than using another table layout in this row ?
It does not have to be a TableLayout, you can put the second column items e.g. into a RelativeLayout. I doubt that it is possible to easily do it without nested layouts.

Android -- how to strech columns of Tablelayout without streching its children

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>

Android: TableLayout next to 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.

Categories

Resources