I am trying to extend the TableLayout class, such that rows will be populated automatically by the class. The issue I am having is the rows that I add inside the custom class do not display.
Ex. within the Activity:
private TextView getTableCell(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatasetTableLayout table = (DatasetTableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.addView(getTableCell("activity1"));
tr.addView(getTableCell("activity2"));
tr.addView(getTableCell("activity3"));
table.addView(tr);
This successfully adds a row to the table. However, within my custom class:
private TextView getTableCell(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
private void update() {
TableRow tr = new TableRow(context);
tr.addView(getTableCell("class1"));
tr.addView(getTableCell("class2"));
tr.addView(getTableCell("class3"));
addView(tr);
}
does not successfully add a row. Well, it does add a row, as getChildCount(), and getChildAt() do return this row - but it does not get displayed.
Am I missing something?
#theresia (Can't seem to format text if I add a comment):
It does appear that it adds the TableRow:
for(int i = 0; i < getChildCount(); i++) {
TableRow row = (TableRow) getChildAt(i);
for(int j = 0; j < row.getChildCount(); j++) {
TextView tv = (TextView) row.getChildAt(j);
Log.e("DatasetTableLayout-data", tv.getText().toString() + " ");
}
}
gives me:
01-24 11:03:31.668: ERROR/DatasetTableLayout-data(1624): activity1
01-24 11:03:31.677: ERROR/DatasetTableLayout-data(1624): activity2
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): activity3
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class1
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class2
01-24 11:03:31.727: ERROR/DatasetTableLayout-data(1624): class3
My guess would be something along this line: addView(tr); Does it successfully add your TableRow to TableLayout?
Edit:
TableLayout tb = new TableLayout(this);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("row");
tr.addView(tv);
tb.addView(tr); // this is what I mean
setContentView(tb);
Your code for creating rows works fine, but if you haven't add it to the table, and later on, add the table to the parent view as well, your rows wouldn't get displayed.
Related
I am working on a screen that basically shows questions and answers in a grid format and I am putting them all into table rows dynamically. The problem I am having is that once I put in the first row (the header), the rest of the rows in the table do not show up. I first add the headers. This shows up.
protected void setupTableHeaders() {
TableRow tableRow = getNewTableRow();
tableRow.setTag(header);
TextView textView = new TextView(this);
GlobalVars.setupText(this,textView,32320); //Event
textView.setTextColor(R.color.black);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.setMarginStart(50);
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Iterator it = mapQuestions.entrySet().iterator();
//first get all the answers
if (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
HolderQuestion holderQuestion = mapQuestions.get(pair.getKey());
//assume all answers sets for all questions on this screen are the same.
int i = 0;
for (HolderAnswer answer : holderQuestion.getListAnswers()) {
TextView textViewHeader = new TextView(this);
textViewHeader.setTextColor(R.color.black);
GlobalVars.setupText(this, textViewHeader, answer.getTextID());
tableRow.addView(textViewHeader, (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .5f)));
if (i < holderQuestion.getListAnswers().size() - 1) {
tableRow.addView(getNewDivider(), (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .1f)));
}
i++;
}
}
table1.addView(tableRow);
}
Then I add the questions and answers, which do not show up:
protected void setupRadioButtonAnswers() {
for (int i=0;i<holderQuestionMultis.size();i++) { //assume this is populated correctly
TableRow tableRow = getNewTableRow();
tableRow.setTag(answerRow);
TextView textView = new TextView(this);
textView.setTextColor(R.color.black);
GlobalVars.setupText(this,textView,holderQuestionMultis.get(i).getHolderQuestion().getTextID());
textView.setTag(textCell);
textView.setVisibility(View.VISIBLE);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.leftMargin = 50;
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
for (int j=0;j<holderQuestionMultis.get(i).getRadioButtons().size();j++) {
RadioButton radioButton = holderQuestionMultis.get(i).getRadioButtons().get(j);
radioButton.setVisibility(View.VISIBLE);
tableRow.addView(radioButton,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.5f)));
if (j< holderQuestionMultis.size()-1) {
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
}
}
tableRow.setVisibility(View.VISIBLE);
table1.addView(tableRow);
}
}
And the two methods which get the radioButtons and table rows:
// table elements
protected TableRow getNewTableRow() {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tlparams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setBackground(getResources().getDrawable(R.drawable.border_table_multi_grid));
tableRow.setLayoutParams(tlparams);
return tableRow;
}
protected RadioButton getRadioButton() {
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(getResources().getDrawable(R.drawable.radio_button_multigrid));
//radioButton.setButtonDrawable(R.color.transparent);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
radioButton.setLayoutParams(params);
return radioButton;
}
The tablelayout xml:
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_above="#+id/viewSpacer"
android:layout_below="#+id/lblSubQuestionText"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/border_table_multi_grid">
What am I missing? Is the first table header row covering up the other rows? Why do they not appear?
It turns out that this code:
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Was the culprit which consisted of adding a LinearLayout containing a view. Just adding the view alone solved the problem.
I have a custom list with name,age and grade .I want to loop through the list and show the contents in a table layout.Each row will have following attributes.Row will have some kind of styling
Name,
Age,
Age_Value,
Grade,
Grade_Value
Here Age and Grades are just a label.Given below is a sample table
Dolly
Age
23
Grade
A+
Roman
Age
22
Grade
C
Ben
Age
23
Grade
B+
I want to add these details to the Table layout in my layout dynamically.
Here is the code im trying
for(int j=0;j<_attributeList.size();j++)
{
TableRow tr = new TableRow(MyActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tvName = new TextView(MyActivity.this);
tvName.setPadding(3,0,0,0);
tvName.setText(_attributeList.get(j).getName() +" : ");
tr.addView(tvName);
TextView tvAgeL = new TextView(MyActivity.this);
tvAgeL .setText("Age");
tr.addView(tvAgeL );
TextView tvAgeValue = new TextView(MyActivity.this);
tvAgeValue .setPadding(3,0,0,0);
tvAgeValue .setText(_attributeList.get(j).getValue() +" : ");
tr.addView(tvAgeValue );
attributeTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Since these items are adding to the same row im getting results in a single line. How can i show them in seperate rows
You should try like this
Code
for(int i = 0; i < _attributeList.size();i++){
TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
newCheckBox = new CheckBox(getApplicationContext());
TextView feeTypeText = new TextView(getApplicationContext());
feeTypeText.setText(jsonObj.getString("feeType"));
feeTypeText.setTextColor(Color.BLACK);
feeTypeText.setTextSize(16f);
feeTypeText.setTypeface(tf);
TextView dueAmountText = new TextView(getApplicationContext());
dueAmountText.setText(jsonObj.getString("dueAmount"));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(16f);
dueAmountText.setTypeface(tf);
TextView dueDateText = new TextView(getApplicationContext());
dueDateText.setText(jsonObj.getString("dueDate"));
dueDateText.setTextColor(Color.BLACK);
dueDateText.setTextSize(16f);
dueDateText.setTypeface(tf);
newRow.addView(newCheckBox,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(dueAmountText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
newRow.addView(dueDateText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
attributeTable.addView(newRow);
}
you can add like this way...here i added TableRow Programatically in which i added textView.
addtree.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
tr1 = new TableRow(YourActivity.this);
tr1.setClickable(true);
tr1.setId(jj);
tr1.setPadding(0, 5, 0, 5);
Log.d("id", "id is......"+tr1.getId());
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView textview = new TextView(New_Quote_Activity.this);
textview.setText("Job "+jj);
textview.setTextColor(Color.BLACK);
textview.setTypeface(null, Typeface.BOLD);
textview.setTextSize(14);
textview.setPadding(5, 0, 0, 0);
View vv=new View(New_Quote_Activity.this);
vv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv.setBackgroundResource(R.color.viewcolor);
View vv1=new View(New_Quote_Activity.this);
vv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv1.setBackgroundResource(R.color.viewcolor);
tr1.addView(textview);
table_crete_invoice.addView(vv1);
table_crete_invoice.addView(tr1,layoutParams);
table_crete_invoice.addView(vv);
}
I have created tabs and their content from a database. Now I need help figuring out how to get the user entered values from those controls - how to get a list of the controls on the tabs (so the data can be saved). Here is a condensed sample of how I'm populating the tabs contents:
public View createTabContent(String tag)
{
SQLiteDatabase db2 = openOrCreateDatabase(msDbFile, MODE_PRIVATE, null);
Cursor cList = db2.rawQuery("SELECT * FROM CheckList WHERE CatID=" + tag, null);
TableLayout tl = new TableLayout(FormChecklist.this);
tl.setBackgroundColor(new Color().WHITE);
while(cList.moveToNext())
{
/* Create a new row to be added. */
TableRow tr = new TableRow(FormChecklist.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create the row-content. */
TextView lblQuestion = new TextView(FormChecklist.this);
lblQuestion.setText(cList.getString(2));
lblQuestion.setTag(tag);
/* Add control to row. */
tr.addView(lblQuestion);
CheckBox chkCompleted = new CheckBox(FormChecklist.this);
chkCompleted.setText("Completed");
chkCompleted.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
chkCompleted.setTag(tag);
/* Add control to row. */
tr.addView(chkCompleted);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
db2.close();
return tl;
}
No replies, but finally figured out, hopefully someone will find this info helpful.
Approach I took was modifying my createTabContent adding an ID for the TableLayout:
tl.setId(1000 + miTlCnt);
Then in my save routine I loop through controls like so:
for (int i = 1000 + miTlCnt; i > 1000; i--)
{
TableLayout tl = (TableLayout)findViewById(i);
if (tl != null)
{
for (int j = 0; j < tl.getChildCount(); j++)
{
TableRow row = (TableRow)tl.getChildAt(j);
CheckBox chkCompleted = (CheckBox)row.getChildAt(1);
}
}
}
I need to add a number of rows to my Table layout and to each row I need to add two Image Buttons. Number of rows can be different each time I start the Activity - I look into a SQLite database and for each row in the database I need to add an Image Button. And there will be two Image Buttons in each row of Table layout. So far, I have this code:
db.open();
int count = db.getCount();
boolean newRow = true;
for (int i = 0; i < count; i++) {
if (newRow == true) {
newRow = false;
row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
ImageButton button = new ImageButton(this);
row.addView(button);
tableLayout.addView(row);
}
db.close();
TableRow row is defined above this block of code simply as variable for this Activity. My Table layout (tableLayout in code) is defined in XML code of the Activity layout:
<TableLayout
android:id="#+id/tableLayoutContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
The code crashes at the line
tableLayout.addView(row);
I wasnt able to resolve this. Any ideas? Thanks!!
I think the problem is different
It seems that Your code will have only one row and you are adding that row again and again to tableLayout as shown below.
tableLayout.addView(row);
This will lead to IllegalStateException at addViewInner Function of ViewGroup
try this
db.open();
int count = db.getCount();
boolean newRow = false;
for (int i = 0; i < count; i++) {
if (i % 2 == 0)
newRow = true;
if (newRow == true) {
newRow = false;
row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tableLayout.addView(row);
}
ImageButton button = new ImageButton(this);
row.addView(button);
}
db.close();
I am trying to add a tableLayout at runtime to the existing LinearLayout in main.xml.
I have added a editText(R.id.editText1) in the main.xml. Here's my code. Its not working. I get a runtime error (The application has stopped unexpectedly).
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = (EditText)findViewById(R.id.editText1);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setLayoutParams(new TableLayout.LayoutParams(8,5));
tblLayout.setPadding(1,1,1,1);
for(int r=0; r<ROW_COUNT; ++r)
{
TableRow tr = new TableRow(this);
for(int c=0; c<COL_COUNT; ++c)
{
int index = r * COL_COUNT + c;
buttonList.add(new Button(this));
buttonList.get(index).setText(buttonNames[index]);
tr.addView(buttonList.get(index), 60, 30);
}
tblLayout.addView(tr);
}
LinearLayout mainLayout = (LinearLayout)findViewById(R.layout.main);
mainLayout.addView(tblLayout);
setContentView(mainLayout);
}
Any pointers would be greatly appreciated. Thanks.
Before your loop add :
Arraylist<TableRow> tr = new ArrayList<TableRow>;
In the loop instead of :
TableRow tr = new TableRow(this);
Put :
tr.add(new TableRow(this));
And finally replace your tr by tr.get(r). Something like these modifications should help you because, you erase your tr at each turn of your loop with your :
TableRow tr = new TableRow(this);
Mr. Happy Go Lucky just do one thing write setContentView(R.layout.your Main Layout); beofre text = (EditText)findViewById(R.id.editText1);
means at top of the onCreate() method because we cant define any view before setContentView.
without any setContentView you cant find any view like as findViewById(R.id.editText1);
Below line will be the first one after super.onCreate(savedInstanceState);
setContentView(mainLayout);
then only findViewByid works