0

So I am trying to add EditText to LinearLayout programmatically. I have added a '+' ImageButton on whose click, I will add an EditText until there are five. Below is a snippet of my XML file.

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_16"
            android:orientation="horizontal">

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



            </LinearLayout>

            <ImageButton
                android:id="@+id/add_field"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginBottom="@dimen/margin_16"
                android:background="@drawable/cyan_top_rounded"
                android:src="@drawable/add" />
        </LinearLayout>

The LinearLayout with id "Container" will have EditText as child. Below is my code for adding EditText.

  mAddField.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (count < 5) {
                mAddField.setEnabled(true);
                mContainer.addView(getTextField());
                count++;
            } else {
                mAddField.setEnabled(false);
            }
        }
    });

private EditText getTextField() {

    EditText et = new EditText(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    et.setLayoutParams(params);
    et.setText("abcd");

    return et;
}

 private void initUI() {
    mAddField = (ImageButton) findViewById(R.id.add_field);
    mContainer = (LinearLayout) findViewById(R.id.container);

    EditText et = new EditText(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    et.setLayoutParams(params);
    et.setText("abcd");
    mContainer.addView(et);
}

However, when I click on the ImageButton, nothing happens. If the EditText is added at all, the LinearLayout also does not expand. Please help.

3
  • try changing android:layout_height="match_parent" to android:layout_height="wrap_content" Commented Sep 29, 2016 at 6:52
  • Remove weight & set height to wrap_content & check. Commented Sep 29, 2016 at 6:52
  • yes this one worked. I set height of both LinearLayouts to wrap_content. Thanks men Commented Sep 29, 2016 at 7:04

3 Answers 3

1

Try this:

...
            <LinearLayout
                android:id="@+id/container"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">



            </LinearLayout>
...
Sign up to request clarification or add additional context in comments.

Comments

1
Change ur layout to this it will work fine:(Make the necessary changes according to ur own). Problem is with the height and width of the outer as well as the inner LinearLayout.

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

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

    <ImageButton
        android:id="@+id/add_field"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/image" />
</LinearLayout>

Comments

0

The view in which you what to add your Edittext dynamically,you have to set it width and height as "wrap_content". So just change your internal layout to :

 <LinearLayout
                android:id="@+id/container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

I think this will help you out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.