How to set layout fields for editing text fields?

In the table layout, I have tablerow, and in this tablerow I have 6 text fields, and I want to set the layout fields for these 6 text edit fields

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); 
+6
java android ellipse
source share
2 answers

first of all, something you should know: according to the official Android Dev pages , views (and TextView obtained from the view) do not support Margin settings, but ViewGroups (e.g. LinearLayout , RelativeLayout , etc.)).

So what you can 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 this and it worked for me (albeit with a LinearLayout with vertical alignment). Give him a chance, and let me know if I can help further :).

Greetings

Ready4Fajir

+8
source share

EDIT:
I would try with the XML below (you, of course, update the identifier, etc.). The "magic" in xml is that it evenly distributes the entire available width among the TextView (and the EditText in the second line).

 <?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 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 accordingly --> </LinearLayout> </LinearLayout> 

In your Java code, you can access your EditText views, for example:

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

Now I have ignored the fact that you need to create your EditText programmatically. Do you really, really have to create them in Java? (Why?)

OLD ANSWER:
If you just want to set the layout fields in your EditText view, I can say that you can use the setMargins(left, top, right, bottom) function call in the setMargins(left, top, right, bottom) 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 want to evenly distribute all available space among six EditText views in a row of a table, I would suggest you look at the following message: 2-column TableLayout with 50% for each column exactly

+4
source share

All Articles