Table layout span not working properly - android

I am trying to implementing a table layout but gap comes in between the left border and first column of the screen.
Secondly, I am not able to give required width to my spinner in first row of screen which always takes full width of screen.
I have tried with different permutations of code with stretch columns and layout_span, but they don't seem to work.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp>![enter image description here][1]
<TableRow>
<TextView
android:layout_column="1"
android:text="Adult 1"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
style="#style/text"/>
<Spinner
android:layout_width="20dp"
android:layout_marginLeft="2dip"
android:layout_column="2"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:background="#drawable/select_spinner"
android:entries="#array/Passenger_title"/>
<EditText
android:id="#+id/First_Name"
android:layout_column="3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="10dp"
style="#style/Edit_box_background"
android:layout_marginLeft="5dip"
android:layout_marginBottom="3dp"
android:textColor="#B3B3B3"
android:hint=" First Name "
android:textAppearance="?android:attr/textAppearanceSmallInverse"
android:padding="3dip"/>
<EditText
android:id="#+id/Last_Name"
android:layout_column="4"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="10dp"
style="#style/Edit_box_background"
android:layout_marginLeft="3dip"
android:layout_marginBottom="5dp"
android:textColor="#B3B3B3"
android:hint=" Last Name "
android:textAppearance="?android:attr/textAppearanceSmallInverse"
android:padding="3dip"/>
</TableRow>

firstly looking over your scenario I firstly recommend not to use android:layout_column="1" and layout_span unless required.By Default all items added to a TableRow would be added to right starting from first column.
And after give desired width to your Spinner.
Also one more mistake that you are making android:layout_column="0" you forgot to use it uses first column as index 0 not 1.
Hope My suggestion help you out and link you to right direction.

Related

how to rearrange my textview in my relativelayout?

Here's my layout screenshot:
How can I arrange all these TextView for them to have a good format (heading being Taking responsibility (means being aware)..) alignment etc.
<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="match_parent"
tools:context=".Menu"
android:background="#android:color/holo_blue_bright"
>
<TextView
android:text="*Follow safety guide for moving around the town and between towns."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView25"
android:gravity="left"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="51dp" />
<TextView
android:text="*Avoid crowds and do not participate actively in demonstrations even when it is related to programme work."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView25"
android:gravity="left"
android:layout_below="#+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp" />
<TextView
android:text="*Avoid crowds and do not participate actively in demonstrations even when it is related to programme work."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView25"
android:gravity="left"
android:layout_marginBottom="23dp"
android:layout_above="#+id/textView25"
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25" />
<TextView
android:text="*keep this briefing pack in an accessible place, and the emergency numbers in your mobile phone memory"
android:layout_width="wrap_content"
android:gravity="bottom"
android:layout_height="wrap_content"
android:id="#+id/textView28"
android:layout_above="#+id/textView25"
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25"
android:layout_marginBottom="46dp" />
<TextView
android:gravity="center"
android:textColor="#000"
android:textSize="15dp"
android:text="Taking resposibility(means being aware)"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView22"
android:layout_marginTop="18dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="*Avoid moving around town by yourself and always make others aware of your location."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:id="#+id/textView25"
android:layout_marginTop="76dp"
android:layout_below="#+id/textView22"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="*Share any security information that might have implications for Ballloon Ventures to the Country Manager or pc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView24"
android:gravity="left"
android:layout_below="#+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp" />
<TextView
android:text="*If using host home accommondation,check that it is secure.if it is not,tell the Programme Coordinator"
android:layout_width="wrap_content"
android:gravity="bottom"
android:layout_height="wrap_content"
android:id="#+id/textView28"
android:layout_marginTop="14dp"
android:layout_below="#+id/textView24"
android:layout_alignRight="#+id/textView25"
android:layout_alignEnd="#+id/textView25" />
<TextView
android:text="*Report all incidents to your Programmer Cordinator."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView26"
android:gravity="left"
android:layout_below="#+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />
</RelativeLayout>
Because if I drag one TextView it affecting all other TextView.
As #GabeSechan said, you need to learn what android:layout_xxx properties in RelativeLayout means.
When you moving a View in RelativeLayout via Layout Editor, you need to consider some of the following properties in your layout:
android:layout_above="#+id/textView25"
android:layout_below="#+id/textView24"
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/textView25" means that you want the View above textView25 view.
android:layout_below="#+id/textView24" means that you want the View below textView25 View.
The following properties means that you want the view to the right side of TextView25 View:
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25"
The following properties means that you want the view to align with the left side of the parent layout:
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
If you want to edit views position in a RelativeLayout, please don't depends with drag and drop via Layout Editor. You better do it manually except you want to have a headache later. Use Layout Editor to view the design result.
Design with a paper or imagination first, then code it after that.
Try to learn it from:
Relative Layout
Constructing View Layouts
The best practice for doing that would be to open a splitview between your code and design views and then editing the code to place the text in the correct location, using attributes such as padding, margin, aligning, below, above, etc. refer to this guide for positioning stuff in relative layout and combine it with other formatting attributes such as margin and padding.

Prevent GridLayout to Strech Ouside The Screen

I have a GridLayout with a ColumnWeight set to expand the middle column. this column has text on it, so the purpose of the weight is to make it expand as much as it wants to fit the text.
But if the text is too big, I would like it to add ellipses to the text and preserve the last column on the edge of the screen.
The behavior I am getting is that the last column is pushed out of the screen and the middle column grows as much as it wants....How can I prevent this?
Here's a sample code on how to replicate this issue:
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clipChildren="false"
app:columnCount="3"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
app:rowCount="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="- Start Text -"
android:id="#+id/textView"
app:layout_row="0"
app:layout_column="0"
app:layout_gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Test Test Test Test Test Test Test Test Test Test Test Test Test "
android:id="#+id/textView2"
app:layout_column="1"
app:layout_row="0"
app:layout_columnWeight="1"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="- End Text -"
android:id="#+id/textView3"
app:layout_column="2"
app:layout_row="0"
app:layout_gravity="right"/>
</android.support.v7.widget.GridLayout>
A curious thing to note is that if I add an android:maxEms="5" to the middle TextView, it works as expected, does anyone knows why? I don't want to add something like an android:maxEms="5" because this will limit the length of the text, instead I want it to occupy all available space left on the screen (and if I use android:maxEms="5" the 5 will have to be choosen for a specific minimum screen size).

Android TextView ellipsize

I'm using Android TextView to display the name of the CATALOG.
I added the specific width for TextView outer layout (150dp).
I wants to display "..." when text has more length.
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20dp"
android:singleLine="true"
android:ellipsize="end"
/>
</RelativeLayout>
Catalog Name: Furniture and Books
My result showing:
Furniture an...
But my expectation result is
Furniture and... (or) Furniture...
I wants to show the "..." when there is no space for word in catalog name.
Please check whether this works
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:singleLine="true"
android:text="Furniture and Books"
android:textColor="#android:color/holo_blue_dark"
android:textScaleX="1.1"
android:textSize="20dp" />
</RelativeLayout>
You can try this in java code, if it's really necessary.
String str = "Furniture and Books";
str = str.substring(0,8)+"...";
text_view.setText(str);
try to set the EMS for your text view, like :
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="end"
android:maxEms="4"
/>
by the way, using "sp" in fonts is a good practice ... any ways
where EMS is the size of characters your textview should hold, in practice, 4 EMS makes the text view holds 8 charachters (you can test it with your devices), and if you want to give it a fixed width based on the numer of charachters, you can use
android:ems="4"
instead

How to add row which is having multiple column dynamically in android?

Hi I have developed the UI like the screen below.Now I want to add the last row in the UI on last plus button click event.
I know how to add Button or EditText dynamically but I am not getting how can I add and delete row which is having multiple column dynamically.
Edit:- One Important thing about last row is each column is editable.That means I need the reference of each column for getting it's content.When I am adding row dynamically plus button should get placed in new row.Similarly if I remove row row plus button should get shifted to the upper row.
How can I do this.Any guideline or any approach will be appreciated.
This is my XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/navi_bar" />
<Button
android:id="#+id/title_bar_btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:background="#drawable/back_button_image" />
<Button
android:id="#+id/title_bar_btnExport"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/title_bar_btnBack"
android:layout_marginRight="10dp"
android:background="#drawable/export_button_normal" />
<!-- Create PDF Part 2 -->
<ImageView
android:id="#+id/pdf_Upper_Image"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title_bar_btnBack"
android:background="#drawable/grid_bg_part1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pdf_Upper_Image"
android:layout_alignParentLeft="true"
android:text="Kassenbuch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#3EC7F9"
android:textSize="23dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignTop="#+id/textView2"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/textView2"
android:text="Akshay"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<!-- Custom Pdf Part 2 -->
<ImageView
android:id="#+id/pdf_Middle_Image"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/pdf_Upper_Image"
android:background="#drawable/grid_bg_part2"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="2dp"
android:text="Einnahmen"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignTop="#+id/txtName"
android:layout_marginLeft="82dp"
android:layout_toRightOf="#+id/txtName"
android:text="Month :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/textView4"
android:layout_marginBottom="2dp"
android:text="Mand Nr. :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtMand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView5"
android:layout_alignTop="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:text="Mand"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignTop="#+id/textView4"
android:layout_toRightOf="#+id/textView5"
android:text="05"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp"
/>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtYear"
android:layout_alignTop="#+id/txtMonth"
android:layout_toLeftOf="#+id/title_bar_btnExport"
android:text="Year :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView5"
android:layout_alignBottom="#+id/textView5"
android:layout_alignLeft="#+id/textView8"
android:text="Blatt :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtMonth"
android:layout_alignLeft="#+id/title_bar_btnExport"
android:layout_alignTop="#+id/txtMonth"
android:text="2012"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/textView3"
android:text="Ausgabne"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" >
</TextView>
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/textView6"
android:text="Bestand"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_toLeftOf="#+id/txtMand"
android:text="Datum"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="#+id/textView13"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_toLeftOf="#+id/textView5"
android:text="Beleg Konto"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="8dp" />
<TextView
android:id="#+id/textView14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView13"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginRight="03dp"
android:layout_toLeftOf="#+id/textView13"
android:text="Gegen Konto"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="08dp" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView14"
android:text="Anfangsbestand/Ubertrag"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="9dp" />
<TextView
android:id="#+id/textView11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView13"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/textView12"
android:text="USt satz."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="09dp" />
<TextView
android:id="#+id/txtBlatt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_toRightOf="#+id/textView9"
android:text="Blatt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView11"
android:layout_alignLeft="#+id/textView9"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<include android:id="#+id/firstRow"
layout="#layout/custom_pdf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pdf_Middle_Image"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"/>
</RelativeLayout>
Thanks
Create a layout that represents a complete row,then you can use:
public View inflate (int resource, ViewGroup root) from LayoutInflater(a class in android.view package) to Inflate a new view hierarchy from the specified XML resource(for example your new row).
Now you can add this view(your row) dynamically to your main layout with a specific tag(like adding a button dynamically to the layout).
Create an XML layout file, which will represent a 'row in the list view.
Implement a custom Adapter, using base Adapter.
In the getView() method, Inflate the Xml layout, Reference the inner items (using outerLayout.findViewById(), set Listeners right there, and Finally return the view.
Write the implementation for your listener.
Simple enough? :-)
Try creating the whole layout at runtime. Create a single row containgin all ur columns and set visibility of the last one to gone. Change the visibility when the plus button is pressed.
Creating a row of type Relative layout will be good i think. When u want to remove a row Then you will have to count the Relative layouts in the main layout and then remove the index which u want to remove.
I think you will understand best through an example. Lets simplify things a little bit. Lets say your main layout is this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout" >
</TableLayout>
It's a simple TableLayout without any views yet (You could have pre-baked rows in it). Now you want to create TableRows wich you want to add dinamically, and handle it's subviews separately, right? You could create your whole TableRow in xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tablerow_textview"
android:text="Hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tablerow_button"
android:text="World" />
</TableRow>
Then the java part. Lets say you want to add 10 of those rows to your TableLayout, and handle each rows button and separately. You can do it like this (You must have access to a Context object):
// get the main TableLayout from xml
TableLayout layout = (TableLayout) findViewById(R.id.main_layout);
// get a LayoutInflater object. 'this' is the current Activity
LayoutInflater inflater = LayoutInflater.from(this);
for(int i=0; i<10; i++)
{
//Create an inflated instance of that layout
TableRow row = (TableRow) inflater.inflate(R.layout.table_row, null);
//Notice, that we call the findViewById() function on the previously
//inflated layout, which means, that we can only access its subviews
Button rowButton = (Button) row.findViewById(R.id.tablerow_button);
TextView rowText = (TextView) row.findViewById(R.id.tablerow_textview);
//Now that you have those Views, you can do whatever you want with them, (set
an onClickListener, change the text, etc)
//finally we need to add the row to the table
layout.addView(row);
}
It would also be a good idea if you wrapper created a class for your inflated layout, with its own getter-setter functions, and you could just instantiate it with the new operator, and all the inflation would be done in the background.
You need to work with custom views. Then each view can be a complex view and you can do what ever you wish, this task should not be easy.
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<com.sample.MyCustomView
android:id="#+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And then MyCustomView can be Linear Layout or what ever.
In above xml
you did all the things in Textview and adjust that
instead of using that you have to create a Tablelayout in your xml file.
onButtonClick event you have to create a new tablerow dynamically then add components by using tablerow.addview(componentsname) and finally add tablerow to tablelayout
Simple TableLayout
After trying lot of different things I solved my problem like below.I made certain changes in my xml.I used LinearLayout with ScrollView.
And I created one xml file which is having complete row.Now my task is to add that xml in the LinearLayout which I did in the following manner.I am calling this method on button click event at each time.
public void AddDynamicView() {
linearLayout = (LinearLayout) findViewById(R.id.show_pdfTableLayout);
layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_pdf3, null);
linearLayout.addView(view);
list.add(view); // Here I have created list of View so that I can get the id of specific view.
}
I have tried this with TableLayout but I was having some issue with occupying whole row.That's why I had used LinearLayout.
Probably I didn't understand well!!!
You can do everything mentioned in the question using ListView and Adapter.
You can provide an array to hold fields, feed an adapter with the array and feed listView with adapter.
If you need a new Row just add an element to the array.
If you want to delete a Row, just remove it from array.
If you want to sort items, just sort the array.
If you want to select prev/next, just use listview selection method.
I was wondered you checked your answer that is not absolutely correct approach. It's true but if your target ui is complex, the page will need a lot of time to render and scroll than ListViews that is dynamic and optimized.
If you think my answer sounds good, Let me know to update it with some code.

Android - Layout is giving buttons extra padding; giving me extra gray hairs [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to completely eliminate padding in a LinearLayout containing buttons?
What in the world could be giving me this extra ~5-6 pixels of spacing???? It's been driving me mad, I've tried setting the paddings/margins on the parent TableRow, each individual button, the textviews, everything!
Here's the XML layout of what is shown:
<TableLayout android:id="#+id/tableLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" >
<TextView android:text="Resource1 - $Price" android:id="#+id/resourceText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3"></TextView>
<Button android:text="#string/buybtn" android:id="#+id/buyButton1" android:onClick="buyButtonClick" android:layout_width="80dip" android:layout_height="36dip"></Button>
<TextView android:text="XXX" android:id="#+id/resourceHeld1" android:layout_width="30dip" android:layout_height="wrap_content" android:gravity="center"></TextView>
<Button android:text="#string/sellbtn" android:id="#+id/sellButton1" android:onClick="sellButtonClick" android:layout_width="80dip" android:layout_height="36dip"></Button>
</TableRow>
<View android:layout_height="1dip" android:background="#FF303030"/>
<TableRow android:id="#+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Resource2 - $Price" android:id="#+id/resourceText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3"></TextView>
<Button android:text="#string/buybtn" android:id="#+id/buyButton2" android:onClick="buyButtonClick" android:layout_width="80dip" android:layout_height="36dip"></Button>
<TextView android:text="XXX" android:id="#+id/resourceHeld2" android:layout_width="30dip" android:layout_height="wrap_content" android:gravity="center"></TextView>
<Button android:text="#string/sellbtn" android:id="#+id/sellButton2" android:onClick="sellButtonClick" android:layout_width="80dip" android:layout_height="36dip"></Button>
</TableRow>
etc etc etc copypasta
What chicken do I need to sacrifice to get that padding out of there? In an ideal world, I would nudge the 1dip view up with a margin of 1dip against the bottom of the button.
Because in the buttons you have android:layout_height="36dip" intead of these android:layout_height="wrap_content" place evrything like android:layout_height="wrap_content"
Hope it helped
You can also use for spacing for every elements
android:layout_marginLeft="20dip"
android:layout_marginLRight="20dip"
android:layout_marginTop="20dip"

Categories

Resources