简体   繁体   中英

Unable to dynamically add EditText to LinearLayout

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.

<LinearLayout
            android:id="@+id/layout_add_task"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/separator"
            android:layout_margin="5dp"
            android:background="@color/yellow_background"
            android:orientation="vertical"
            android:padding="5dp">

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

                <ImageView
                    android:id="@+id/iv_check_research_tasks"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_check_empty" />

                <EditText
                    android:id="@+id/et_task_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:selectAllOnFocus="true"
                    android:text="Task Name"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_add_team_member"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginTop="5dp"
                android:text="Add a team member"
                android:textColor="@color/text_green"
                android:textSize="15dp"
                android:textStyle="bold" />

            <LinearLayout
                android:id="@+id/ll_add_task"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_add_team_member"
                android:orientation="horizontal"
                android:weightSum="100" >

                <ImageView
                    android:id="@+id/iv_create_assignment_attach"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="10"
                    android:src="@drawable/icon_flag_alt" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="15"
                    android:text="Due"
                    android:textSize="15dp" />

                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="65"
                    android:hint="None"
                    android:selectAllOnFocus="true" />

                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="10"
                    android:src="@drawable/icon_calendar_empty" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/btn_create_assignment_trash"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="5dp"
                    android:text="Trash" />

                <Button
                    android:id="@+id/btn_create_assignment_done"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="5dp"
                    android:text="Done" />
            </LinearLayout>
        </LinearLayout>

Here is the Java code, the onClickListener implementation of the TextView.

tvAddTeamMember.setOnClickListener(new OnClickListener() {

        @Override

        public void onClick(View v) {
            LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
            EditText et = new EditText(getActivity());
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            et.setLayoutParams(p);
            et.setText("Text");
            ll.addView(et);

        }

You may proceed this way ! This is not what you're looking for. But your problem will get resolved.

1) By default add the EditText view in your XML and make it invisible.

2) On Clicking the TextView, make it visible.

enjoy !

Hi @Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below

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

            <ImageView
                android:id="@+id/iv_check_research_tasks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <EditText
                android:id="@+id/et_task_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:selectAllOnFocus="true"
                android:text="Task Name"
                android:textSize="20dp"
                android:textStyle="bold" />
        </LinearLayout>

Try to change the line :

 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

With this:

  android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 //Then do your work

If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:

ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();

This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.

Try with this code. replace ur line ll.addView(et);

by:: ll.addView(et,p);

and delete ur line et.setLayoutParams(p);

A simple way to do this.

        Button add = (Button) findViewById(R.id.add_checkpoint);
        final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);        
        // layout has assigned weight = "1.0" in xml & it's a child of ScrollView
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater oLayoutInflater = getLayoutInflater();
                // oView is declared globally & Layout "row_checkpoint" contains a EditText
                oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);

                final EditText speciesEditText = (EditText) oView
                        .findViewById(R.id.checkpoint); 
                speciesEditText.setId(layout.getChildCount());
                // Perform whatever required over EditText, same way you can add more elements.
                layout.addView(oView);
            }
        });

Hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM