TableLayout with pagination - android

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

Related

work with dynamically created checkboxes

I create tablerows and checkboxes dynamically with a code like this:
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a Checkbox to add to the row **/
CheckBox cb = new CheckBox(this);
cb.setText(p.getEan());
LinearLayout Ll1 = new LinearLayout(this);
Ll1.addView(cb);
System.out.println(j);
tr.addView((View)Ll1); // Adding CheckBox to tablerow.
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getName());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
After its created the user can check or uncheck.
With the check, I need to do run another command for every checkbox.
My problem is, how do I use the checkboxes? How i can "speak" with so i can work with it?
Call generateViewId() (or leave setId(i)) and use generated id in setId() for your programatically generated checkboxes. Save id's locally during your activity life and use this id's in same way as static id's from R. in setOnCheckedChangeListener.
You gave the rows and checkboxes the same id's. Don't give the rows id's. Or different ones.
You can place following code in on check change listeners.
LinearLayout my_layout = (LinearLayout)findViewById(R.id.my_layout);
for (int i = 0; i < Array_Count; i++)
{
CheckBox checkBox = (CheckBox)my_layout.findViewById(i);
if ( checkBox == null )
continue;
Toast.makeText(context, checkBox.getText(), Toast.LENGTH_SHORT).show();
checkBox.setText("my id is: " + i);
}

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

Multiple rows for table in android application

I am a starter in android application development. Kindly help me how to create multiple rows in a table and key performance to create a row. I am able to create 3 rows now. but i want to autogenerate the rows while entering the datas in table.
try this:
//Declare mytable in layout XML file.
TableLayout tl = (TableLayout) findViewById(R.id.mytable);
//loop through number of times u want to create row.
for (int current = 0; current < numofrowsrequired; current++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//add required views with required parameters
TextView labelTV = new TextView(this);
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

Android - Dynamic ColSpan not working

I am having two table rows in one of my activity. First row will always contains 2 textview and second might contains one or two textview (it depends on userinput). Whenever I am having one textview in 2nd row, I tried to span it to two columns. I am trying something this
TableRow.LayoutParams param = (LayoutParams)editText.getLayoutParams();
param.span = 2;
txtView.setLayoutParams(param);
But its not working. What happens is, second txtview in first row is pushed out of the screen. So can anyone here tell where i am making mistake?
I made this example, hope it helps you.
TableLayout table = (TableLayout)findViewById(R.id.table_id_in_xml);
TableRow row = new TableRow(this);
TableRow row2 = new TableRow(this);
TextView col1, col2, col3;
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
col1 = new TextView(this);
col2 = new TextView(this);
col3 = new TextView(this);
col1.setText("col1");
col2.setText("col2");
col3.setText("col3");
row.addView(col1);
row.addView(col2);
row.addView(col3);
table.addView(row, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//HERE IS WHAT YOU NEED
TableRow.LayoutParams params = (TableRow.LayoutParams) row2.getLayoutParams();
params.span = 3; //amount of columns you will span
col1 = new TextView(this);
col1.setText("span on col1 makes it bigger than the others");
col1.setLayoutParams(params);
row2.addView(col1);
table.addView(row2, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Explaining the answer marked as correct - looks like you need to set layout params after adding the view to it's parent. for instance, add the textview to a row and then set layout params

Align components in TableRow

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

Categories

Resources