Add array of strings to linear layout - android

My code is below. I am displaying the details, but not in a proper way. Can you please help me?
TextView tv1=new TextView(this);
tv1.setText("DisplayName:"+strDisplayName);
TextView tv2=new TextView(this);
tv2.setText("Email:"+strEmail);
TextView tv3=new TextView(this);
tv3.setText("FirstName:"+strFirstName);
TextView tv4=new TextView(this);
tv4.setText("LastName:"+strLastName);
TextView tv5=new TextView(this);
tv5.setText("CreatedDate:"+strCreatedDate);
mProfileLayout.addView(tv1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv3, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv4, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv5, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Do you have all these TextViews in your xml? If you do then you can access them with
TextView tvX = (TextView)findViewById(R.id.textviewXid);
and then just set the text
tvX.setText(string);
And so you dont have to use mProfileLayout.addView(....);

Related

How to place table layout below Linear layout programmatically in android

Here is my code to display table layout below linear layout.here i am setting scroll view as contentview.but only table layout displaying on screen.here any problem in layout.please help me thanks.
ScrollView scrollView = new ScrollView(this);
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Button btn1 = new Button(this);
btn1.setText("Button1");
ll.addView(btn1);
btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
Here tried by placing the linear layout above the table layout now only linearlayout displaying
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Set the TextView for the Question
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setText("HI");
ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
//Here i have table rows code
ll.addView(scrollView);
setContentView(ll);
now setting linear layout as contentview as per your answers scroll view contains only table layout.but why it is not displaying in the screen
ScrollViews in android should contain only a single child. So might I suggest you add a LinearLayout inside the ScrollView and then add the other components in this LinearLayout
check this:\
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Set the TextView for the Question
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setText("HI");
ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TableLayout resultLayout = new TableLayout(this);
resultLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
scrollView.addView(resultLayout);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
ll.addView(scrollView);

Android Adding more than one TextView to a TableRow Programmatically

I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code.
LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
This gets my LinearLayout and creates a TableRow to the specifications I want. I know this part is working.
TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");
TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");
Here I make my two TextViews with no problem.
tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Here is where I assume everything goes wrong. What happens is it only shows the textTwo. I don't know why it's not showing both of them in order like a normal TableRow would in XML. And I repeat, this must be done in code.
Please help, thank you.
Have you imported this import android.widget.TableRow.LayoutParams
Below code works for me
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:
TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
TableRow tableRow = new TableRow(getContext());
int i = 0;
for (Skills s : listSkills) {
TextView textView = new TextView(getContext());
textView.setText("" + s.getName());
tableRow.addView(textView);
if(i > 0) {
tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
}
tlSkills.addView(tableRow);
i++;
}
}

TableLayout under LinearLayout is not displaying

I used LinearLayout under that TextView and TableLayout.
Output:
I'm able see TextView contents in model but not TableLayout.
Please tell me what the mistake i did?
LinearLayout quizlinear = new LinearLayout(this);
quizlinear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
quizlinear.setOrientation(LinearLayout.VERTICAL);
quizlinear.setBackgroundColor(Color.BLACK);
quiztextview = new TextView(this);
quiztextview.setText("BEER THERE");
quiztextview.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
quiztextview.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
quiztextview.setTextColor(Utils.getColor("#FFFFFF"));
quiztextview.setTextSize(30);
quizlinear.addView(quiztextview);
quiztablelayout = new TableLayout(this);
TableRow quiztablerow = new TableRow(this);
quiztablerow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TableRow quiztablerow1 = new TableRow(this);
quiztablerow1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ImageView arrowImg1 = new ImageView(this);
arrowImg1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
arrowImg1.setImageResource(R.drawable.icon);
quiztablerow.addView(arrowImg1);
ImageView arrowImg2 = new ImageView(this);
arrowImg2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
arrowImg2.setImageResource(R.drawable.icon);
quiztablerow.addView(arrowImg2);
ImageView arrowImg3 = new ImageView(this);
arrowImg3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
arrowImg3.setImageResource(R.drawable.icon);
quiztablerow1.addView(arrowImg3);
ImageView arrowImg4 = new ImageView(this);
arrowImg4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
arrowImg4.setImageResource(R.drawable.icon);
quiztablerow1.addView(arrowImg4);
quiztablelayout.addView(quiztablerow,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
quiztablelayout.addView(quiztablerow1,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
quizlinear.addView(quiztablelayout);
TextView quiztextview1 = new TextView(this);
quiztextview1.setText("DONE THAT!");
quiztextview1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
quiztextview1.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
quiztextview1.setTextColor(Utils.getColor("#FFFFFF"));
quiztextview1.setTextSize(30);
quizlinear.addView(quiztextview1);
TextView quiztextview2 = new TextView(this);
quiztextview2.setText("Version1.0 copyrights #2011 janardhan solutions pvt ltd");
quiztextview2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
quiztextview2.setTypeface(Typeface.SANS_SERIF);
quiztextview2.setGravity(Gravity.CENTER);
quiztextview2.setTextColor(Utils.getColor("#FFFFFF"));
quiztextview2.setBackgroundColor(Color.BLUE);
quiztextview2.setTextSize(10);
quizlinear.addView(quiztextview2);
setContentView(quizlinear);
change your LayoutParams should be of android.widget.TableRow.LayoutParams except one that supplied to quiztablelayout.addView(...)
quiztablelayout.addView(quiztablerow,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
quiztablelayout.addView(quiztablerow1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
quizlinear.addView(quiztablelayout);

TableLayout items on the left and right

hy!
I want to have a TextView on the left and the right. But both is on the right. all settings should be done by code.
Another quest:
How to add´a horizantal line? by making a nother TableRow?
Photo:
My code:
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("Test");
tv.setGravity(Gravity.LEFT);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.RIGHT);
tv2.setText("Test");
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
Try this stuff,
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv.setText("Test");
tv.setGravity(Gravity.LEFT);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv2.setGravity(Gravity.RIGHT);
tv2.setText("Test");
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
setContentView(tl);

Programmatically RelativeLayout layouting issue

I have an Achievement dialog, and would like this:
And this is my code, each achievement is a relative layout, and all are contained in a LinearLayout (pnlAchievement):
RelativeLayout pnlItem = new RelativeLayout(Parent);
pnlItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView txtItemName = new TextView(Parent);
txtItemName.setText(ACHIEVEMENTS_NAME[i]);
txtItemName.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView txtAchived = new TextView(Parent);
txtAchived.setText(Achived[i] ? "Achieved!" : "Not Achieved!");
RelativeLayout.LayoutParams rlpAchived = new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rlpAchived.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlpAchived.addRule(RelativeLayout.ALIGN_RIGHT, txtItemName.getId());
txtAchived.setLayoutParams(rlpAchived);
TextView txtDescription = new TextView(Parent);
txtDescription.setText(ACHIEVEMENTS_DESCRIPTION[i]);
txtDescription.setHorizontallyScrolling(false);
RelativeLayout.LayoutParams rlpDesc = new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rlpDesc.addRule(RelativeLayout.ALIGN_BOTTOM, txtItemName.getId());
rlpDesc.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
txtDescription.setLayoutParams(rlpDesc);
pnlItem.addView(txtItemName);
pnlItem.addView(txtAchived);
pnlItem.addView(txtDescription);
pnlAchievement.addView(pnlItem);
It work well list all the achievements, but the description is NOT below the Name, it is OVER the Name, like this:
And, can you please tell me how to add the line between every item without using Canvas (this is not very important, but if there is, it's better)?
You need to use RelativeLayout.BELOW
For the line you can use a Label with a backgroundcolor.

Categories

Resources