Jacob Moulton Rhodes
Jacob Moulton Rhodes

Reputation: 93

Programatically set layout_width of EditText in Android

So I have an application that dynamically adds a table row every time the "Add Row" button is pressed. The code attached is the code that runs when "Add Row" is pressed. What I am trying to do is have 3 EditTexts next to each other on the same row. In order to do that, I need to change the Layout_Width of each EditText so the first EditText doesn't cut the other two off the screen. I can't seem to figure out a way to properly do this and was wondering if anyone would help me out. After I figure out how to do that, the next step is to adjust the layout_width according to the screen size but thats later down the road. It needs to be done programmatically because a user can theoretically have as many rows as they want to.

private OnClickListener btnListener = new OnClickListener()
    {

        public void onClick(View v)
        {   

            tablerow = new TableRow(getActivity());

            ed1 = new EditText(getActivity());
            ed1.setInputType(InputType.TYPE_CLASS_TEXT);

            ed2 = new EditText(getActivity());
            ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

            ed3 = new EditText(getActivity());
            ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

            tablerow.addView(ed1);
            tablerow.addView(ed2);
            tablerow.addView(ed3);
            table1.addView(tablerow);

        } 

    };  

Upvotes: 0

Views: 3314

Answers (2)

gio
gio

Reputation: 5020

You can try next approach, it is based on xml. You will be able to play around with layout at table_row.xml.

   public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.a_mn_button_add_row:
                onClickButtonAddRow();
                break;
        }
    }

    private void onClickButtonAddRow() {
        TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table);
        tableLayout.addView(View.inflate(this, R.layout.table_row, null));
    }
}

activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/a_mn_button_add_row"
        android:onClick="onClick"
        android:text="Add row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableLayout
            android:id="@+id/a_mn_table"
            android:stretchColumns="0,1,2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

table_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <EditText
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</TableRow>

table_row.xml for use case with 3-1-2 proportions.

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <EditText
        android:layout_weight="1"
        android:inputType="text"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_weight="2"
        android:inputType="text"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

</TableRow>

Upvotes: 0

Robert Nekic
Robert Nekic

Reputation: 3107

I believe what you are looking for is LayoutParams and its "weight" value.

Try:

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);

ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed1.setLayoutParams(params);

The third value (1.0f) in the LayoutParams constructor is a weight...all EditTexts in that TableRow should end up the same width.

Upvotes: 2

Related Questions