I have a table layout for a form that has two columns - a field name, and a field value. In some cases the field value is a checklist. I'd like the row height of the "checklist" row to expand so I can see all of the items in the listview.
The following code shows the listview in a tiny vertical scrollable view, the same height as the field name in the first column. How do I expand the row height so I can see the entire list? (The FrameLayout in the table cell is so I can overlay an icon or text).
// get the table
TableLayout table = FindViewById<TableLayout>(Resource.Id.formTable);
// make a new table row
TableRow row = new TableRow(this);
// add the field name (left column)
TextView rowName = new TextView(this);
rowName.Text = field.Name + ":";
rowName.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
TableRow.LayoutParams tableLayoutParams = new TableRow.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.FillParent);
tableLayoutParams.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
tableLayoutParams.Weight = 1.0f;
tableLayoutParams.Width = 0;
row.AddView(rowName, tableLayoutParams);
// add the field value (right column - in this case, a listview for a checklist)
FrameLayout frameLayout = new FrameLayout(this);
List<string> strings = field.Items.ToList();
ListView listView = new ListView(this);
listView.Adapter = new ListViewFormAdapterCheckList(this, strings);
frameLayout.AddView(listView);
tableLayoutParams = new TableRow.LayoutParams(TableLayout.LayoutParams.WrapContent, TableLayout.LayoutParams.WrapContent);
tableLayoutParams.SetMargins(0, 10, 0, 0);
tableLayoutParams.Width = 0;
row.AddView(frameLayout, tableLayoutParams);
Never got this working with the listview. Used a linearlayout instead...
// get the table
TableLayout table = FindViewById<TableLayout>(Resource.Id.formTable);
// make a new table row
TableRow row = new TableRow(this);
// put the field name at the top of the row
rowName.Gravity = GravityFlags.Right | GravityFlags.Top;
rowName.SetPadding(0, 20, 20, 0);
// frame layout so we can overlay an image / icon over the list
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.Id = kCurrentFieldId++;
frameLayout.Tag = index;
mFieldToView[field] = frameLayout;
List<string> strings = field.Items.ToList();
LinearLayout listLayout = new LinearLayout(this);
listLayout.Orientation = Android.Widget.Orientation.Vertical;
LayoutInflater inflater = (LayoutInflater) GetSystemService(Context.LayoutInflaterService);
foreach (string item in strings)
{
View v = inflater.Inflate(Resource.Layout.list_checklist, null);
v.FindViewById<CheckBox>(Resource.Id.checkbox).Text = item;
listLayout.AddView(v);
}
frameLayout.AddView(listLayout);
tableLayoutParams = new TableRow.LayoutParams(TableLayout.LayoutParams.WrapContent, TableLayout.LayoutParams.WrapContent);
tableLayoutParams.SetMargins(0, 10, 0, 0);
tableLayoutParams.Width = 0;
tableLayoutParams.Weight = 2.0f;
row.AddView(frameLayout, tableLayoutParams);
list_checklist defined as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dip">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="checkdesc" />
</RelativeLayout>
Related
i create 1 LinearLayout inside LinearLayout, that contains 1 EditText and 1 TextView programmatically and i need to get text from EditText and TextView. And this is my code:
main.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<LinearLayout
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
MainActivity.java:
linearLayout = (LinearLayout) view.findViewById(R.id.result);
.......
public void addItem(){
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
EditText quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
Help me, please.
I've edit my question
Actually, my case above happened when i clicked addItem(), my app will display new TextView and EditText, so if this process is finished, and move to the next Fragment to display all the text of TextViews and EditTexts like this using StringBuilder:
Items Qtt
Cola......1
Chicken...1
Meat......2
Help me.
Do you want to get like this?
String str = item.getText().toString();
String str2 = quantity.getText().toString();
As stated above, using item.getText().toString(); is all you need, nonetheless I do presume your problem lies in the fact that you are declaring your Views inside the loop, by doing it locally later anywhere on the code you won't be getting any text at all.
Try declaring your Views as global variables or doing it outside of the loop such as;
//....
TextView item;
EditText quantity;
for(int i = 0; i < cartCount; i++) {
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
On the other hand, by looking at your code, are you trying to create a list of EditText and TextViews? If so, it would be wise to consider using a ListView with custom items; Here you can find a nice tutorial doing something simmilar.
This feels like you should be looking at an adapter view such as a recyclerview. You might start to see some performance issues using a linearlayout.
Anyway. You could add the views to an list as you add them to the linearlayout.
List<TextView> items = new LinkedList<>();
List<EditText> quantities = new LinkedList<>();
for(int i = 0; i < cartCount; i++) {
...
childLayout.addView(item);
childLayout.addView(quantity);
items.add(item);
quantities.add(quantity);
linearLayout.addView(childLayout);
}
then loop through your views
for (TextView textView : items) {
}
for (EditText editText : quantities) {
}
item.getText().toString()
quantity.getText().toString()
I currently have my code that is set up to show one TextView in the parent layout and 3 textviews in the same layout by row. My problem is that the 3 textviews are displaying as 3 different rows when I would like to be set up in the same row as a group. I am thinking I need a child linerlayout but this has not worked for me. The 3 textviews are dynamic and I can't set a certain amount of textviews as the data coming in is dynamic. Here is an example of my code of how my code will display....
Screenshot of 3 textviews in different rows
My code is the following:
//initialize linearlayout
public LinearLayout pickcontainerLayout;
int counter = 0;
//creating a new view by using functions, using keys and values from multimap
for (String key : myMultimap.keySet()) {
ArrayList<SaleOrder> sale = new ArrayList<>();
for (SaleOrder s : myMultimap.get(key)) {
sale.add(s);
}
pickcontainerLayout.addView(containerLayoutfunction(counter, sale.size(), sale, key));
View line = new View(getContext());
line.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1));
line.setBackgroundColor(Color.rgb(255,255,255));
pickcontainerLayout.addView(line);
counter++;
}
private TextView newSku(int id, String sku) {
TextView skuView = new TextView(getContext());
skuView.setId(id);
skuView.setText("SKU: " + sku);
/*LinearLayout.LayoutParams skuParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
skuView.setLayoutParams(skuParams);*/
skuView.setGravity(Gravity.LEFT);
return skuView;
}
private TextView newqty(int id, String qty) {
TextView qtyView = new TextView(getContext());
qtyView.setId(id);
qtyView.setText("QTY: " + qty);
/*LinearLayout.LayoutParams qtyParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
qtyView.setLayoutParams(qtyParams);*/
qtyView.setGravity(Gravity.CENTER);
return qtyView;
}
private TextView newlocation(int id, String location) {
TextView loc = new TextView(getContext());
loc.setId(id);
loc.setText("Location: " + location);
/*LinearLayout.LayoutParams locParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
loc.setLayoutParams(locParams);*/
loc.setGravity(Gravity.RIGHT);
return loc;
}
private LinearLayout containerLayoutfunction(int parentid, int rowId, ArrayList<SaleOrder> s, String key) {
LinearLayout layout = new LinearLayout(getContext());
layout.addView(newSale(parentid, key));
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i<rowId;i++) {
//LinearLayout layoutchild = new LinearLayout(getContext());
//layoutchild.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//layoutchild.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(newSku(rowId,s.get(i).getSku()));
layout.addView(newqty(rowId,s.get(i).getQty()));
layout.addView(newlocation(rowId,s.get(i).getlocation()));
}
return layout;
}
you should wrap the three TextViews in a LinearLayout with horizontal orientation, so the textviews will automatically be on the same row. The container will have FILL_PARENT horizontally as LayuoutParam, while the textview should have WRAP_CONTENT with gravity assigned to LEFT (for the first), CENTER (for the second) and RIGHT (for the third).
For example I create 4 tablerows programatically and I want each tablerow height to be 1/4 size of the screen. Each tablerow height is half the screen size. I tried different ways, but tablerow height didn't change. I manage change tablerow width, but height doesn't.
Here is my code :
#SuppressLint("NewApi")
public class GameActivity extends Activity {
private Context context;
public void drawTable(){
int i;
TableLayout table = new TableLayout(context);
table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
//table.setStretchAllColumns(true);
//table.setShrinkAllColumns(true);
//table.setWeightSum(1.0F);
TableRow[] rows = new TableRow[5];
for(i = 0; i < 4; ++i){
rows[i] = new TableRow(context);
rows[i].setBackgroundResource(R.drawable.images214);
rows[i].setWeightSum((float)1.0);
rows[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 10));
//rows[i].setScaleY(0.25F);
table.addView(rows[i]);
}
LinearLayout l = (LinearLayout) findViewById(R.id.linear);
l.addView(table);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
context = this;
// TableLayout table = new TableLayout(this);
// TableRow row1 = new TableRow(this);
// TableRow row2 = new TableRow(this);
// row1.addView(new EditText(this));
// row2.addView(new Button(this));
// table.addView(row1);
// table.addView(row2);
// //table.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
// table.setBackgroundResource(R.drawable.ic_launcher);
// LinearLayout l = (LinearLayout) findViewById(R.id.linear);
// l.addView(table);
drawTable();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_game, menu);
return true;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_weight="1">
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical|horizontal">
</LinearLayout>
</ScrollView>
</RelativeLayout>
You're not using the proper LayoutParams, for the table TableLayoutyou should set LinearLayout.LayoutParams(as you add the table to a LinearLayout) and for your TableRows the LayoutParams should be TableLayout.LayoutParams(as they are children of TableLayout). Even with the above changes things will not work because the TableLayout is a widget that imposes constraints on its children, most notable the height will be automatically set to WRAP_CONTENT for TableRows. The only way to change this is to use weight like this:
TableLayout table = new TableLayout(context);
table.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
table.setWeightSum(1.0f);
TableRow[] rows = new TableRow[4];
for(int i = 0; i < 4; ++i){
rows[i] = new TableRow(context);
rows[i].setBackgroundResource(R.drawable.images214);
rows[i].setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0, 0.25f));
table.addView(rows[i]);
}
LinearLayout l = (LinearLayout) findViewById(R.id.linear);
l.addView(table);
But this will not work, because your TableLayout is set to fill the parent, parent which is a ScrollView which doesn't imposes dimensions on its child. I don't know what you're trying to do with that ScrollView there(especially as you want the table to fill the entire screen) so I don't know what to recommend you. Also, I don't know your full layout but your nesting to many layouts.
For each table row set height:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int height = metrics.heightPixels / 4; // quarter of screen height
rows[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, height));
I am simply trying to add a separator line under my header row in a table layout, but no matter what i set the width/height to in layoutparams, the view takes up the whole page.
TableLayout tl = (TableLayout)FindViewById(Resource.Id.counted);
TableRow row = new TableRow(this);
TextView itemNumber = new TextView(this);
TextView itemDesc = new TextView(this);
TextView quantity = new TextView(this);
itemNumber.Text = "Item Number";
var inlo = new TableRow.LayoutParams(1);
inlo.SetMargins(10, 10, 30, 10);
itemNumber.LayoutParameters = inlo;
itemDesc.Text = "Item Description";
var idlo = new TableRow.LayoutParams(2);
idlo.SetMargins(10, 10, 30, 10);
itemDesc.LayoutParameters = inlo;
quantity.Text = "Quantity";
var qlo = new TableRow.LayoutParams(3);
qlo.SetMargins(10, 10, 30, 10);
quantity.LayoutParameters = qlo;
View v = new View(this);
v.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent | 0);
v.SetBackgroundColor(Android.Graphics.Color.White);
row.AddView(itemNumber);
row.AddView(itemDesc);
row.AddView(quantity);
tl.AddView(row);
tl.AddView(v);
v and its white background takes up the whole screen below row. ????
Just decided to use Strings to underline my column headers rather than trying to put a line under the first row.
How should I implement a table of 15 by 15 cells if I plan to dynamically update the cells individually?
I began creating the table in my main.xml, but I don't want to describe the table with 15 TableRow elements and 15 elements in each of those all in one body of code. I feel like even putting just the table itself in a separate file is too bulky. It is possible to create a file of a row and and a file of a table including the row 15 times and dynamically assign different IDs to each cell?
You could do comething like:
public void generateTable() {
TableLayout tv = new TableLayout(context);
for (int y = 0; y < 15; y++) {
TableRow tr = new TableRow(context);
for (int x = 0; x < 15; x++) {
TextView tv = new TextView(context);
tr.addView(tv);
tv.setText("ID: " + (y*15 + x));
tv.setId(y*15 + x);
// And to allow for easy reading
if (y*15+x % 2 == 0)
tv.setBackgroundColor(0xffff0000);
else
tv.setBackgroundColor(0xff0000ff);
}
}
}
Create a table layout like this from xml
TableLayout tl = (TableLayout) findViewById(R.id.content_table);
You can create a table row in xml as tablerow.xml.This will help you to define table row layout in an easier way.I am adding only 1 TextView.
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/content"
android:singleLine="false"
/>
LayoutInflater inflater = getLayoutInflater();
for (int rowID = 0; rowID < 15; rowID++) {
TableRow row = (TableRow)inflater.inflate(R.id.tablerow, tl, false);
TextView content = (TextView)row.findViewById(R.id.content);
content.setId(rowID);
content.setText("this is the content");
tl.addView(row);
}