Im trying to create two table layouts through activity..
I already have one table layout but how to set through activity?
I know to do it through xml but want to do it programatically..
Please Help
Check this answer and another example here
Just like in xml, you will create a TableLayout, provide params and add rows with your own UI in it.
Take one linear layout(or relative layout) in in your xml get it reference by findViewById() in onCreate() method of your activity.after that create table dynamically and add it to the linear layout.I create a method to do so . ex-
LinearLayout linear= (LinearLayout ) findViewById(R.id.linear);
//call method to add the tablelayout.
linear.addView(createtable(3,5));
private TableLayout createtable(int requiredcolumn, int requiredrow) {
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1f);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
//for border
rowParams.setMargins(2, 2, 2, 2);
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(Color.WHITE);
for (int row = 0; row < requiredrow; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < requiredcolumn; column++) {
Random color = new Random();
int randomColor = Color.argb(255, color.nextInt(256),
color.nextInt(256), color.nextInt(256));
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}
Related
I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.
I have tablelayout with rows and in each row a bunch of textviews (all added programatically, no xml), I want to have gridlines so I am trying to add margins through laoyoutparams but it looks like TableLayout.LayoutParams doesn't apply to textviews only to rows so I am getting a margin on the rows but not on the cells. I have also tried using RelativeLaout.LayoutParams on the cells but thaqt is not working also. Anyone can propose any solution for my problem?
You should use correct LayoutParams for views.
While adding a view to:
TableLayout; use TableLayout.LayoutParams
TableRow; use TableRow.LayoutParams.
Edit: I've edited the code. You want some black spaces for some cells. You can achive this with setting same bg color to that views(or transparent bg for row and views on cell). I think setting same bg color to textViews with tableLayout is easier.
Here is a complete code sample that you can try(colors are added to see output easily):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = createTableLayout(16, 14);
setContentView(tableLayout);
makeCellEmpty(tableLayout, 1, 6);
makeCellEmpty(tableLayout, 2, 3);
makeCellEmpty(tableLayout, 3, 8);
makeCellEmpty(tableLayout, 3, 2);
makeCellEmpty(tableLayout, 4, 8);
makeCellEmpty(tableLayout, 6, 9);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
// make it black
textView.setBackgroundColor(Color.BLACK);
}
private TableLayout createTableLayout(int rowCount, int columnCount) {
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j= 0; j < columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
And here is the output for this code that we can see margins are applied correctly:
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);
In My application i want to create the layout as like below image:
So How to make it possible ?
I have done Something like below code:
public void showResult()
{
List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
for(int i = 0; i<=tempEmployerList.size()-1; i++)
{
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
innerLayout.setBackgroundColor(0x00000000);
// set the Multiple TextView
TextView mHeading = new TextView(getApplicationContext());
TextView middleValue = new TextView(getApplicationContext());
TextView aImageView = new TextView(getApplicationContext());
mHeading.setText("\n"+"1");
//mHeading.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mHeading.setTextColor(0xFFFF0000);
mHeading.setPadding(3, 0, 0, 0);
middleValue.setText("\n"+"2");
//middleValue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
middleValue.setTextColor(0xFFFF0000);
middleValue.setGravity(Gravity.RIGHT);
middleValue.setPadding(2, 0, 9, 0);
aImageView.setText("\n"+"3");
//aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
aImageView.setGravity(Gravity.RIGHT);
aImageView.setTextColor(0xFF000000);
aImageView.setPadding(0, 0, 9, 0);
View line = new View(getApplicationContext());
//line.setOrientation(1);
line.setLayoutParams(new LayoutParams(2, android.view.ViewGroup.LayoutParams.FILL_PARENT));
line.setBackgroundColor(0xFF000000);
/**** Any other text view setup code ****/
innerLayout.addView(mHeading);
innerLayout.addView(middleValue);
innerLayout.addView(aImageView);
innerLayout.addView(line);
myLinearLayout.addView(innerLayout);
textListWord.add(mHeading);
textListAnswer.add(middleValue);
imageListAnswer.add(aImageView);
}
}
So please guide me that what more i have to do to create such view ?
Thanks.
try TableLayout and TableRow instead of LinearLayout.
You can create TableLayout in xml as well, as it, I think would be static and add TableRows. To Create a TableLayout in Java use,
TableLayout tblLayout=new TableLayout(this);
Set LayoutParams and other properties of table layout in java, like I am setting LayoutParams:
LayoutParams params=new LayoutParams(LayoutParams.Fill_Parent, LayoutParams.Wrap_Content);
tblLayout.setLayoutParams(params);
Create a loop for TableRows creation and insertion:
for(int i=0;i<arrList1.size();i++)
{
TableRow row=new TableRow(this);
row.setLayoutParams(params);
TextView lbl1=new TextView(this);
lbl1.setText(arrList1.get(i));
row.addView(lbl1);
TextView lbl2=new TextView(this);
lbl2.setText(arrList2.get(i));
row.addView(lbl2);
TextView lbl3=new TextView(this);
lbl3.setText(arrList3.get(i));
row.addView(lbl3);
tblLayout.addView(row);
}
I've read this article:
Setting width of Child View using Percentage
So far it works great if we set the weight and width in XML but I want to do it programmatically. I've a table in my layout (XML) and I am adding rows to it programmatically. In each row, I am adding 2 Text Views. I tried doing like this:
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i = 0; i < nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
TableRow trDetails = new TableRow(this);
trDetails.setBackgroundColor(Color.BLACK);
TextView tvInfo = new TextView(this);
tvInfo.setTextColor(Color.WHITE);
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(3, 3, 3, 3);
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvInfo.setText("info");
TextView tvDetails = new TextView(this);
tvDetails.setTextColor(Color.WHITE);
tvDetails.setGravity(Gravity.LEFT);
tvDetails.setPadding(3, 3, 3, 3);
tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvDetails.setText(np.getNamedItem("d").getNodeValue());
trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
tl.addView(trDetails,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
but with no luck, it shows blank screen but if I do it in XML then it shows perfectly. Please help me sorting out this issue where I am doing wrong?
Thanks
You should use TableRow.LayoutParams and not Linearlayout.LayoutParams
trDetails.addView(tvInfo, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));
trDetails.addView(tvDetails, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));