I want to dynamically add a row in a table whenever this function is called from a different class. Right now it crashes the program. I looked through many topics but couldn't find anything.
public void newRow(String time, String cloc, String loc, String contact) {
TableLayout tl = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
TextView t3 = new TextView(this);
TextView t4 = new TextView(this);
t1.setText(time);
t1.setText(cloc);
t1.setText(loc);
t1.setText(contact);
tr.addView(t1);
tr.addView(t2);
tr.addView(t3);
tr.addView(t4);
tl.addView(tr, TableLayout.LayoutParams.WRAP_CONTENT);
}
Don't create new instance of TableLayout on each call
Create a global instance of TableLayout so that it will be accessible
in newRow() method. Remember that you would want to creat just one
TableLayout and repeatedly add new rows to it.
You are calling setText() method repeatedly on t1 instance. I don't
think you intend to do that.
Post the crash logs so that I could help you further.
Related
I am developing one app where i am displaying some data in table format.so i implemented following code
#Override
protected void onPostExecute(String result) {split1=new String[split.length];
split2=new String[split.length];
TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
for(int i=0;i<split.length;i++)
{
String aa=split[i];
String [] aaa=aa.split(" : ");
split1[i]=aaa[0];
split2[i]=aaa[1];
TableRow row=new TableRow(getApplicationContext());
TableLayout.LayoutParams lp =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(10,10,10,10);
row.setLayoutParams(lp);
TextView tvDebt=new TextView(OfferItemView.this);
TextView tv=new TextView(OfferItemView.this);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv.setSingleLine(true);
tv.setSelected(true);
Log.v("Detail",""+split2[i]);
tvDebt.setText("" + split1[i]);
tv.setText(" : " + split2[i]);
row.addView(tvDebt);
row.addView(tv);
table.addView(row);
}
}
It display data but the if one of the string is large then that text go out of screen.i want to apply merquee effect if string is large..
I am adding the screenshot of that tablelayout
See the text go out of the screen size.
so i want to display marquee effect to it
Please give solution to me because from last 2 months i didnt get solution
Thanks a lot in advance
You will probably like this solution more. You will have to do minimal modifications to make it work with your existing code. The problem you had is due to the order you set the text view attributes I believe. Set the attributes in the order I have in the below example to get the marquee effect.
TableLayout tableLayout;
private void marquee() {
tableLayout = (TableLayout) findViewById(R.id.myTableLayout);
TextView textView1 = new TextView(getApplicationContext());
TextView textView2 = new TextView(getApplicationContext());
//the order in which you set the properties of the text view is important
//this order works
textView1.setText("Some long garbage bla bla bla barf narf laf tarf marf garf yarf parf");
textView1.setTextSize(25);
textView1.setWidth(100);
textView1.setHeight(150);
textView1.setLines(1);
textView1.setHorizontallyScrolling(true);
textView1.isFocusableInTouchMode();
textView1.isFocusable();
textView1.setSelected(true);
textView1.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView1.setMarqueeRepeatLimit(-1);
//I show that you can apply the effect on more than 1 text view
textView2.setText("Some long garbage bla bla bla barf narf laf tarf marf garf yarf parf");
textView2.setTextSize(25);
textView2.setWidth(100);
textView2.setHeight(150);
textView2.setLines(1);
textView2.setHorizontallyScrolling(true);
textView2.isFocusableInTouchMode();
textView2.isFocusable();
textView2.setSelected(true);
textView2.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView2.setMarqueeRepeatLimit(-1);
//create row
TableRow row = new TableRow(getApplicationContext());
TableLayout.LayoutParams lp =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(10, 10, 10, 10);
row.setLayoutParams(lp);
//add the text views to the row
row.addView(textView1);
row.addView(textView2);
//add the row to the table
tableLayout.addView(row);
}
I'm designing an app which will accept user input into a TableLayout . The table has 4 headings . I'm unsure of how to get the data into the table . I have a successfully added a test piece of code but I can only add it once. I've attached the code below.
What I wish to know is how to get this to add more than one line to the table?
public void addNew(){
TableLayout tL = (TableLayout)findViewById(R.id.table);
TableRow tR = new TableRow(getApplicationContext());
TextView tV = new TextView(getApplicationContext());
tV.setTextColor(0xff000000);
tV.setText("TEST");
TextView tV2 = new TextView(getApplicationContext());
tV2.setTextColor(0xff000000);
tV2.setText("TEST");
TextView tV3 = new TextView(getApplicationContext());
tV3.setTextColor(0xff000000);
tV3.setText("TEST");
TextView tV4 = new TextView(getApplicationContext());
tV4.setTextColor(0xff000000);
tV4.setText("TEST");
tR.addView(tV);
tR.addView(tV2);
tR.addView(tV3);
tR.addView(tV4);
tL.addView(tR);
}
for (/* x rows */)
{
tL.addView(tR);
}
This should work but if you run into an issue with text view id's clashing you can create an array of text views and an array of table rows. Would make managing and creating them easier as well.
If you make them in an array by doing something like this.
TextView[] textViewArray = new TextView[textViewCount];
for(int i = 0; i < textViewCount; i++)
{
textViewArray[i] = new TextView(this);
}
then you would be able to handle individual values by doing something like this.
textViewArray[i].setText(/* This stuff */);
I have a little problem. I have a tool that will likly have a large an changing number of channels which should be displayed in an Layout. So I have to create the TextView for the displaying of the values in code. Which worked like a charm.
My problem:
How do I access the created "valueTV" Field in my Code? Specifically I want to write a Value of the accelerometer into it, which is stored in String SAccX.
Is there a method to see the created field with the R.id.XXX in the DDMS or Debug window of Eclipse?
If I try to access it with the assigning like an normal XML Object Eclipse gives me an error (something like " sAccX = (TextView) findViewById(R.id.valueTV201); ). I understand why this error is coming up but I don't know how to get around it ;-)
//Get Tablelayout
TableLayout ChannelTable = (TableLayout) findViewById(R.id.TableChannels);
//Create a new Row for every Channel
for (int current = 0; current < numberOfChannels; current++)
{
// Create a Table with new ID
TableRow tr = new TableRow(this);
tr.setId(100 + current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to show the Name of the Channel
TextView TVChannels = new TextView(this);
TVChannels.setId(200 + current);
TVChannels.setText(channels[current]);
TVChannels.setTextColor(Color.DKGRAY);
TVChannels.setTextSize(20);
TVChannels.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(TVChannels);
// Create a TextView to house the values
TextView valueTV = new TextView(this);
valueTV.setId(200 + current);
valueTV.setTextColor(Color.DKGRAY);
TVChannels.setTextSize(20);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
ChannelTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
} /* End of OnCreate*/
Use tags instead of ID's
View.setTag()
and then to find the view with a certain tag use
findViewWithTag()
I want to populate the android tableview's rows with an array at runtime...
How to go about it....please advise
Thanks,
You can populate table view row's by reading tables child as follows:
TableLayout x = (TableLayout) findViewById(R.id.table_id);
for(int i=0;i<x.getChildCount();i++){
TableRow tr = (TableRow) x.getChildAt(index);
//Do here with table row, what u want to do.
}
TableLayout layout = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(context);
TextView view = new TextView(context);
view.setText("Test");
tr.addView(view);
layout.addView(tr);
I have a dynamic table layout created in OnCreate(). Everytime the activity resumes, the data in the table rows can change, so I would like to destroy the table rows and create new ones.
How can I do that? Thanks!!!
Here is what the OnCreate table creation portion looks like:
tableLayout tl = new TableLayout(this);
for (i = 0; i < numberOfVariables; i++) {
TableRow tr = new TableRow(this);
CheckBox cb = new CheckBox(this);
tr.addView(cb);
TextView dv = new TextView(this);
tr.addView(dv);
EditText et = new EditText(this);
tr.addView(et);
tl.addView(tr);
}
ll.addView(tl);
Just use tl.removeAllViews();, and then recreate the rows.