Search code examples
androidandroid-linearlayout

How to add a textview and an edittext once I clicked a button?


I want to ask how to add a textview and an edittext once I click a button? My button to add a textview and edittext is the "Button addStaff"

Here is my code:

_12_EventAssign.java

public class _12_EventAssign extends AppCompatActivity {

LinearLayout linearL;
TextView tvdept1;
EditText etdept1;
TextView tvheadofdept1;
EditText etheadofdept1;
View lineview;


Button addStaff;
LinearLayout staff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity__12__event_assign);

    getSupportActionBar().setTitle("Add Event: Information");

   getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    linearL = (LinearLayout)findViewById(R.id.ev_assign_linearlay);
    tvdept1 = (TextView)findViewById(R.id.textv_dept1);
    etdept1 = (EditText)findViewById(R.id.editT_dept1);
    tvheadofdept1 = (TextView)findViewById(R.id.textv_headofdept1);
    etheadofdept1 = (EditText)findViewById(R.id.editT_headofdept1);
    lineview = findViewById(R.id.view1);

   // staff = (LinearLayout)findViewById(R.id.add_staff);


    addStaff = (Button)findViewById(R.id.btnaddstaff);
    addStaff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            linearL.addView(tvdept1);
        }
    });
}
}

Here is my xml file

activity__12__event_assign.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ortegapatriciaa.enventer._12_EventAssign"
android:background="@color/colorWhite"
>


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ev_assign_linearlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/add_staff">

            <TextView
                android:id="@+id/textv_dept1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="25dp"
                android:text="Department"
                android:textColor="@color/colorBlack"
                android:textSize="23dp" />

            <EditText
                android:id="@+id/editT_dept1"
                android:layout_width="300dp"
                android:layout_height="40dp"
                android:layout_marginLeft="35dp"
                android:layout_marginTop="7dp"
                android:background="@color/colorGray"
                android:ems="10"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/textv_headofdept1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:text="Head of the Department"
                android:textColor="@color/colorBlack"
                android:textSize="23dp" />

            <EditText
                android:id="@+id/editT_headofdept1"
                android:layout_width="300dp"
                android:layout_height="40dp"
                android:layout_marginLeft="35dp"
                android:layout_marginTop="7dp"
                android:background="@color/colorGray"
                android:ems="10"
                android:inputType="textPersonName" />

            <View
                android:id="@+id/view1"
                android:layout_width="330dp"
                android:layout_height="1dip"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorBlack" />

        </LinearLayout>

        <Button
            android:id="@+id/btnaddstaff"
            android:layout_width="130dp"
            android:layout_height="45dp"
            android:text="Add Staff"
            android:textStyle="bold"
            android:textAllCaps="false"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:height="60dp"
            android:background="@color/colorWhite"
            android:drawablePadding="6dp"
            android:drawableLeft="@drawable/ic_person_add_black_24dp"
            android:gravity="left|center"
            android:padding="6dp"
            android:textColor="#000"
            android:textSize="18dp"
            android:layout_marginTop="5dp"
            />

        <Button
            android:id="@+id/btnsave"
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:text="Save"
            android:textAllCaps="false"
            android:textSize="22dp"
            android:background="@color/buttonColor"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            />


    </LinearLayout>
</ScrollView>

I hope you could help me. Thank you!


Solution

  • You can not add existing view to layout. You must create new TextView and add that:

        addStaff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = new TextView(getApplicationContext());
                tv.setBackgroundColor(Color.GRAY);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
                linearL.addView(tv, params);
            }
        });