androidandroid-edittexttablerowtablecell

How do I fit an EditText to a Table Cell in Java during dynamic programming?


Problem: I'm having difficulty getting an EditText field to fit within a table cell dimensions.

What I've tried: I have tried a number of suggestions, like below in my code, stated here on StackOverflow but nearly all are static and not dynamic examples. I looked at a couple videos which were about the same.

What I'm trying to do: I'm trying to give the user an option to add a row of data to a table and fill in the fields. The following code works out well except I cannot seem to find the correct way of stating the LayoutParams to fit the EditText within the cell boundaries.

for(int r=1; r<5;r++) {
   EditText editText = new EditText(this);
   editText.setBackgroundColor(Color.WHITE);
   editText.setTextSize(12);
   editText.setSingleLine();
   editText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
   row.addView(editText);
}

Table Cell EditText

<TableLayout
      android:id="@+id/table_authors"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:background="@color/colorDkGray"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toBottomOf="@id/btn_add_author"
      android:layout_marginTop="15dp"
      android:layout_marginEnd="5dp"
      android:layout_marginStart="5dp"/>

The code to setup table in the EditNote activity:

tableLayoutAuthors = findViewById(R.id.table_authors);
        tableLayoutAuthors.addView(setupAuthorsTableRow("+", "Organization/First", "Middle Name", "Last Name","Suffix", true));

btnAddAuthor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tableLayoutAuthors.addView(setupAuthorsTableRow("-","", "", "", "", false));
            }
        });

Solution

  • With the help of Mayur Gajra, I was able to figure out how to fix my issue. Posted below is what fixed my issue:

    Adding this line to the XML:

    android:stretchColumns="*"
    

    Changing to creating an instance of TableRow.LayoutParams and setting the margins and setting the padding for the editText:

        for(int r=1; r<5;r++) {
              EditText editText = new EditText(this);
     // Change:   
              TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams();
              trLayoutParams.setMargins(3,3,3,3);
              editText.setLayoutParams(trLayoutParams);
        
              editText.setBackgroundColor(Color.WHITE);
              editText.setTextSize(12);
              editText.setGravity(Gravity.CENTER);
              editText.setSingleLine();
    
       // Change: 
              editText.setPadding(8, 8, 8, 8);
        
              row.addView(editText);
              row.setClickable(true);
          }
    

    Corrected Table Row with EditText