1

I've created a Vertical LinearLayout with a Horizontal LinearLayout within it in order to hold 3 EditText boxes within them for data entry. I've also placed a button below the vertical LinearLayout that I want to program to add another row of editText with every press.

I've succeeded so far and managed to programmatically add another row with 1 edittext in it. Then I tried with 3 and hit a roadblock. I tried to use weightsum and weights to get the editText boxes to be in 3 columns. However with the code below I get 2 editText boxes next to each other then a very very tiny editbox as the last one.

What am I doing wrong? Why aren't the widths of the edittext boxes being divided by 3?

XML:

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="3"
                android:orientation="horizontal">

                <EditText android:id="@+id/edit_recipeIngredient"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_recipe_ingredient"/>

                <EditText android:id="@+id/edit_recipeAmount"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_recipe_amount"/>

                <EditText android:id="@+id/edit_recipeUnit"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_recipe_unit"/>

            </LinearLayout>

        </LinearLayout>

        <Button android:id="@+id/btn_add_ingredient"
            android:background="@color/orange"
            android:layout_marginTop="10dp"
            android:layout_below="@id/layout_ingredients"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="@string/btn_add_ingredient"
            android:onClick="addIngredients"/>

Java:

public void addIngredients(View view) {

    final String ADDINGREDIENT = "ADD INGREDIENT";

    Log.d(ADDINGREDIENT, ADDINGREDIENT);

    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_ingredients);

    LinearLayout ingredientLayout = new LinearLayout(this);
    ingredientLayout.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    ingredientLayout.setOrientation(LinearLayout.HORIZONTAL);
    ingredientLayout.setWeightSum(3f);

    EditText editTextIngredient = new EditText(this);
    editTextIngredient.setHint("Ingredient");
    editTextIngredient.setLayoutParams(new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            1f));

    EditText editTextAmount = new EditText(this);
    editTextAmount.setHint("Amount");
    editTextAmount.setLayoutParams(new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            1f));

    EditText editTextUnit = new EditText(this);
    editTextAmount.setHint("Unit");
    editTextAmount.setLayoutParams(new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            1f));

    ingredientLayout.addView(editTextIngredient);
    ingredientLayout.addView(editTextAmount);
    ingredientLayout.addView(editTextUnit);

    linearLayout.addView(ingredientLayout);

}

1 Answers1

0

The problem here is that you are adding the weighted layouts to a layout that is not on the screen, so its width is unknown at the time you add them.

You have a couple of approaches, but the easiest one is probably to grab the widths of the elements already on the screen and simply duplicate them (since you are using a weighted layout to begin with).

You could also measure the screen and use that measurement to set the widths of each layout.

Last, you could use ViewTreeObserver to add them after the parent has been added and the width is known (see the post here: Getting the width/height of a layout in Android).

The last is the most "interesting" but the least efficient and useful since your original layout already did the calculations.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • I've fixed it now. That wasn't the problem. My code actually works perfectly fine however I was an idiot and didn't actually assign editTextUnit any LayoutParams. I had accidentally put editTextAmount.setLayoutParam thus editTextUnit didn't have any layoutParams when it was added. My bad. – Alex Knight May 24 '15 at 19:45