Align components in TableRow - android

First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!

Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);

Related

Android how to create two table layouts in Activity?

Im trying to create two table layouts through activity..
I already have one table layout but how to set through activity?
I know to do it through xml but want to do it programatically..
Please Help
Check this answer and another example here
Just like in xml, you will create a TableLayout, provide params and add rows with your own UI in it.
Take one linear layout(or relative layout) in in your xml get it reference by findViewById() in onCreate() method of your activity.after that create table dynamically and add it to the linear layout.I create a method to do so . ex-
LinearLayout linear= (LinearLayout ) findViewById(R.id.linear);
//call method to add the tablelayout.
linear.addView(createtable(3,5));
private TableLayout createtable(int requiredcolumn, int requiredrow) {
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1f);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
//for border
rowParams.setMargins(2, 2, 2, 2);
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(Color.WHITE);
for (int row = 0; row < requiredrow; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < requiredcolumn; column++) {
Random color = new Random();
int randomColor = Color.argb(255, color.nextInt(256),
color.nextInt(256), color.nextInt(256));
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}

Adding TableRows dynamically in Android doesn't work

I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.

TableLayout with pagination

I want to implement a TableLayout with pagination, showing records of 10 at page 1 and then next 10 on page 2 and so on... I'm using Table Layout but I'm not getting the required result. Using the tutorial, the table just grows horizontally not vertically. How do I change it so that it also grows vertically and then when record size is greater then 10 then shift to next page?
The code:
public class Sample extends Activity {
String companies[] = {"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung","Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung"};
String os[] = {"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada","Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada"};
TextView companyTV,valueTV,deviceTv,actionTv,dateTv,timeTv,creditTv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// creates the Pagination Layout
PaginationLayout paginationLayout = new PaginationLayout(this);
paginationLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// creates content only for sample
TableLayout table = new TableLayout(this);
//table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row);
TableRow row2 = new TableRow(this);
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row2);
for (int i=0;i<companies.length;i++)
{
deviceTv = new TextView(this);
deviceTv.setText(companies[i]);
deviceTv.setTextColor(Color.RED);
deviceTv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
deviceTv.setPadding(5, 5, 5, 5);
row2.addView(deviceTv);
actionTv = new TextView(this);
actionTv.setText(os[i]);
actionTv.setTextColor(Color.GREEN);
actionTv.setPadding(5, 5, 5, 5);
actionTv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row2.addView(actionTv); // Adding textView to tablerow.
}
/* for(int i = 0; i< 50;i++){
Button button = new Button(this);
button.setText("Button " + i);
if (i%2==0) {
row.addView(button);
} else {
row2.addView(button);
}
}*/
// add the content in pagination
paginationLayout.addView(table);
// set pagination layout
setContentView(paginationLayout);
}
I have already using Table Layout but not getting my required result
using this tutorial it just grow horizontally not vertically
For the code on github it appears that using addView on the PaginationLayout adds that view to a HorizontalScrollView. As in your code you're creating a single Tablelayout to which you add rows, it's normal that your content grows vertically.
You'll most likely need to insert on your own the pages, with something like this:
// creates the Pagination Layout
PaginationLayout paginationLayout = new PaginationLayout(this);
LinearLayout wrapper = new Linearlayout(this);
paginationLayout.addView(wrapper);
// now create a TableLayout, if the content is bigger then your intended number of rows
// then insert another Tablelayout and so on
TableLayout table = new TableLayout(this);
//table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row);
TableRow row2 = new TableRow(this);
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row2);
wrapper.addView(table); // it will probably don't look good if you don't
// also setup the proper width with LayoutParams
If you continue to add TableRows and you see that you've added 10 rows to table then create a new TableLayout and add that to the wrapper LinearLayout.
I would advise you to drop that library and implement your own pagination. If you don't what direct interaction from your user(through touching) then it's quite simple to setup a ViewFlipper(with two views, two pages through which you'll continue to flip) with two buttons at the bottom to control the pagination. If you also want touch interaction, then use a ViewPager(with a bit more work to do).

Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.
My current code looks as follows.
paymentTable = (TableLayout) findViewById(R.id.paymentTable);
for(i = 0; i < cursor.getCount(); i++) {
TableRow row = new TableRow(this);
TextView payAmount = new TextView(this);
payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
payAmount.setPadding(payAmount.getPaddingLeft(),
payAmount.getPaddingTop(),
textView.getPaddingRight(),
payAmount.getPaddingBottom());
TextView payDate = new TextView(this);
payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));
row.addView(payDate);
row.addView(payAmount);
paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
cursor.moveToNext();
}
Afterwards, the TableLayout looks something along the lines of:
January$500
February$500
March$405
But I want it to look like:
January $500
February $500
March $405
To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).
yawus this is a good tutorial will help you set the table layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is the xml layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
this is the activity code
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
You need to add a RelativeLayout to each Table
TableRow row = new TableRow(this);
row.setId(tag);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT);
rlp.setMargins(0, 0, 0, 0);
row.addView(relativeLayout, rlp);
Then you add your TextView to the RelativeLayout. Like this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);
This is the way I handle positioning interface components within a table row.

How to set the row in a table layout?

So far, I have code that dynamically creates my table within a horizontal scrollview. I want each item to appear in the same y location and so that you can only see one at a time. Right now, each column gets set, but the visual is like a staircase, where the next row is underneath and only moved over about 50px. Is there a way to force each column to take up the entire screen width? Also, can you set the row that the content appears in?
TableLayout tl = (TableLayout)findViewById(R.id.contentTable);
for(int i=0;i<itemIndexes.size();i++)
{
TableRow tr = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
{
ImageView iv = new ImageView(this);
iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
else
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.noimage);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
TextView tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tl.addView(tr);
}
As you have use the TableLayout and now you want each cell to consume appropriate same size throughout the table respectfully by comparing itself via row and column.
So, you have to set WEIGHT to each item and specify weight value = "1.0" or below it.

Categories

Resources