I have a complicated table, so I would like to make one row a little more flexible for adding a button or text view, so I have the following code for adding TableRow to Table. I am trying to set LinearLayout to TableRow programmatically, but the row does not show up when I run it. I'd appreciated if somebody can point what the problem is.
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final CheckBox checkbox = new CheckBox(this);
checkbox.setPadding(10, 5, 0, 0);
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item_clicked_on = (String) ((TextView) view).getText();
Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show();
}
});
TextView tv = new TextView(this);
tv.setText(" " + count + ". " + baby.getName());
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14);
tv.setPadding(10, 10, 0, 0);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item_clicked_on = (String) ((TextView) view).getText();
Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show();
}
});
checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(tr.getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tableRow.addView(linearLayout);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I am trying to make it look like this xml.
<TableRow android:id="#+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<CheckBox android:id="#+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choice 1" android:textColor="#FFFFFF"></TextView>
</LinearLayout>
</TableRow>
You are adding linear layout to tableRow .
tableRow.addView(linearLayout);
But you are adding table row tr to Table table
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I think you have to add tableRow to table. Once check it.
Related
I am currently working on an android app where I select a customer in an activity, it retrieves all records pertaining to that customer and displays them in a table layout in a new activity.
The problem arises when I go back to the previous activity and select a different customer, the new records are added behind the records of the previous customer (meaning the previous table layout entries are not deleted when I press the back button and go to select a new customer).
I've tried 2 solutions:
tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();
and
layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));
Both these solutions do not work for me as the new data is still being added behind the existing data.
Could anyone suggest me a different solution that could work?
Also I am attaching the entire code.
OutstandingBills.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_outstanding_bills);
layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));
addHeaders();
addData();
}
public void addHeaders(){
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView bilDateTV = new TextView(OutstandingBills.this);
bilDateTV.setText("Date");
bilDateTV.setTextColor(Color.BLACK);
bilDateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
bilDateTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
bilDateTV.setPadding(5, 5, 5, 0);
tr.addView(bilDateTV); // Adding textView to tablerow.
TextView billNoTV = new TextView(OutstandingBills.this);
billNoTV.setText("Bill No.");
billNoTV.setTextColor(Color.BLACK);
billNoTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
billNoTV.setPadding(5, 5, 5, 0);
billNoTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(billNoTV); // Adding textView to tablerow.
TextView dueAmtTV = new TextView(OutstandingBills.this);
dueAmtTV.setText("Amount Due");
dueAmtTV.setTextColor(Color.BLACK);
dueAmtTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
dueAmtTV.setPadding(5, 5, 5, 0);
dueAmtTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(dueAmtTV); // Adding textView to tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView divider = new TextView(OutstandingBills.this);
divider.setText("-----------------");
divider.setTextColor(Color.BLACK);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider); // Adding textView to tablerow.
TextView divider2 = new TextView(OutstandingBills.this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.BLACK);
divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider2); // Adding textView to tablerow.
TextView divider3 = new TextView(OutstandingBills.this);
divider3.setText("-------------------------");
divider3.setTextColor(Color.BLACK);
divider3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
divider3.setPadding(5, 0, 0, 0);
divider3.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider3); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
public void addData(){
for (int i = 0; i < arr.size(); i++)
{
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
bilDate = new TextView(OutstandingBills.this);
bilDate.setText(arr.get(i).toString());
bilDate.setTextColor(Color.BLACK);
bilDate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
bilDate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bilDate.setPadding(5, 5, 5, 5);
tr.addView(bilDate); // Adding textView to tablerow.
bilNo = new TextView(OutstandingBills.this);
bilNo.setText(arr1.get(i).toString());
bilNo.setTextColor(Color.BLACK);
bilNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bilNo.setPadding(5, 5, 5, 5);
bilNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(bilNo); // Adding textView to tablerow.
dueAmt = new TextView(OutstandingBills.this);
dueAmt.setText(arr2.get(i).toString());
dueAmt.setTextColor(Color.BLACK);
dueAmt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
dueAmt.setPadding(5, 5, 5, 5);
dueAmt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(dueAmt); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
OutstandingBills.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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/layout1"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ashwin.projectx1.OutstandingBills">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
</RelativeLayout>
Implementing # Mr.Neo 's solution:
This is how I implemented your solution in my code:
addHeaders();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Add view for table layout here
addData();
}
}, 2000);
But there is still no effect on the TableLayout
Screenshots:
The first time I enter the activity with a Customer name
On re-entering the activity the second time, you can see the duplicate records
Just try using tl.removeAllViews() instead of tl.removeAllViewsInLayout().
Call this method before adding views in tablelayout.
Here is the solution I am using. Using removeAllViews() and add view after seconds to completely removing. If you add immediately, some rows could not be removed. I know this is not the best solution, but it's the best for me now.
tableMainContent.removeAllViews();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Add view for table layout here
}
}, 2000);
I am trying to dynamically create a View to act as a spacer between table rows however my view is only going as wide as the text even though I am trying to have them match parent.
I am dynamically adding to the TableLayout with
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
tr.setOrientation(LinearLayout.VERTICAL);
tr.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
layout.setPadding(15, 15, 10, 15);
TextView description = new TextView(this);
description.setText(row.getString(TAG_DESCRIPTION));
description.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
description.setTextSize(25);
TextView dateCreated = new TextView(this);
dateCreated.setText("Date Created: " + row.getString(TAG_DATEADDED).replace("\\", ""));
dateCreated.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
dateCreated.setTextSize(20);
TextView reportType = new TextView(this);
reportType.setText("Report Type: " + row.getString(TAG_REPTYPE));
reportType.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
reportType.setTextSize(20);
View view = new View(this);
view.setMinimumHeight(2);
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
layout.addView(description);
layout.addView(dateCreated);
layout.addView(reportType);
layout.addView(view);
tr.addView(layout);
existingReports.addView(tr);
and I define my TableLayout in my XML with
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt_numExistingReports"
android:layout_alignStart="#+id/txt_numExistingReports"
android:layout_below="#+id/txt_numExistingReports"
android:layout_alignParentBottom="true"
android:background="#android:color/white"
android:layout_toLeftOf="#+id/home"
android:layout_toStartOf="#+id/home">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:showDividers="middle"
android:id="#+id/existingReports"
android:layout_marginTop="5dp"
android:divider="?android:attr/dividerHorizontal"
android:background="#android:color/white"/>
</ScrollView>
The scrollview width is Wrap Content...try with Match Parent
I have a problem in Android creating a table adding rows dynamically. The error message is:
The specified child already has a parent. You must call removeView()
on the child's parent first
But why?
void setCalendario(List<ArrayList<String>> l){
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
the xml code:
<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"
tools:context="com.MSca.gorhinos.Calendario$PlaceholderFragment" >
<TableLayout
android:id="#+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:stretchColumns="0,1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
</TableRow>
</TableLayout>
</RelativeLayout>
So, you once create tv_item1, tv_item2 and tv_item3. Then in cycle for all ArrayList you adding this views
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
At the second iteration you already add tv_item1 to tr. And you want to do it again. I guess you need just transfer this lines to cycle:
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
You're using a for-loop to add the same references to TextViews to your TableRow. So in the next iteration of the loop, the same objects are added to the TableRow (or TableLayout), again! They already have a parent by then.
Try to initialize the TableRow and TextView objects inside the (outer)for-loop.
EDIT: Modified your code.
void setCalendario(List<ArrayList<String>> l) {
// Here we initialize the objects we re-initialize every iteration of the loop
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
for (ArrayList<String> al : l) {
TableRow tr = new TableRow(this);
// I can't believe a freshly initialized TableRow object has views attached...
tr.removeAllViews();
tr.setPadding(0,10,0,10);
// Not sure why these layout params are needed already, as they are specified
// when adding this TableRow to the TableLayout object as well.
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
int i = 0;
for(String s : al) {
if (i == 0) {
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1) {
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2) {
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
because you are trying to put the same textView many times. you can use it only once, so you have to instantiate it again and again :
// you remove the definition of the text views here and you put it inside the loop
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
This is not the best way to do it, you should may be creat a methode that takes your "i" as parameter and returns a TextView (with gravity center ... and your staff).
I have a problem in Android creating a table adding rows dynamically. The error message is:
The specified child already has a parent. You must call removeView()
on the child's parent first
But why?
void setCalendario(List<ArrayList<String>> l){
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
the xml code:
<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"
tools:context="com.MSca.gorhinos.Calendario$PlaceholderFragment" >
<TableLayout
android:id="#+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:stretchColumns="0,1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
</TableRow>
</TableLayout>
</RelativeLayout>
So, you once create tv_item1, tv_item2 and tv_item3. Then in cycle for all ArrayList you adding this views
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
At the second iteration you already add tv_item1 to tr. And you want to do it again. I guess you need just transfer this lines to cycle:
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
You're using a for-loop to add the same references to TextViews to your TableRow. So in the next iteration of the loop, the same objects are added to the TableRow (or TableLayout), again! They already have a parent by then.
Try to initialize the TableRow and TextView objects inside the (outer)for-loop.
EDIT: Modified your code.
void setCalendario(List<ArrayList<String>> l) {
// Here we initialize the objects we re-initialize every iteration of the loop
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
for (ArrayList<String> al : l) {
TableRow tr = new TableRow(this);
// I can't believe a freshly initialized TableRow object has views attached...
tr.removeAllViews();
tr.setPadding(0,10,0,10);
// Not sure why these layout params are needed already, as they are specified
// when adding this TableRow to the TableLayout object as well.
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
int i = 0;
for(String s : al) {
if (i == 0) {
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1) {
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2) {
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
because you are trying to put the same textView many times. you can use it only once, so you have to instantiate it again and again :
// you remove the definition of the text views here and you put it inside the loop
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
This is not the best way to do it, you should may be creat a methode that takes your "i" as parameter and returns a TextView (with gravity center ... and your staff).
I'm having one empty Linear Layout in xml. And I'm adding some text view to it dynamically.
once these textviews exceeds 5 then i'm creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.
I'm able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.
my xml is as follows:
<?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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and my java file is as follows:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddTextViewsDynamically extends Activity implements
OnClickListener {
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_textview_dynamically);
button = (Button) findViewById(R.id.dyn_button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
}
Where am I going wrong?
Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using
text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
instead of
text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));
and you should see all your text views.
EDIT :
In your case this should do the trick;
xml :
<?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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
<LinearLayout
android:id="#+id/dyn_layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and code :
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
Actually you are doing things right, I just removed padding and made orientation of all Linear Layouts to vertical. Modify this on xml file too and it will work :))) Its just you are adding a padding of 10 or 60 which is going of the view taken by the parent.
Modified Java Code:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
// layout2.setPadding(10, 10, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.VERTICAL);
// layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
Try this one, then you will see that your code is OK.
<?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"
android:weightSum="100" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="80" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="20"
android:text="Add TextViews" />
</LinearLayout>