how can i pin up the first row of a TableLayout? - android

I show a TableLayout with Data, the first row is the columns' name and since the second row is data, i need to pin up the first row but i have to be scrollable horizontally with the data(others rows) not vertically, but the data(others rows) just the data, have to be both(horizontally and vertically), because a i need to see always the columns' name.
I make my tablelayout like this:
TableLayout table= (TableLayout) findViewById(R.id.table);
//columns' name (first row)
TableRow column= new TableRow(this);
for(int i=0;i<lenght; i++){
TextView columnName = new TextView(this);
columnName.setText("Column "+i);
column.addView(columnName);
}
table.addView(column); //with this i add the first row with columns' name
//Data
for(int i=0;i<lenght; i++){
TableRow rowData = new TableRow(this);
TextView data= new TextView(this);
data.setText("data "+i);
rowData .addView(data);
table.addView(rowData );//add rows(since the second row)
}
With this structure i scroll all the tablelayout:
->ScrollView
->HorizontalScrollView
->TableLayout

I fix the header doing this :
<HorizontalScrollView>
<TableLayout>
<TableRow>
<TableLayout>
<--! Headers -->
</TableLayout>
</TableRow>
<TableRow>
<ScrollView>
<TableLayout>
<--! Rows/Data -->
</TableLayout>
</ScrollView>
</TableRow>
</TableLayout>
</HorizontalScrollView>
Obviously if you want it dynamic, you have to do the same but with programming.
Something like this
Your xml:
<HorizonalScrollView>
<TableLayout
android:"#+id/tableMaster">
</TableLayout>
</HorizonalScrollView>
-------------------------------------------------------------------------------------
Your jave code:
//it contains the TablesLayout
TableLayout tableFirst = (TableLayout) findViewById(R.id.tableMaster);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
//The Header
TableLayout tableSecond = new TableLayout(this);
TableRow rowHeader = new TableRow(this);
tableSecond.addView(rowHeader);
//The Data
ScrollView scroll = new ScrollView(this);//It makes this table scrollable vertically
TableLayout tableThird = new TableLayout(this);
TableRow rowData = new TableRow(this);
tableThird.addView(rowData);
scroll.addView(tableThird);
//Add the header
row1.addView(tableSecond);
tableFirst.addView(row1);
//Add the data scrollable
row2.addView(scroll);
tableFirst.addView(row2);
You have to put your own logic.

Related

Having difficulty in getting dynamically added table rows to show up

I am working on a screen that basically shows questions and answers in a grid format and I am putting them all into table rows dynamically. The problem I am having is that once I put in the first row (the header), the rest of the rows in the table do not show up. I first add the headers. This shows up.
protected void setupTableHeaders() {
TableRow tableRow = getNewTableRow();
tableRow.setTag(header);
TextView textView = new TextView(this);
GlobalVars.setupText(this,textView,32320); //Event
textView.setTextColor(R.color.black);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.setMarginStart(50);
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Iterator it = mapQuestions.entrySet().iterator();
//first get all the answers
if (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
HolderQuestion holderQuestion = mapQuestions.get(pair.getKey());
//assume all answers sets for all questions on this screen are the same.
int i = 0;
for (HolderAnswer answer : holderQuestion.getListAnswers()) {
TextView textViewHeader = new TextView(this);
textViewHeader.setTextColor(R.color.black);
GlobalVars.setupText(this, textViewHeader, answer.getTextID());
tableRow.addView(textViewHeader, (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .5f)));
if (i < holderQuestion.getListAnswers().size() - 1) {
tableRow.addView(getNewDivider(), (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .1f)));
}
i++;
}
}
table1.addView(tableRow);
}
Then I add the questions and answers, which do not show up:
protected void setupRadioButtonAnswers() {
for (int i=0;i<holderQuestionMultis.size();i++) { //assume this is populated correctly
TableRow tableRow = getNewTableRow();
tableRow.setTag(answerRow);
TextView textView = new TextView(this);
textView.setTextColor(R.color.black);
GlobalVars.setupText(this,textView,holderQuestionMultis.get(i).getHolderQuestion().getTextID());
textView.setTag(textCell);
textView.setVisibility(View.VISIBLE);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.leftMargin = 50;
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
for (int j=0;j<holderQuestionMultis.get(i).getRadioButtons().size();j++) {
RadioButton radioButton = holderQuestionMultis.get(i).getRadioButtons().get(j);
radioButton.setVisibility(View.VISIBLE);
tableRow.addView(radioButton,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.5f)));
if (j< holderQuestionMultis.size()-1) {
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
}
}
tableRow.setVisibility(View.VISIBLE);
table1.addView(tableRow);
}
}
And the two methods which get the radioButtons and table rows:
// table elements
protected TableRow getNewTableRow() {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tlparams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setBackground(getResources().getDrawable(R.drawable.border_table_multi_grid));
tableRow.setLayoutParams(tlparams);
return tableRow;
}
protected RadioButton getRadioButton() {
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(getResources().getDrawable(R.drawable.radio_button_multigrid));
//radioButton.setButtonDrawable(R.color.transparent);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
radioButton.setLayoutParams(params);
return radioButton;
}
The tablelayout xml:
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_above="#+id/viewSpacer"
android:layout_below="#+id/lblSubQuestionText"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/border_table_multi_grid">
What am I missing? Is the first table header row covering up the other rows? Why do they not appear?
It turns out that this code:
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Was the culprit which consisted of adding a LinearLayout containing a view. Just adding the view alone solved the problem.

How to add a row dynamically in a tableLayout in Android

I have a function who gets a list of products from a webpage, and I want to add a row in a tableLayout for each element of the list.
public void getProductsOfCategory() throws IOException{
new Thread(){
public void run(){
//background code...
try {
Bundle extras = getIntent().getExtras();
String categoryName = extras.getString("categoryName");
HttpGet httpget = new HttpGet("http://romal.hopto.org/foodadvisor/users/getProductsOfCategory.json?category="+categoryName);
HttpResponse httpresp = httpClient.execute(httpget);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
httpresp.getEntity().writeTo(baos);
final String content = new String(baos.toByteArray());
CategoryActivity.this.runOnUiThread(new Runnable(){
public void run(){
//foreground code (UI)
//update user interface, for example, showing messages
try {
JSONObject jObject = new JSONObject(content);
JSONArray productsList = jObject.getJSONArray("response");
for(int i=0; i<productsList.length();i++){
JSONObject product = productsList.getJSONObject(i);
JSONObject productData = product.getJSONObject("Product");
String productDescription = productData.getString("description");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
In my layout xml file I have defined the table layout like this:
<TableLayout
android:id="#+id/tableOfProducts"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/productDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:paddingBottom="7dp"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="7dp"
android:textSize="20dp" />
</TableLayout>
I imagine I have to add some extra code in the for loop, adding a new row and a new textview for each element, and setting the content of the text view with the string that has the description of the product.
How can I do this?
Check this out, this is the general way of creating table rows dynamically. Modify it accordingly
XML file
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_table"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TableLayout>
JAVA PART
TableLayout t1;
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
Create table row header to hold the column headings
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY); // part1
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
I'm adding two data sections to the table row
TextView label_hello = new TextView(this);
label_hello.setId(20);
label_hello.setText("HELLO");
label_hello.setTextColor(Color.WHITE); // part2
label_hello.setPadding(5, 5, 5, 5);
tr_head.addView(label_hello);// add the column to the table row here
TextView label_android = new TextView(this); // part3
label_android.setId(21);// define id that must be unique
label_android.setText("ANDROID..!!"); // set the text for the header
label_android.setTextColor(Color.WHITE); // set the color
label_android.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_android); // add the column to the table row here
After adding the columns to the table row its time to add the table row the the main table layout that we fetched at the start
tl.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, //part4
LayoutParams.MATCH_CONTENT));
EDIT : YOU CAN DO THE FOLLOWING IN YOUR CODE
TextView[] textArray = new TextView[productsList.length()];
TableRow[] tr_head = new TableRow[productsList.length()];
for(int i=0; i<productsList.length();i++){
JSONObject product = productsList.getJSONObject(i);
JSONObject productData = product.getJSONObject("Product");
String productDescription = productData.getString("description");
//Create the tablerows
tr_head[i] = new TableRow(this);
tr_head[i].setId(i+1);
tr_head[i].setBackgroundColor(Color.GRAY);
tr_head[i].setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Here create the TextView dynamically
textArray[i] = new TextView(this);
textArray[i].setId(i+111);
textArray[i].setText(productDescription);
textArray[i].setTextColor(Color.WHITE);
textArray[i].setPadding(5, 5, 5, 5);
tr_head[i].addView(textArray[i]);
// Add each table row to table layout
tl.addView(tr_head[i], new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
} // end of for loop
Creating the TextView and TableRow array are not necessary. You can just include part1, part2, part3 (if you need more than 1 field) and part4 inside your for loop.
Look, i think you have to modify your xml to adding rows to your tableview:
First, the inflater:
LayoutInflater inflater = mContext.getLayoutInflater();
or in this way:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Here you can set up the layoutparameters from your tableview and the rowview.
//Maybe you don't have to modify nothing from the parameters of your tableview so
//you can dismiss it.
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);
//Here were we take the tablelayout from your xml
TableLayout tableLayout = (TableLayout)inflater.inflate(R.layout.tableOfProducts, null);
//Like a told you before, maybe you don't need set the parameters of the tablelayout
//so you can comment next line.
tableLayout.setLayoutParams(this.tableParams);
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(this.tableParams);
//Here you have to create and modify the new textview.
TextView textView = new TextView(context);
textView.setLayoutParams(this.rowParams);
tableRow.addView(textView);
tableLayout.addView(tableRow);
If you need more help, tell me, if it is helpful, rating me!!! ;) Un saludo Gallega! O como se dice en gallego: una aperta!
for this, you have to create one xml file for row, then you have to modify your layout.xml file like
<ScrollView>
<LinearLayout orientation=vertical id="+id/rowHolder">
</LinearLayout
</scrollview>
Then in for loop, inflate the row layout and add it runtime to rowHolder object.
I'm new myself to Android, and after years with Python/Gtk, I'm lost if I can't think in terms of Model/View/Presenter.
one useful approach I managed to apply, it puts me back into that optics, and even if it's not relative to a Table, I'm quite sure it can be extended to that (I guess this page would be a good start, for me too).
start by declaring the two objects in your fragment (or activity):
List<String> stringList; // think of it as the model.
ArrayAdapter<String> listAdapter; // and this would be the presenter
define the model in the fragment constructor:
stringList = new ArrayList<>();
next, when the onCreateView of my fragment is invoked, I do this (edit correspondingly if you're using Activity, not Fragment):
View rootView = inflater.inflate(R.layout.fragment_taxonomy, container, false);
ListView mainListView = rootView.findViewById(R.id.taxonomy_results);
// Create ArrayAdapter using the string list. think of it as the presenter.
// see how it's being handled a model and a view.
listAdapter = new ArrayAdapter<>(
rootView.getContext(), R.layout.textrow, stringList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
textrow.xml is an extra layout which is being reused every time I add an element to the stringList model.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
in the rest of the code, I only "talk" to listAdapter, and I only need its two add and clear methods, to add elements, or for clearing the list.
There are many ways to do this. First, for minimizing your code, create an XML file that initializes the TableRow and its attribute (TextView should be also included). Then, in your target XML file that is linked to the activity, you have to create only the TableLayout.
In the main, create LayoutInflater that will link the items to gather. Then, create as much as you can of the number of TableRow. In the end, each TableRow that has a unique variable should be linked using addView() to the TableLayout that you have created in the XML.
For more, see here. https://github.com/AreejTurky/Dynamic-TableRow-Generation

How we can align one or more textviews one below to another in table row of table layout in android through java code not xml

I am consrtucting a registration form in table layout for which I have to add various textviews in a table row one one below another.
You can get something like this:
Here is the code, you can get the basic idea.
// <table>
TableLayout table = new TableLayout(context);
// <row1>
TableRow row = new TableRow(context);
// <column1>
LinearLayout container = new LinearLayout(context);
container.setBackgroundColor(Color.parseColor("#ff0000"));
container.setOrientation(LinearLayout.VERTICAL);
container.addView(getSampleTextView("Stacked TextView 1", "#ff0000"));
container.addView(getSampleTextView("Stacked TextView 2", "#ffff00"));
row.addView(container);
// </column1>
// <column2>
row.addView(getSampleTextView("TextView in Another Column", "#ffffff"));
// </column2>
table.addView(row);
// </row1>
// <row2>
TableRow row2 = new TableRow(context);
row2.setBackgroundColor(Color.parseColor("#00ffff"));
row2.addView(getSampleTextView("TextView in Next Row", "#0000ff"));
table.addView(row2);
// </row2>
Here is how I get sample text view for demo purposes:
private TextView getSampleTextView(String text, String color) {
TextView textView = new TextView(getContext());
setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText(text);
textView.setBackgroundColor(Color.parseColor(color));
return textView;
}

TableLayout related issue

I want to show an array of strings into table layout. Having following code.I am able to get the data into log file but table layout i am seeing nothing.
TableLayout tl=(TableLayout)findViewById(R.id.maintable);
for(int current=0;current<cursor1.getCount();current++)
{
//create a table row
TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
// create a text view for start time
TextView sTime= new TextView(this);
sTime.setText(values[current]);
sTime.setTextColor(Color.YELLOW);
Log.i("Data Check", values[current]);
sTime.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(sTime);
//add the table row into TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
I know this is long time after but it may help you or someone else.
Where you create the row and add layout params to it , use the following type of params
TableRow.LayoutParams NOT the TableLayout.LayoutParams

Addint textview to tableview dynamically

I want to populate the android tableview's rows with an array at runtime...
How to go about it....please advise
Thanks,
You can populate table view row's by reading tables child as follows:
TableLayout x = (TableLayout) findViewById(R.id.table_id);
for(int i=0;i<x.getChildCount();i++){
TableRow tr = (TableRow) x.getChildAt(index);
//Do here with table row, what u want to do.
}
TableLayout layout = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(context);
TextView view = new TextView(context);
view.setText("Test");
tr.addView(view);
layout.addView(tr);

Categories

Resources