I'm having a tablelayout, where columns are added dynamically. I'm unable to set width of the column/TextView dynamically.
This is my code:
TableLayout employeeTable = (TableLayout) findViewById(R.id.main_table);
TableRow header = new TableRow(this);
header.setId(100);
header.setBackgroundColor(Color.rgb(255, 165, 0));
//header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView empCodeHeader = new TextView(this);
empCodeHeader.setId(200);
empCodeHeader.setText("Employee Code");
empCodeHeader.setTextSize(16);
empCodeHeader.setPadding(2,2,2,2);
//empCodeHeader.setWidth(200);
header.addView(empCodeHeader);
TextView empNameHeader = new TextView(this);
empNameHeader.setId(201);
empNameHeader.setText("Employee Name");
empNameHeader.setTextSize(16);
empNameHeader.setPadding(2,2,2,2);
//empNameHeader.setWidth(300);
header.addView(empNameHeader);
I'm able to set using setWidth, but i want to set it dynamically, say 30% for column1 and 70% for column2.
Use LayoutParams for set height and width dynamically.
Related
TableLayout tl = new TableLayout(ActivityAddFlashcard.this);
TableRow tittleRow = new TableRow(ActivityAddFlashcard.this);
TableRow fRow = new TableRow(ActivityAddFlashcard.this);
TableRow.LayoutParams params = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
fRow.addView(getTextView("کلمه"));
fRow.addView(getTextView("معنی"));
TextView tv = new TextView(ActivityAddFlashcard.this);
tv.setLayoutParams(new TableRow.LayoutParams(tl.getWidth() // <---- //return 0
, TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("dksvlfsnfmmmmnv");
tittleRow.addView(tv);
tittleRow.setGravity(Gravity.CENTER);
tl.addView(tittleRow);
fRow.addView(new TextView(ActivityAddFlashcard.this));
tl.addView(fRow);
TableLayout.LayoutParams params2 = new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
fRow.setGravity(Gravity.CENTER);
fRow.setLayoutParams(params);
tl.setLayoutParams(params2);
tl.setGravity(Gravity.CENTER);
this is my table layout, i want to set the width of the tittle row equal to width of table.
but getWidth return 0 .
Your view can change its width time to time during layouting process. Probably you're looking for MATCH_PARENT solution:
tv.setLayoutParams(
new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT
)
);
You may be interested to setup MATCH_PARENT as layoutParameter for tittleRow as well.
I have the following in my activity which successfully displays 3 columns of data (there will be up to 10 rows, thus the for loop):
for (int i=0;i<10;i++)
{
TableRow tr = new TableRow(this);
TextView tv0 = new TextView(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
trlp.weight = 1;
tr.setLayoutParams(trlp);
tv0.setText("" + debtName[i]);
tv1.setText("" + debtAmount[i]);
tv2.setText("" + debtPayment[i]);
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
}
I want them to be evenly weighted out as there is a title row that is hard coded in to the layout. The "trlp.weight = 1;" did not change it from when it did not exist.
What I am looking for is a way to get the tv0,tv1,tv2 TextViews to be distributed evenly (horizontally).
Also for reference, I'm running at API 12.
you have to set the weight onto the children that are supposed to scale.
1 means 100% scaling by default, if you want 3 textvies to spread evenly set 0.33 as weight for each.
TableRow.layoutParams trlp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.33f);
tv0.setLayoutParams(trlp);
...
tr.add(tv0);
...
I have the following LinearLayout, into which I want to insert a number of dynamically generated TableLayouts. This runs without any errors but nothing is appearing on the screen. Why aren't the TableLayouts appearing? How can I generate the TableLayouts and add them to the LinearLayout?
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/lblOverviewText">
</LinearLayout>
This is how I'm generating the TableLayouts:
var linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
foreach (var block in status.blocks)
{
var tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FillParent, TableLayout.LayoutParams.FillParent);
var rowParams = new TableLayout.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent);
var tableLayout = new TableLayout(this);
tableLayout.LayoutParameters = tableParams;
TableRow tableRow = new TableRow(this);
tableRow.LayoutParameters =tableParams;
TextView textView = new TextView(this);
textView.Text = block.Name;
textView.LayoutParameters = rowParams;
tableRow.AddView(textView);
tableLayout.AddView(tableRow, rowParams);
linearLayout.AddView(tableLayout);
}
To start I would delete the alignParentLeft/alignParentRight in the xml for the linearlayout and simply put android:layout_width="match_parent".
You will also need to define the 'orientation' property of the linearlayout in the xml as 'vertical'.
The next part of the problem is the complicated mix of different kinds of layout params, you have correctly identified the need for table and row layout params seperatley but have defined the rowParams variable as a 'new TableLayout.LayoutParams' instead of which it should be 'new TableRow.LayoutParams' but both should have a width of match_parent and a height of wrap_content. Code example below:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
for(int i = 0; i < listItems.length; i++)
{
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(lp2);
tableLayout.setColumnStretchable(0, true);//NOTE: you may not want this if you do not want your textview to always fill the available space
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText(listItems[i]);
textView.setLayoutParams(lp);
tableRow.addView(textView);
tableLayout.addView(tableRow);
linearLayout.addView(tableLayout);
}
Hope this helps.
First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!
Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);
So far, I have code that dynamically creates my table within a horizontal scrollview. I want each item to appear in the same y location and so that you can only see one at a time. Right now, each column gets set, but the visual is like a staircase, where the next row is underneath and only moved over about 50px. Is there a way to force each column to take up the entire screen width? Also, can you set the row that the content appears in?
TableLayout tl = (TableLayout)findViewById(R.id.contentTable);
for(int i=0;i<itemIndexes.size();i++)
{
TableRow tr = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
{
ImageView iv = new ImageView(this);
iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
else
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.noimage);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
TextView tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tl.addView(tr);
}
As you have use the TableLayout and now you want each cell to consume appropriate same size throughout the table respectfully by comparing itself via row and column.
So, you have to set WEIGHT to each item and specify weight value = "1.0" or below it.