Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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>

Thanks

like image 786
Akshay Avatar asked Oct 06 '22 17:10

Akshay


1 Answers

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

like image 131
hasanghaforian Avatar answered Oct 10 '22 03:10

hasanghaforian