How to add a row that dynamically contains multiple columns in android?

Hi, I have developed a user interface as shown below. Now I want to add the last line to the user interface with the last click on the "+" button.

I know how to add dynamically Buttonor EditText, but I don’t understand how to add and remove a row that dynamically has multiple columns.

Edit: - . One important thing on the last row is each edited column. This means that I need a link for each column for its contents. When I add a line dynamically, plus the button should be placed on a new line. Similarly, if I delete the row plus row, it should be shifted to the top row.

How can i do this. Any guidance or any approach will be appreciated.

enter image description here 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>

thank

+5
source share
8 answers

After trying a lot of different things, I solved my problem as shown below. I made certain changes to my xml.I used LinearLayoutwith ScrollView.

And I created one xml file that has a full line. Now my task is to add this xml to LinearLayout, which I did as follows. I call this method every time I click.

     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 tried this with TableLayout, but I had a problem with taking a whole line. That is why I used LinearLayout.

0
source

Create a layout representing the complete line, then you can use:

public View inflate (int resource, ViewGroup root) LayoutInflater ( android.view) new view hierarchy XML (, ).

( ) (, ).

+2
  • XML, .
  • .
  • getView() Xml, ( outerLayout.findViewById(), Listeners , , return view.
  • .

?: -)

+1

. , ur, , . "". , . , , .

+1

, . . , :

<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>

TableLayout - ( ). TableRows, , , ? TableRow 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>

java. , 10 TableLayout . ( Context):

// 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);
}

, set-setter, , ,

0

. , , , .

<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" />

MyCustomView Linear Layout - .

0

xml

, , Tablelayout XML .

onButtonClick , tablerow.addview(componentsname) , , tablerow tablelayout

Layout

0

, !!!

, , ListView Adapter. , feed listView . , . , . , . prev/next, listview.

, , . , ui , , ListViews, .

, , , .

0

All Articles