1

I am trying to add a new LinearLayout with a EditText element in it whenever a button on my app gets pushed. The xml I have is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/app"
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:weightSum="1">
   <EditText android:id="@+id/dish_name"
      android:layout_width="match_parent"
      android:layout_height="58dp"
      android:hint="@string/dish_name"
      android:background="@drawable/my_border"
      android:paddingLeft="10dip"/>
      <LinearLayout android:id="@+id/ingredient_list"
         android:layout_width="match_parent"
         android:layout_height="58dp"
         android:weightSum="1"
         android:layout_marginTop="20dp">
         <EditText android:id="@+id/edit_message"
             android:layout_width="0dp"
             android:layout_height="42dp"
             android:hint="@string/edit_message"
             android:background="@drawable/my_border"
             android:layout_weight="1"
             android:paddingLeft="10dip"/>
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/button_send"
             android:onClick="sendMessage" />
       </LinearLayout>
</LinearLayout>

I am trying to duplicate the second LinearLayout along with the EditText inside of it. The java code I have so far is:

public void sendMessage(View view) {
   LinearLayout layout = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setWeightSum(1f);
    params.weight = 1.0f;


    params.setMargins(0,20,0,0);

    EditText editText = new EditText(this);

    editText.setBackgroundResource(R.drawable.my_border);
    editText.setHint(R.string.edit_message);
    editText.setLayoutParams(params);

    LinearLayout container = (LinearLayout)findViewById(R.id.app);
    container.addView(editText);

}

The problem I am running into is that when I click the button I get the following:

AppImage

I want the EditText box to be the same height as the one I have defined in the xml but it takes up the entire screen.

Any ideas?

1
  • remove layout.setWeightSum(1f);params.weight = 1.0f; Commented Oct 11, 2016 at 7:31

3 Answers 3

3

No need to re-define the layout entirely within Java. You should define it as its own XML file. Call it ingredient_input.xml

For example, feel free to modify. This is one "row" for the EditText and button, so a horizontal layout. Could also be a RelativeLayout.

  <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="58dp"
     android:weightSum="1"
     android:orientation="horizontal"
     android:layout_marginTop="20dp">
     <EditText android:id="@+id/edit_message"
         android:layout_width="0dp"
         android:layout_height="42dp"
         android:hint="@string/edit_message"
         android:background="@drawable/my_border"
         android:layout_weight="1"
         android:paddingLeft="10dip"/>
     <Button
         android:id="@+id/btnAddMore"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/button_send"/>
   </LinearLayout>

First, you need to get the parent linear layout

LinearLayout ll = (LinearLayout) findViewById(R.id.ingredient_list);

Then, you can inflate the XML file for the "input row"

LayoutInflater inflater = LayoutInflater.from(MainActivity.this); // some context
View row = inflater.inflate(R.layout.ingredient_input, null);

You can also find the button, then set its click listener

row.findViewById(R.id.btnAddMore).setOnClickListener(...); // TODO

Then, just add that view onto the parent linear layout

ll.addView(row);

Realistically, you really could be using a ListView with an Adapter instead.

Sign up to request clarification or add additional context in comments.

Comments

0
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);

In the code above, you have the height set to match_parent and widht set to wrap_content. Instead, try setting the height and the width to match your xml. The height should be 58dp and the width should be match parent.

Hope this helps!

Comments

0

I haven't the time to test this but you can try:

// assuming you have variables for ingredient_list and edit_message
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ingredientList.getLayoutParams();
LinearLayout.LayoutParams editTextParams = (LinearLayout.LayoutParams)editMessage.getLayoutParams();

You can then use these instead of creating the parameters.

P.S. you don't have a specified orientation in your ingredient_list in the xml.

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.