bIG_aL
bIG_aL

Reputation: 113

Button to add a custom layout to android application

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>

Upvotes: 0

Views: 69

Answers (1)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

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.

Upvotes: 4

Related Questions