Home » Android » java – How to set the layout margins of edit text boxes?

java – How to set the layout margins of edit text boxes?

Posted by: admin June 15, 2020 Leave a comment

Questions:

In the table layout i have a tablerow and in that tablerow i have 6 edit text boxes and i want to set the layout margins for that 6 edit text boxes

TableLayout t1=(TableLayout)findViewById(R.id.table_layout01);

  TableRow tr1=new TableRow(inventory.this);
  tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


  tr1.setBackgroundColor(Color.BLACK);
  EditText ed6=new EditText(inventory.this);
  //ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  /*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
  editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/


  ed6.setTextColor(Color.BLACK);
  ed6.setBackgroundColor(Color.WHITE);


  ed6.setText("1");
        tr1.addView(ed6);



  EditText ed7=new EditText(inventory.this);
  //ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed7.setTextColor(Color.BLACK);
  ed7.setBackgroundColor(Color.WHITE);
  ed7.setText("2");

  tr1.addView(ed7);

  EditText ed8=new EditText(inventory.this);
  //ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed8.setTextColor(Color.BLACK);
  ed8.setBackgroundColor(Color.WHITE);
  ed8.setText("3");

  tr1.addView(ed8);

  EditText ed9=new EditText(inventory.this);
  //ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed9.setTextColor(Color.BLACK);
  ed9.setBackgroundColor(Color.WHITE);
  ed9.setText("4");

  tr1.addView(ed9);

  EditText ed10=new EditText(inventory.this);
  //ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed10.setTextColor(Color.BLACK);
  ed10.setText("5");
  ed10.setBackgroundColor(Color.WHITE);

  tr1.addView(ed10);

  EditText ed11=new EditText(inventory.this);
  //ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  ed11.setTextColor(Color.BLACK);
  ed11.setText("6");
  ed11.setBackgroundColor(Color.WHITE);

  tr1.addView(ed11);

  t1.addView(tr1);
How to&Answers:

first of all something you should know: According to the Official Android Dev Pages, Views (and a TextView derives from View) do not support the setting of Margin, but ViewGroups (such as LinearLayout, RelativeLayout etc…) do.

So what you could do is the following:

TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);

This would set the margin for all children to 5 pixels – I tried it and it worked for me (albeit with a LinearLayout with vertical alignment). Give it a shot and let me know if I can help further 🙂 .

Cheers,

Ready4Fajir

Answer:

EDIT:
I would try with the XML below (you’d, of course, update the id’s etc.). The “magic” in the xml is that it distributes all available width evenly among the TextView‘s (and the EditText‘s on the second row).

<?xml version="1.0" encoding="utf-8"?>

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

    <!-- The first "row" -->    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView 01"
            android:id="@+id/textView01" />

        <!-- Here you'd add your other five TextView's accordingly -->

    </LinearLayout>

    <!-- The second "row" -->    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView 01"
            android:id="@+id/editText01" />

        <!-- Here you'd add your other five EditText's accordingly -->

    </LinearLayout>
</LinearLayout>

In your Java code you could then access your EditText views like:

EditText editText01 = (EditText) findViewById(R.id.editText01);
editText01.setText("1");

I have now ignored the fact that you need to create your EditText’s programatically. Do you really, really need to create them in Java? (Why?)

OLD ANSWER:
If you just want to set the layout margins to your EditText view i quess you could use the setMargins(left, top, right, bottom) function call on the LayoutParams variable.

int left = 6;
int top = 12;
int right = 6;
int bottom = 6;

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);

EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);

If you ultimately wish to distribute all available space evenly among the six EditText views in a table row I would suggest you have a look at the following post: 2-column TableLayout with 50% exactly for each column