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()
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 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.
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 custom list with name,age and grade .I want to loop through the list and show the contents in a table layout.Each row will have following attributes.Row will have some kind of styling
Name,
Age,
Age_Value,
Grade,
Grade_Value
Here Age and Grades are just a label.Given below is a sample table
Dolly
Age
23
Grade
A+
Roman
Age
22
Grade
C
Ben
Age
23
Grade
B+
I want to add these details to the Table layout in my layout dynamically.
Here is the code im trying
for(int j=0;j<_attributeList.size();j++)
{
TableRow tr = new TableRow(MyActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tvName = new TextView(MyActivity.this);
tvName.setPadding(3,0,0,0);
tvName.setText(_attributeList.get(j).getName() +" : ");
tr.addView(tvName);
TextView tvAgeL = new TextView(MyActivity.this);
tvAgeL .setText("Age");
tr.addView(tvAgeL );
TextView tvAgeValue = new TextView(MyActivity.this);
tvAgeValue .setPadding(3,0,0,0);
tvAgeValue .setText(_attributeList.get(j).getValue() +" : ");
tr.addView(tvAgeValue );
attributeTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Since these items are adding to the same row im getting results in a single line. How can i show them in seperate rows
You should try like this
Code
for(int i = 0; i < _attributeList.size();i++){
TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
newCheckBox = new CheckBox(getApplicationContext());
TextView feeTypeText = new TextView(getApplicationContext());
feeTypeText.setText(jsonObj.getString("feeType"));
feeTypeText.setTextColor(Color.BLACK);
feeTypeText.setTextSize(16f);
feeTypeText.setTypeface(tf);
TextView dueAmountText = new TextView(getApplicationContext());
dueAmountText.setText(jsonObj.getString("dueAmount"));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(16f);
dueAmountText.setTypeface(tf);
TextView dueDateText = new TextView(getApplicationContext());
dueDateText.setText(jsonObj.getString("dueDate"));
dueDateText.setTextColor(Color.BLACK);
dueDateText.setTextSize(16f);
dueDateText.setTypeface(tf);
newRow.addView(newCheckBox,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(dueAmountText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
newRow.addView(dueDateText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
attributeTable.addView(newRow);
}
you can add like this way...here i added TableRow Programatically in which i added textView.
addtree.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
tr1 = new TableRow(YourActivity.this);
tr1.setClickable(true);
tr1.setId(jj);
tr1.setPadding(0, 5, 0, 5);
Log.d("id", "id is......"+tr1.getId());
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView textview = new TextView(New_Quote_Activity.this);
textview.setText("Job "+jj);
textview.setTextColor(Color.BLACK);
textview.setTypeface(null, Typeface.BOLD);
textview.setTextSize(14);
textview.setPadding(5, 0, 0, 0);
View vv=new View(New_Quote_Activity.this);
vv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv.setBackgroundResource(R.color.viewcolor);
View vv1=new View(New_Quote_Activity.this);
vv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv1.setBackgroundResource(R.color.viewcolor);
tr1.addView(textview);
table_crete_invoice.addView(vv1);
table_crete_invoice.addView(tr1,layoutParams);
table_crete_invoice.addView(vv);
}
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