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.
Related
I have a list view with each list item being designed in a RelativeLayout. Within each relative layout I have three buttons and two text views. Everything should be in one row. The first text view is supposed to be left aligned, while the buttons are in one horizontal row aligned to the end of the parent, with the second text view being directly before them. My problem is, that very long text in the primary text view overlaps with the buttons and the second text view. The text does break into a second line if long enough, but I'd like it to break.
For now I have achieved the list to look like this. Sadly the text does not break before the buttons/associated text, but only at the end of the parent.
Previously I had a layout_marginEnd of 160dp, which worked fine on my personal phone, but since then I have received bug reports from users with other phones that own another brand.
My code looks as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:textIsSelectable="false"
android:textSize="#dimen/listItemTextSize"
tools:ignore="UnusedIds" />
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
tools:ignore="RelativeOverlap,UnusedIds">
<TextView
android:id="#+id/amountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:textIsSelectable="false"
android:textSize="#dimen/listItemTextSize" />
<ImageButton
android:id="#+id/subtractButton"
style="#style/Theme.ShoppingList.ImageButton"
android:background="#drawable/ic_minus"
android:contentDescription="#string/mainSubtractButtonDescription" />
<ImageButton
android:id="#+id/addButton"
style="#style/Theme.ShoppingList.ImageButton"
android:background="#drawable/ic_plus"
android:contentDescription="#string/mainAddButtonDescription" />
<ImageButton
android:id="#+id/deleteButton"
style="#style/Theme.ShoppingList.ImageButton"
android:background="#drawable/ic_trash"
android:contentDescription="#string/mainDeleteButtonDescription" />
</LinearLayout>
</RelativeLayout>
Thank you in advance for your help!
Add android:layout_alignParentStart="true" and android:layout_toStartOf="#id/buttonLayout" to the nameTextLayout.
This will break nameTextView before LinearLayout and keep nameTextView's text left aligned.
Add android:layout_toStartOf="#id/buttonLayout" to the nameTextLayout. This will make it stop before that linear layout and break there.
I am programming an Android app and I need to create a grid with 4 rows. In each row I need to put different elements. The problem is that I can't insert the elements in the desired row. In my example the ImageButton is inserted in the first row although I want to insert it in the second row. Does anyone have a solution how to do this with XML ? Thank you
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="4"
tools:context=".GridXMLActivity"
tools:layout_editor_absoluteX="56dp"
tools:layout_editor_absoluteY="0dp">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="160dp"
android:layout_height="90dp"
android:layout_row="2"
android:layout_gravity="left|top"
android:src="#drawable/scene1" />
</GridLayout>
from the docs
Notice in the XML above, each TextView does not specify a row or
column. When these are not specified, the GridLayout assigns each
child view in order
cells are filled L-R, T-B in a horizontal layout, or T-B,L-R in a vertical layout
or you can specify explicit row and column values
<TextView
android:text="Cell 0"
android:textSize="14dip"
android:layout_row="0"
android:layout_column="0" />
I have a ListView which each row of which has the following
1. TextView
2. Button
The TextViews can contain text of multiple lengths. So the list appears in an awkward way. I thought the way out will be to set each List item to a Table Row with 2 cells(TextView and Button) . Is there a way to do this? Or may be there is a better solution to this?
You could use Relative Layout to display the row. In the Relative Layout put a button on the right with align parent right as true, and then set the TextView to the left of the Button to match parent. Also you could specify the TextView to be single line and ellipsize as true at end. This is just an example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toLeftOf="#+id/button1"
android:text="This is your textView with a very long text. I hope this code serves your problem" />
</RelativeLayout>
You could also use 2 TextViews, that totally depends upon the text that you want to display.
LinearLayout and it's weight attribute. More in official docs http://developer.android.com/guide/topics/ui/layout/linear.html
How do you put two children into one cell in table layout I andorid? Is is even possible, can't seem to find anything on the internet.
Wrap the two children in a layout. This will cause the cell to have one child that has two children in it.
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
</TableRow>
Just combine the two Views into one LinearLayout and make that the cell.
So the hierarchy is like this:
- table
- row
- view
- view
- layout
- child view
- child view
- view
- row
...
Then both children will appear as one cell (in my example above, the 3rd of 4 cells).
Yeah, you can wrap them in a LinearLayout
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>