Accepting user data into a table - android

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

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.

Dynamically Update TableLayout

Edit: as Blumer pointed out, I was not adding the items to the table, so that this question appeared just because I was careless and I didn't see my mistake.
I am trying to create a dynamic TableLayout, as I have to receive results from the server and add rows based on the results, but the table is not updating. (Also, the TableLayout already has 1 initial row, the header row).
This is my code:
Room[] rooms = State.rooms;
TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
for(int i = 0; i < rooms.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
Resources res = getResources();
TextView txt1 = new TextView(this);
txt1.setLayoutParams(layout_wrapwrap);
txt1.setTextColor(res.getColor(android.R.color.black));
txt1.setText(rooms[i].name);
TextView txt2 = new TextView(this);
txt2.setLayoutParams(layout_wrapwrap);
txt2.setTextColor(getResources().getColor(android.R.color.black));
txt2.setText(rooms[i].price + " " + rooms[i].currency);
EditText edit1 = new EditText(this);
edit1.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit1.setEms(3);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
EditText edit2 = new EditText(this);
edit2.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit2.setEms(3);
edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
Spinner spinner = new Spinner(this);
layout_wrapwrap.rightMargin = 0;
spinner.setLayoutParams(layout_wrapwrap);
Integer[] numbers = new Integer[rooms[i].count];
for(int j = 0; j < numbers.length; j++) {
numbers[j] = i + 1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
BookActivity.this, R.layout.spinner_textview, numbers);
spinner.setAdapter(adapter);
tblBookDetails.addView(tr);
}
//Another exquisite beauty of Java.
Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
tblBookDetails.invalidate();
tblBookDetails.refreshDrawableState();
To prevent any confusion, the Room[] array is just a simple property-holder class.
This code looks enormously convoluted, and the table is not updating. I've searched quite a bit on the Internet, and I could not find any solution for this problem.
Thank you in advance.
I see where you add tr to tblBookDetails, but I don't see anywhere where you put txt1, txt2, edit1, etc. in tr. Try adding those views to the row, and I think that should get you there, because right now you appear to be adding the TableRow, but there's nothing in it.

TextView & StringArray misfunction

Well i made an activity where i am creating some TextViews based on the size of a string array! But despite the fact that my string array has 4 items on it, which i tested it with debugging, the textviews that are created is only 1. If anyone has an idea about it please tell me :)
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
final TextView[] tv = new TextView[daily_lessons.length];
final LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
for (int i=0; i<daily_lessons.length; i++){
tv[i] = new TextView(this);
tv[i].setText(daily_lessons[i]);
tv[i].setTextSize(20);
tv[i].setLayoutParams(new LinearLayout.LayoutParams((int)LayoutParams.FILL_PARENT,(int) LayoutParams.WRAP_CONTENT));
tv[i].setGravity(Gravity.CENTER);
layout.addView(tv[i]);
}
If you still need an answer to this question here is what I would do.
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
TextView tmpView = null;
for (int i=0; i<daily_lessons.length; i++){
tmpView = new TextView(this);
tmpView.setText(daily_lessons[i]);
tmpView.setTextSize(20);
layout.addView(tmpView , new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
I use this type of code alot for my dynamically genenerated content (obtain content from prepopulated database).
The TextView seems to be created but might now be visible on the GUI may be stacked over each other etc.
1.Use the layout.setOrientation(ORIENTAIION.VERTICAL) on the parent linear layout's.
2.Use the childCount() on layout to make it sure on the fact that the all 4 text views have been added to the snippet.
3.Also make sure your are not using removeALLView() etc methods for your case study to the problem case.

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

Categories

Resources