TableLayout related issue - android

I want to show an array of strings into table layout. Having following code.I am able to get the data into log file but table layout i am seeing nothing.
TableLayout tl=(TableLayout)findViewById(R.id.maintable);
for(int current=0;current<cursor1.getCount();current++)
{
//create a table row
TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
// create a text view for start time
TextView sTime= new TextView(this);
sTime.setText(values[current]);
sTime.setTextColor(Color.YELLOW);
Log.i("Data Check", values[current]);
sTime.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(sTime);
//add the table row into TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

I know this is long time after but it may help you or someone else.
Where you create the row and add layout params to it , use the following type of params
TableRow.LayoutParams NOT the TableLayout.LayoutParams

Related

Dynamic TextView In Dynamic TableRow with marquee effect to textview in android

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);
}

Add row dynamically in TableLayout

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.

How we can align one or more textviews one below to another in table row of table layout in android through java code not xml

I am consrtucting a registration form in table layout for which I have to add various textviews in a table row one one below another.
You can get something like this:
Here is the code, you can get the basic idea.
// <table>
TableLayout table = new TableLayout(context);
// <row1>
TableRow row = new TableRow(context);
// <column1>
LinearLayout container = new LinearLayout(context);
container.setBackgroundColor(Color.parseColor("#ff0000"));
container.setOrientation(LinearLayout.VERTICAL);
container.addView(getSampleTextView("Stacked TextView 1", "#ff0000"));
container.addView(getSampleTextView("Stacked TextView 2", "#ffff00"));
row.addView(container);
// </column1>
// <column2>
row.addView(getSampleTextView("TextView in Another Column", "#ffffff"));
// </column2>
table.addView(row);
// </row1>
// <row2>
TableRow row2 = new TableRow(context);
row2.setBackgroundColor(Color.parseColor("#00ffff"));
row2.addView(getSampleTextView("TextView in Next Row", "#0000ff"));
table.addView(row2);
// </row2>
Here is how I get sample text view for demo purposes:
private TextView getSampleTextView(String text, String color) {
TextView textView = new TextView(getContext());
setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText(text);
textView.setBackgroundColor(Color.parseColor(color));
return textView;
}

Accessing in code generated TextView Resource

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()

Addint textview to tableview dynamically

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);

Categories

Resources