TableLayout items on the left and right - android

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

Related

How to create table structure in Android programmatically not in xml file

I am creating an Android app for online shopping. I just want to generate invoice after payment in table format programmatically. Please guide me to create a table view in java program of Android.
This is how you create a Table Layout Programmatically for both Linear Layout and Relative Layout :
-------------------------------------------------------------------------
1) Linear Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------
2) Relative Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new Relative.LayoutParams(Relative.LayoutParams.WRAP_CONTENT, Relative.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------

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

Centering TableLayout rows using Java Code

I would to create a TableLayout and center its content.
I write this piece of code:
TableLayout tl = new TableLayout(this);
tl.setGravity(Gravity.CENTER);
TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);
But It doesn't work. Rows are centered but only vertically... How can I do?
Thanks in advance.
Edit:
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));
TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);
firstRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));
secondRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));
You should set a LayoutParams for the TableLayout and set the gravity on it:
tl.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL) );

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

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