samedi 23 mai 2015

Programmatically Adding EditText with weight

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);

}

Aucun commentaire:

Enregistrer un commentaire