I have created a table through TableLayout (id=myProducts_tableLayout) programatically but it doesn't show on the screen. Below is the XML code.
#`<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<!-- Header Starts-->
<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<!-- Logo Start-->
<ImageView android:src="@drawable/real_time_bidding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="@layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- All Products View -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="@id/header">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#153E7E"
android:textSize="25px"
android:textStyle="bold"
android:typeface="serif"
android:gravity="center"
android:text="My Products"/>
<TableLayout
android:id="@+id/myProducts_tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
</TableLayout>
<!-- Link to Enter New Product -->
<TextView android:id="@+id/link_to_allProducts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="213dip"
android:layout_marginBottom="50dip"
android:textColor="#153E7E"
android:textSize="20px"
android:textStyle="bold|italic"
android:typeface="serif"
android:gravity="center"
android:text="Back to All Products"/>
</LinearLayout>
<!-- All Products View Ends -->
</RelativeLayout>
</ScrollView>
`
Below is the .java code
super.onCreate(savedInstanceState);
setContentView(R.layout.my_products);
String[] sampleTxt = {"txt1", "txt2", "txt3"};
ScrollView sv = new ScrollView(this);
final TableLayout layout = TableLayout findViewById(R.id.myProducts_tableLayout);
layout.setLayoutParams(new TableLayout.LayoutParams(4, 5));
for(int i = 0; i < sources.length; i++)
{
final TableRow tr = new TableRow(this);
final TextView t = new TextView(this);
final Button b = new Button(this);
final LayoutParams textparams = new LayoutParams(400,40); // Width , height
t.setLayoutParams(textparams);
t.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
t.setTextColor(Color.BLUE);
final LayoutParams btnparams = new LayoutParams(20,20);
btnparams.setMargins(10, 20, 15, 0); // (left, top, right, bottom)
b.setLayoutParams(btnparams);
b.setBackgroundResource(R.drawable.delete);
t.setText(sampleTxt[i]);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
layout.removeView(tr);
}
});
tr.addView(b);
tr.addView(t);
layout.addView(tr);
}
Could any one please point out the error. And also I have no idea why did the eclipse ask to make TableLayout, TableRow, TextView, Button "final", otherwise it was giving me an error.
Thanks
The problem is in the code which you did not copy. You probably have:
import android.view.ViewGroup.LayoutParams;
but you want:
import android.widget.TableRow.LayoutParams;
I would have thought the first one would have worked too since you are only specifying width and height, but it seems that it does not. TableRow.LayoutParams is a subclass of ViewGroup.LayoutParams. When you call b.setLayoutParams() or t.setLayoutParams, you have to pass as argument the LayoutParams for their parent , the TableRow.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.