TableLayout under LinearLayout is not displaying - android

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

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

View not showing in layout when layoutparams is set programatically

Every time I set the layout param of TextView to wrap content or fill parent it's not showing in the view, but when I set it to pixels it's working why is that?
Here is the relevant code:
items = (LinearLayout) findViewById(R.id.llmain);
tb = new TableLayout(MainActivity.this);
tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tb.setGravity(Gravity.CENTER);
TableRow tr = new TableRow(MainActivity.this);
ImageButton ima = new ImageButton(MainActivity.this);
ImageButton imr = new ImageButton(MainActivity.this);
TextView tvin = new TextView(this);
TableRow.LayoutParams trp = new TableRow.LayoutParams();
trp.setMargins(0, 0, 0, 10);
tr.setLayoutParams(trp);
tr.setGravity(Gravity.CENTER);
ima.setBackgroundColor(Color.TRANSPARENT);
ima.setImageResource(R.drawable.add);
imr.setBackgroundColor(Color.TRANSPARENT);
imr.setImageResource(R.drawable.remove);
tvin.setWidth(100);
tvin.setHeight(45);
tvin.setGravity(Gravity.CENTER);
tvin.setText(sil[i]);
tvin.setBackgroundColor(Color.rgb(200, 0, 255));
tr.addView(ima);
tr.addView(tvin);
tr.addView(imr);
tb.addView(tr);
items.addView(tb);
I tried using this
tvin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
and this
tvin.setWidth(LayoutParams.FILL_PARENT);
tvin.setHeight(LayoutParams.FILL_PARENT);
but none of these work.
Try:
tvin.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
and remove the tvin.setWidth and height.
You should always set the layout params of view's parent, TableRow in this case.
MATCH_PARENT makes no difference but it's a new name and you should be using it instead of FILL_PARENT.
In this Line u have to use height and width .. TableRow.LayoutParams trp = new TableRow.LayoutParams(); Like this
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
Try to use the LinearLayout.LayoutParams
TextView tvin = new TextView(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tvin.setLayoutParams(llp);

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

Add array of strings to linear layout

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

Categories

Resources