In my application i am using the following code for display content in table view. following code works fine for me but here it is displaying data with out borders but i want to display data with borders for each row and column also.
Here is the reference code i used:
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Unit Price ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Stock Remaining ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 0; i < 25; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("Rs." + i);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + i * 15 / 32 * 10);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
}
}
and i called this init() in my oncreate method as below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
}
finally xml code:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
any one help me to add borders to the output.
The easiest way to do is here. You can get a hint from here.
As link says to give background to text view, you can apply it at run time too.
Related
I'm developing a basic android app since I'm a beginner. I created a dynamic table with 4 columns as: Serial number, width, length and the area of width and length entered.
But I'm not understanding how to calculate result at the same time and display in the cell of fourth column as the user is entering the data in the respective fields.
Here is the java code that I've come up with so far:
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
TableRow.LayoutParams r1 = new TableRow.LayoutParams(180, 50);
tv0.setTextSize(15);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.BLACK);
tv0.setBackgroundResource(R.drawable.cell_shape);
tv0.setLayoutParams(r1);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setTextSize(15);
tv1.setBackgroundResource(R.drawable.cell_shape);
tv1.setText(" Len ");
//tv1.setPadding(5,5,5,5);
tv1.setTextColor(Color.BLACK);
tv0.setLayoutParams(r1);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setTextSize(15);
tv2.setBackgroundResource(R.drawable.cell_shape);
tv2.setText(" Wid ");
tv2.setTextColor(Color.BLACK);
//tv2.setPadding(10,5,10,5);
tv2.setLayoutParams(r1);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setTextSize(15);
tv3.setBackgroundResource(R.drawable.cell_shape);
tv3.setText(" Sq Ft ");
//tv3.setPadding(5,5,5,5);
tv3.setTextColor(Color.BLACK);
tv3.setLayoutParams(r1);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 1; i <= n; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setBackgroundResource(R.drawable.cell_shape);
t1v.setText("" + i);
t1v.setTextSize(15);
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
EditText t2v = new EditText(this);
t2v.setBackgroundResource(R.drawable.cell_shape);
//tv2.setHint("");
//t2v.setText("Product " + i);
//t2v.setTextSize(15);
//t2v.setId(i);
t2v.setTextColor(Color.BLACK);
t2v.setGravity(Gravity.CENTER);
t2v.isClickable(true);
tbrow.addView(t2v);
EditText t3v = new EditText(this);
t3v.setBackgroundResource(R.drawable.cell_shape);
//t3v.setText("Rs." + i);
t3v.setTextColor(Color.BLACK);
t3v.setGravity(Gravity.CENTER);
t3v.setId(i+1);
//t3v.setTextSize(15);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setBackgroundResource(R.drawable.cell_shape);
t4v.setText(""+calculation);
t4v.setTextColor(Color.BLACK);
t4v.setGravity(Gravity.CENTER);
t4v.setTextSize(15);
tbrow.addView(t4v);
stk.addView(tbrow);
}
here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<TableLayout
android:layout_margin="9dp"
android:id="#+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</ScrollView>
I'm adding table in linearlayout programmatically but it is not displaying on screen.
Below is code -
public void displayTable(){
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout mTableLayout=new TableLayout(getActivity());
TableRow mTableRow;
LinearLayout mTableLinearLayout;
TextView mSrNotxt,mDatetxt,mTimetxt;
Date mDate=new Date();
String mCDate=mDate.getDate()+"-"+(mDate.getMonth()+1)+"-"+(mDate.getYear()+1900)+"";
mTableLayout.setLayoutParams(lp2);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
for(int count=0;count<mTimeList.size();count++)
{
mTableRow=new TableRow(getActivity());
mTableRow.setLayoutParams(lp);
mSrNotxt=new TextView(getActivity());
mDatetxt=new TextView(getActivity());
mTimetxt=new TextView(getActivity());
mSrNotxt.setTextColor(getResources().getColor(R.color.black));
mDatetxt.setTextColor(getResources().getColor(R.color.black));
mTimetxt.setTextColor(getResources().getColor(R.color.black));
mSrNotxt.setLayoutParams(tlp);
mDatetxt.setLayoutParams(tlp);
mTimetxt.setLayoutParams(tlp);
mSrNotxt.setTextSize(12);
mDatetxt.setTextSize(12);
mTimetxt.setTextSize(12);
if(count==0){
mSrNotxt.setText("No.");
mDatetxt.setText("Date");
mTimetxt.setText("Time in Min.");
}
else{
mSrNotxt.setText((count+1+""));
mDatetxt.setText(mCDate);
mTimetxt.setText(mTimeList.get(count));
}
mTableLinearLayout=new LinearLayout(getActivity());
mTableLinearLayout.setLayoutParams(lp);
mTableLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
mTableRow.addView(mTableLinearLayout);
mTableLayout.addView(mTableRow);
}
System.out.println("table created");
mChartLayout.addView(mTableLayout);
}
xml view for chartLayout -
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/chartView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
How can I display table?Is there any way?
Answer -
Need to add textview in view -
mTableLinearLayout.addView(mSrNotxt);
mTableLinearLayout.addView(mDatetxt);
mTableLinearLayout.addView(mTimetxt);
I am able to add Table using this code :
TableLayout mTableLayout = new TableLayout(this);
mTableLayout.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int count = 0; count < 3; count++) {
TableRow row = new TableRow(this);
row.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)));
TextView valueTV = new TextView(this);
valueTV.setText("text : "+count);
// valueTV.setId(5);
valueTV.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(valueTV);
mTableLayout.addView(row);
}
mChartLayout.addView(mTableLayout);
Add other required properties
Hope it helps ツ
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 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.
No Textview occurs in this TableLayout. I don't know why.
I want to make the entries manually.
Another question, how can I add a horizontal line?
Code:
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
loadData();
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
TextView tv2 = new TextView(this);
//tv.se
tv.setGravity(Gravity.LEFT);
tv2.setGravity(Gravity.RIGHT);
tv.setText("Test");
tv2.setText("Test ");
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
//tv.setTextSize(50);
//tv2.setTextSize(50);
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
setContentView(tl);
Layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tl">
</TableLayout>
The problem could be that you aren't setting any LayoutParams for your TableRow.
Try doing something like this:
tr.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Also if you want to add a horizontal line you can simply add a new View object and set its layout_height to 1dp and its backgroundColor to Color.BLACK, or whatever height/color combination you want
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
TextView tv2 = new TextView(this);
//tv.se
tv.setGravity(Gravity.LEFT);
tv2.setGravity(Gravity.RIGHT);
tv.setText("Test");
tv2.setText("Test ");
tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
//tv.setTextSize(50);
//tv2.setTextSize(50);
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
main.xml
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tl">
</TableLayout>
add new horizontal line using