0

I have a small timesheet application of which I have added a screenshot because it is easier to show you than try to explain it all!

enter image description here

Hopefully from the picture you can see there needs to be a job number for the different sites where a worker will be and they can fill out their hours relative to each job number for the time spent on that job.So here is my problem I dont know how many job numbers they will have per week so here I just added in three but it may be more or less,could I have an "add job" button which could add one of the job views underneath each time it is pressed?

Below is the xml code for the section I would like to add each time the button is pressed

<LinearLayout
    android:id="@+id/MainLayoutThree"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >  //start of 1st job layout

<LinearLayout
    android:id="@+id/LayoutTwoA"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/beige"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/jobNo" >
    </TextView>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>

     <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ordHours" >
    </TextView>

    <EditText
        android:id="@+id/editText4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>

</LinearLayout>

<LinearLayout
    android:id="@+id/LayoutThree"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/beige"
    android:orientation="horizontal" >

     <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/overTimeHours" />

</LinearLayout>

<LinearLayout
    android:id="@+id/LayoutFour"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/beige"
    android:orientation="horizontal" >

     <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x1.25" />

     <EditText

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>


     <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x1.50" />

     <EditText

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>


     <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x2" />

     <EditText

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>

  </LinearLayout>

Greg
  • 9,068
  • 6
  • 49
  • 91
bIG_aL
  • 113
  • 1
  • 4
  • 12
  • possible duplicate of [Adding content to a lineary layout dynamically?](http://stackoverflow.com/questions/6661261/adding-content-to-a-lineary-layout-dynamically) – Manish Dubey Oct 07 '14 at 11:57

1 Answers1

4

You can inflate the View in button's onClick as shown below:

addButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        LayoutInflater inflater = LayoutInflater
                        .from(getApplicationContext());
        View view = inflater.inflate(R.layout.job_row_layout, null);
        containerLayout.addView(view);

    }
});

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Thanks,So would I add my job layout xml from above to a new xml file in the layout folder?? – bIG_aL Oct 07 '14 at 12:01
  • 1
    Yes. @bIG_aL and inflat that xml. – MysticMagicϡ Oct 07 '14 at 12:03
  • Is there anyway to dynamically add a custom id to each view as they are created and how could I structure my code to grab those ids?If I wanted to grab the hours from the 3rd or 4th job created how could I go about doing that? I dont know if I should post this as a new question or should I be asking this in the comments sorry – bIG_aL Oct 07 '14 at 12:15
  • @bIG_aL You can set their Id and use it for retrieving data, too. [Example](http://stackoverflow.com/questions/5923587/how-to-get-data-from-each-dynamically-created-edittext-in-android) – MysticMagicϡ Oct 07 '14 at 12:17
  • 1
    Great I am going to go and study the example you supplied,Thanks! – bIG_aL Oct 07 '14 at 12:22