steros
steros

Reputation: 1924

Android set layout programmatically results in wrong display

I have this LinearLayout in my Layout, if I copy that in my XML layout file I get another line with those three elements and everythings looks as expected. Now I try to add this LinearLayout and its child elements programmatically which works but looks differently and all wrong. The button seems to have the right width but the height is too low and the other two elements are hardly visible with wrong height and width.

This is the layout:

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

        <EditText
           android:id="@+id/editTextValueComposition"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="0.94"
           android:ems="10"
           android:hint="@string/valueHint"
           android:inputType="numberDecimal" >
        </EditText>

        <Spinner
           android:id="@+id/compositionSelector"
           android:layout_width="176dp"
           android:layout_height="wrap_content"
           android:layout_weight="0.06" />

        <Button
           android:id="@+id/button2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:onClick="addComposition"
           android:text="@string/add" />
    </LinearLayout>

And this is my code:

public void addComposition(View view) {
        final float scale = getBaseContext().getResources().getDisplayMetrics().density;
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        EditText valueEdit = new EditText(this);
        valueEdit.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 0.94f));
        valueEdit.setHint(R.string.valueHint);
        valueEdit.setEms(10);
        linearLayout.addView(valueEdit);

        Spinner compositionSelector = new Spinner(this);
        compositionSelector.setLayoutParams(new TableLayout.LayoutParams(dpToPx(176), LayoutParams.WRAP_CONTENT, 0.06f));
        ArrayAdapter<CharSequence> adapterComp = ArrayAdapter.createFromResource(
                          this, R.array.compositionTypes,
                            android.R.layout.simple_spinner_item);
        adapterComp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        compositionSelector.setAdapter(adapterComp);
        linearLayout.addView(compositionSelector);

        Button addCompoButton = new Button(this);
        addCompoButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        addCompoButton.setText(R.string.add);
        addCompoButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                addItem(v);
            }
        });
        linearLayout.addView(addCompoButton);

        LinearLayout addItemLayout = (LinearLayout) findViewById(R.id.screenAddItem);
        int index = addItemLayout.indexOfChild(findViewById(R.id.button1));
        addItemLayout.addView(linearLayout, index);
    }

    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = getBaseContext().getResources().getDisplayMetrics();
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

Upvotes: 0

Views: 507

Answers (1)

Olegas Gončarovas
Olegas Gončarovas

Reputation: 1627

Try to use LinearLayout.LayoutParams instead of TableLayout.LayoutParams

Upvotes: 1

Related Questions