Android TableLayout isn't displaying rows - android

I am having problems getting my TableLayout to display my rows.
I'm propagating the rows programmatically via this method:
private void buildStatesTable() {
Log.d(SelectStatesPanel.class.getCanonicalName(), "Entering Build States Table");
ArrayList<String> states = new ArrayList<String>();
Random rand = new Random();
for(int i = 0; i < 10; i++){
if(i < goodStates.length){
states.add(goodStates[i]);
}else{
states.add(badStates[rand.nextInt(badStates.length)]);
}
}
Collections.shuffle(states);
TableLayout tl = (TableLayout) findViewById(R.id.statesTable);
final int NUM_PER_ROW = 2;
for(int i = 0; i < (states.size() / NUM_PER_ROW); i++){
TableRow tr = new TableRow(getContext());
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for(int j = 0; j < NUM_PER_ROW && (i*NUM_PER_ROW + j) < states.size(); j++){
TextView lblState = new TextView(getContext());
lblState.setText(states.get(i*NUM_PER_ROW + j));
lblState.setTextColor(Color.BLACK);
lblState.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
lblState.setClickable(true);
lblState.setOnClickListener(this);
tr.addView(lblState);
//Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding State: " + states.get(i*NUM_PER_ROW + j));
Log.d(SelectStatesPanel.class.getCanonicalName(), "\t\t\t (" + i + ", " + j + ")");
}
// Add the TableRow to the TableLayout
tl.addView(tr);
Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding Row");
}
}
And this is my XML (table layout is at the bottom):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Good choice! There's hope for you yet."
android:textSize="16dip"
android:textStyle="bold"
android:gravity="center"
/>
<TextView
android:id="#+id/txtLex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Lexington"
android:textSize="14dip"
android:gravity="center"
/>
<TextView
android:id="#+id/txtTampa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Tampa"
android:textSize="14dip"
android:gravity="center"
/>
<TextView
android:id="#+id/txtSacramento"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Sacramento"
android:textSize="14dip"
android:gravity="center"
/>
<TextView
android:id="#+id/txtHiltonhead"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Hiltonhead"
android:textSize="14dip"
android:gravity="center"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dip"
android:gravity="center"
android:src="#drawable/usa"
/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dip"
android:id="#+id/statesTable"
android:stretchColumns="0,1">
</TableLayout>
</LinearLayout>
Why isn't my table layout's showing content?

I can't see specifically what is going wrong in your code, but here is a similar implementation in my own code:
public class EconFragment extends Fragment {
private TableLayout questionContainer;
static int pos = 0;
private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
"hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("Econ", "onCreateView");
return inflater.inflate(R.layout.econfragment, container, false);
}
public void onResume() {
super.onResume();
LayoutInflater inflater = (LayoutInflater) getActivity().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
questionContainer = (TableLayout) getActivity().findViewById(R.id.questionContainer);
//these lines are my first attempt to try and get the questions to repopulate after returning
//from pressing back - as of yet, they don't repopulate the screen:
//View econFragment = inflater.inflate(R.layout.econfragment, null);
//container.addView(econFragment);
int leftMargin=5;
int topMargin=5;
int rightMargin=5;
int bottomMargin=5;
while (pos < 10) {
View question = inflater.inflate(R.layout.question, null);
question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.questionTextView);
title.setText(titles[pos]);
Button charts = (Button) question.findViewById(R.id.chartsButton);
charts.setId(pos);
charts.setOnClickListener(chartsListener);
TableRow tr = (TableRow) question;
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
questionContainer.addView(tr);
pos++;
}
Log.d("Econ", "onResume");
}
And the source of questionContainer, econfragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/questionContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
As you can see, a Question.class object is created as a View object in the EconFragment fragment. question object is used to findViewById for each needed portion.

Comment out the whole row code and replace it with a two liner textview row. Does that work? Then the array from which you build your row views is maybe the problem.
Best regards.

I discovered the problem on my own.
The problem was this:
lblState.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Specifying TableRow.LayoutParams fixed the problem.

Related

Displaying SQLite data into TableRows

I am creating an app that would display data from my SQLite database, though I have been successful in displaying the data in Custom ListViews. But I am wondering how would I be able to display data in a Table like form, I can display the first line of data though I don't know where to go from here.
This is my ActivityMain.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gin.customlistview.MainActivity">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVED MESSAGES"
android:textSize="20dp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<TableRow
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_margin="1dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Phone Number"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Username"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Account Number"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
android:layout_column="2"
/>
</TableRow>
<TableRow
android:id="#+id/tableRow"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<TextView
android:id="#+id/txtPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:background="#FFFFFF"
android:gravity="center"
android:layout_margin="1dp" />
<TextView
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
/>
<TextView
android:id="#+id/txtAccountNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center" />
</TableRow>
</TableLayout>
</LinearLayout>
This is my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new textDatabase(this);
txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
txtUsername = (TextView) findViewById(R.id.txtUsername);
txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);
checkPermission();
showData();
}
public static void showData() {
// databaseNumber.clear();
// databaseUsername.clear();
// databaseAccountNumber.clear();
Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
data.moveToFirst();
do {
txtPhoneNumber.setText(data.getString(1));
txtUsername.setText(data.getString(2));
txtAccountNumber.setText(data.getString(3));
// databaseNumber.add(data.getString(1));
// databaseUsername.add(data.getString(2));
// databaseAccountNumber.add(data.getString(3));
} while (data.moveToNext());
}
data.close();
// adapters.notifyDataSetChanged();
}
Thanks in advance for any help and suggestions! :D
try something like this
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
Once You have Created the tableLayout Create a table row layout table_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/title"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
inside your activity iterate through the values retrieved from the sql and add it to the table
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
data.moveToFirst();
do {
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView name = (TextView) tableRow.findViewById(R.id.name);
TextView title = (TextView) tableRow.findViewById(R.id.title);
name.setText(data.getString(1));
title.setText(data.getString(2));
tableLayout.addView(tableRow);
} while (data.moveToNext());
data.close();
}
Use below code , rows will be created dynamically according to the data , result will be something similar to this
LinearLayout table = (LinearLayout) findViewById(R.id.table);
ArrayList<Tabledata> datalist = new ArrayList<>();
datalist = db.getyourSQLITEdata();
// this method for table title
createtitle(table);
for(int i=0;i<datalist.size();i++){
// this method is creating table rows and filling data
Createtable(table,i);
}
}
table title
// your table title here (if you want title for your table you can use this)
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void createtitle(LinearLayout main){
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
// your table title here
String[] title={"title1","title2","title3","title4","title5","title6","title7"};
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(title[i]);
col1.setTextSize(13);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
table
// your data table is created here
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void Createtable(LinearLayout main,int pos) {
Tabledata data = datalist.get(pos);
// this is where you get data from list and show it in table
String[] yourdata = {data.getdata1(),data.getdata2(),data.getdata3(),data.getdata4(),data.getdata5(),data.getdata6(),data.getdata7()};
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(yourdata[i]);
col1.setTextSize(10);
col1.setLines(1);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
Xml code
<LinearLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
</LinearLayout>
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new textDatabase(this);
txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
txtUsername = (TextView) findViewById(R.id.txtUsername);
txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);
//table layout
tableLayout = (TableLayout) findViewById(R.id.table);
checkPermission();
showData();
}
public void showData() {
TextView phn_no,user_name,account_no;
// databaseNumber.clear();
// databaseUsername.clear();
// databaseAccountNumber.clear();
Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
data.moveToFirst();
do {
phn_no=new TextView(this);
user_name=new TextView(this);
account_no=new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
params.setMargins(1,1,1,1);
params.gravity= Gravity.CENTER;
phn_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
phn_no.setTextColor(Color.parseColor("#000"));
phn_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
phn_no.setLayoutParams(params);
phn_no.setText(data.getString(1));
user_name.setBackgroundColor(Color.parseColor("#FFFFFF"));
user_name.setTextColor(Color.parseColor("#000"));
user_name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
user_name.setLayoutParams(params);
user_name.setText(data.getString(2));
account_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
account_no.setTextColor(Color.parseColor("#000"));
account_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
account_no.setLayoutParams(params);
account_no.setText(data.getString(3));
TableRow row = new TableRow(this);
row.addView(phn_no);
row.addView(user_name);
row.addView(account_no);
tableLayout.addView(row);
txtPhoneNumber.setText(data.getString(1));
txtUsername.setText(data.getString(2));
txtAccountNumber.setText(data.getString(3));
// databaseNumber.add(data.getString(1));
// databaseUsername.add(data.getString(2));
// databaseAccountNumber.add(data.getString(3));
} while (data.moveToNext());
}
data.close();
// adapters.notifyDataSetChanged();
}
}
You can use this
You can use a custom listview or Recyclerview which is more or less like a listview, it's just an advanced listview to be precise. You can use this documentation to understand recyclerviews https://developer.android.com/training/material/lists-cards.html

Problems with column width in Android Table

I'm trying to build a table that will let me list results from a search. I want the two columns to be different widths (e.g. 30% and 70%). I have got this working for the column titles:-
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView1"
android:layout_marginTop="10dip"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*"
android:textColor="#color/white" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<TextView
android:id="#+id/TextViewTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Date"
android:background="#drawable/border"
android:textColor="#color/bluish" />
<TextView
android:id="#+id/TextViewTitle2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Task"
android:background="#drawable/border"
android:textColor="#color/bluish" />
</TableRow>
</TableLayout>
I then want to add rows to the table in my program where the columns have the same width as the title row, but my code generates rows where the columns are split 50%-50% on width:-
private void BuildTable(int rows, int cols) {
// outer for loop
for (int i = 1; i <= rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 1; j <= cols; j++) {
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setBackgroundResource(R.drawable.border);
tv1.setPadding(5, 5, 5, 5);
tv1.setText("R " + i + ", C" + j);
row.addView(tv1);
}
table_layout.addView(row);
}
}
I'd appreciate any help on getting the column width to the same as the title.
private void addRow(Data data){
TableRow row = (TableRow)View.inflate(getApplicationContext(), your table row , null);
//find your textviews and set data
(TextView)row.findViewById(R.id.TextViewTitlei1);
}
}
they will be distributed according to your required weight.
Well I've worked out what I needed to do this. Firstly set up a layout file, which I called table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tr_row"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="1" >
<TextView
android:id="#+id/TextViewTitlei1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text=" "
android:background="#drawable/border"
android:textColor="#color/bluish" />
<TextView
android:id="#+id/TextViewTitlei2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text=" "
android:background="#drawable/border"
android:textColor="#color/bluish" />
</TableRow>
Then this is a simplified version of the code to make column widths 30% and 70%
private void addTableRow(String resIdTitle, String strValue){
int yy=4;
for (int i = 1; i <= yy; i++) {
System.out.println("Step 16");
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, table_layout, false);
System.out.println("Step 17");
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.TextViewTitlei1);
tvTitle.setText(resIdTitle+ " "+i);
System.out.println("Step 18");
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.TextViewTitlei2);
tvValue.setText(strValue+ " "+i);
System.out.println("Step 19");
table_layout.addView(tr);
}
}
That appears to work.

Weight of programmatically added elements in TableRow doesn't work

So, I have a TableLayout with 4 TableRow in one xml file. I also have a xml file defining a TextView that will be inserted in the row (3 TextView per row).
I'd like those TextView to have the same width and that was working well when everything was in the same xml file (at the begining my TextView were "hard-coded") but now that I add them programmatically, they don't have the same width.
Here is the first XML file
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/homeTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/lmbg"
android:clickable="true"
android:padding="20dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
</TableLayout>
This is my TextView item
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:onClick="goToList"
android:textSize="14sp" />
And finally this is how I add them programmatically
for(int i = 0; i < mTopCategories.size(); i++){
Category c = mTopCategories.get(i);
TableRow row = ((TableRow)findViewById(rowID[idIdx])) ;
cat = (TextView) getLayoutInflater().inflate(R.layout.cat_grid_item, null);
int iconID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "drawable", getPackageName());
cat.setCompoundDrawablesWithIntrinsicBounds(0, iconID, 0, 0);
int textID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "string", getPackageName());
cat.setText(textID);
cat.setTag(c.getId().toString());
row.addView(cat);
}
Try explicitly setting the layout params on the TextViews like this:
TableRow.LayoutParams lParams = new TableRow.LayoutParams(0, -2 /* WRAP_CONTENT */, 0.33f /* 33% of width */);
cat.setLayoutParams(lParams);
See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#TableRow.LayoutParams%28int,%20int,%20float%29 for more info.
Try this way,you even no required any xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
int id=1;
for (int i=0;i<4;i++){
TableRow tableRow = new TableRow(this);
tableRow.setOrientation(TableLayout.HORIZONTAL);
for(int j=0;j<3;j++){
TextView textView = new TextView(this);
textView.setGravity(Gravity.CENTER);
textView.setId(id++);
textView.setText(String.valueOf(textView.getId()));
textView.setTag(textView.getId());
textView.setTextColor(Color.WHITE);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Id is : " + v.getId(), Toast.LENGTH_SHORT).show();
}
});
textView.setBackgroundColor(Color.BLUE);
TableRow.LayoutParams textviewParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT,1f);
textviewParams.setMargins(5, 5, 5, 5);
tableRow.addView(textView,textviewParams);
}
table.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,0,1f));
}
setContentView(table);
}

GridView with different number of columns according to the number of row

I am trying this for 2 days,I am trying to make UIs for the view having two columns in first row,then in second row having three columns,and in third again two columns and so on..And also I have to implement dynamic data(means num of rows/columns are dynamic).
I don't have any idea to implement this type of view.I have also try stragged grid view but this is for dynamic views..but in this I have static 2,3,2,3... columns for 1,2,3,4..rows.Please help me.Any tutorial will be most helpful.
Finally I have solved this problem.I have tried with Raghunandan and user3278416's suggestion.And did all the stuff in runtime except Table layout.Here is the xml file:
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
>
And the the two layouts one is for small layout(the second row) and one is for large one(first row):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="130dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_offerrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
The small one:
<RelativeLayout
android:layout_width="90dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_homesmallrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
Now the Java Code:
int leftMargin_small=2;
int topMargin_small=5;
int rightMargin_small=2;
int bottomMargin_small=5;
int j = 0;
while (Show.size()>j) {
try {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow.LayoutParams param = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
param.setMargins(leftMargin_small, topMargin_small, rightMargin_small, bottomMargin_small);
LinearLayout layout1 = new LinearLayout(getApplicationContext());
TableRow.LayoutParams param_layout = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
layout1.setLayoutParams(param_layout);
View question = inflater.inflate(R.layout.offers_rowlayout, null);
question.setLayoutParams(param);
//question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.txt_title_offerrowlayout);
title.setText(""+">>"+Show.get(j).get("name"));
View question1 = inflater.inflate(R.layout.offers_rowlayout, null);
//question.setId(pos);
question1.setLayoutParams(param);
TextView title1 = (TextView) question1.findViewById(R.id.txt_title_offerrowlayout);
title1.setText(""+">>"+Show.get(j+1).get("name"));
View question_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question_small.setLayoutParams(param);
TextView title_small = (TextView) question_small.findViewById(R.id.txt_title_homesmallrowlayout);
title_small.setText(""+">>"+Show.get(j).get("name"));
View question1_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question1_small.setLayoutParams(param);
TextView title1_small = (TextView) question1_small.findViewById(R.id.txt_title_homesmallrowlayout);
title1_small.setText(""+">>"+Show.get(j+1).get("name"));
View question2_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question2_small.setLayoutParams(param);
TextView title2_small = (TextView) question2_small.findViewById(R.id.txt_title_homesmallrowlayout);
title2_small.setText(""+">>"+Show.get(j+2).get("name"));
if (gett) {
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
// trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
layout1.addView(question);
layout1.addView(question1);
tr.addView(layout1);
tablee.addView(tr);
gett = false;
j = j+2;
}
else
{
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
layout1.addView(question_small);
layout1.addView(question1_small);
layout1.addView(question2_small);
tr.addView(layout1);
tablee.addView(tr);
gett = true;
j = j+3;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Here Show is the arraylist of hashmap type and tablee is the tablelayout object which I have made in Xml file.

How to display a list of entries programmatically using TableLayout in android

I'm using a web service to fetch data that is to be shown as a list if entries. I have four attributes which are in a 2X2 table layout format in my xml file. Then I use TableLayout class in my java code to store the result in each TableRow.
Here's my xml code: tabrow.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/attrib_value2"
style="#style/LeftHeaderText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="TextView"
android:textColor="#color/textColor" />
<TextView
android:id="#+id/attrib_value5"
style="#style/HourText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="TextView"
android:textColor="#color/textColor" android:gravity="right">
</TextView>
</TableRow>
<TableRow android:id="#+id/tableRow3"
style="#style/BodyRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/attrib_value3"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="TextView"
android:textColor="#color/textColor" >
</TextView>
<TextView
android:id="#+id/attrib_value4"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="TextView"
android:textColor="#color/textColor" >
</TextView>
</TableRow>
</TableLayout>
Here's the another xml code inside which the tabrow.xml will be called to add each entry.
<TableLayout
android:id="#+id/resultDetail"
android:layout_width="fill_parent"
style="#style/BodyRow"
android:layout_height="wrap_content" >
</TableLayout>
And following is the java code.
public void fillWorkEntries(ArrayList<WorkEntry> workEntries) {
try {
TableLayout table = (TableLayout) WorkEntryScreenActivity.this
.findViewById(R.id.resultDetail);
table.removeAllViews();
for (WorkEntry workEntry : workEntries) {
TableLayout nestedtable = (TableLayout) LayoutInflater.from(this).inflate(
R.layout.tabrow, null);
TableRow bodyRow = (TableRow) LayoutInflater.from(this).inflate(
R.id.tableRow2, null);
((TextView) bodyRow.findViewById(R.id.attrib_value2))
.setText(workEntry.getWorkRequestName());
((TextView) bodyRow.findViewById(R.id.attrib_value5))
.setText(workEntry.getActHrs());
TableRow bodyRownext = (TableRow) LayoutInflater.from(this).inflate(
R.id.tableRow3, null);
((TextView) bodyRownext.findViewById(R.id.attrib_value3))
.setText(workEntry.getActivity());
((TextView) bodyRownext.findViewById(R.id.attrib_value4))
.setText(workEntry.getWorkEntryDesc());
table.addView(nestedtable);
}
Log.d(TAG, "Load Entries");
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
}
}
It used to work before when I have only a 'tablerow' in my xml file which I iteratively called to fetch all four entries one by one. But to save some screen space I tried to insert a 'tableyalout' with two rows and it stopped working. What am I doing wrong here? Any kinda help is appreciated.
Try this code out of curiosity.
public void fillWorkEntries(ArrayList<WorkEntry> workEntries) {
try {
TableLayout table = (TableLayout)WorkEntryScreenActivity.this.findViewById(R.id.resultDetail);
//table.removeAllViews();
for (WorkEntry workEntry : workEntries) {
TableLayout nestedtable = (TableLayout) LayoutInflater.from(this).inflate(R.layout.tabrow, null);
TableRow bodyRow = (TableRow)nestedtablefindViewById(R.id.tableRow2, null);
((TextView) nestedtable.findViewById(R.id.attrib_value2)).setText(workEntry.getWorkRequestName());
((TextView) bodyRow.findViewById(R.id.attrib_value5)).setText(workEntry.getActHrs());
//TableRow bodyRownext = (TableRow) LayoutInflater.from(this).inflate(R.id.tableRow3, null);
((TextView) nestedtable.findViewById(R.id.attrib_value3)).setText(workEntry.getActivity());
((TextView) nestedtable.findViewById(R.id.attrib_value4)).setText(workEntry.getWorkEntryDesc());
table.addView(nestedtable);
}
Log.d(TAG, "Load Entries");
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
}
}
You shouldnt need to inflate your rows, as they are already part of the nested table object you have inflated
The parent View of the layout R.layout.tabrow is a TableLayout and not a TableRow as you cast it so this line:
TableRow bodyRow = (TableRow) LayoutInflater.from(this).inflate(
R.layout.tabrow, null);
will throw a cast exception. Instead it should be like this:
TableLayout bodyRow = (TableLayout) LayoutInflater.from(this).inflate(
R.layout.tabrow, null);
((TextView) bodyRow.findViewById(R.id.attrib_value2))
.setText(workEntry.getWorkRequestName());
((TextView) bodyRow.findViewById(R.id.attrib_value5))
.setText(workEntry.getActHrs());
((TextView) bodyRownext.findViewById(R.id.attrib_value3))
.setText(workEntry.getActivity());
((TextView) bodyRownext.findViewById(R.id.attrib_value4))
.setText(workEntry.getWorkEntryDesc());
TableRow tr = new TableRow(this);
TableRow.LayoutParams p = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
bodyRow.setlayoutParams(p);
tr.addView(bodyRow);
TableLayout.LayoutParams p1 = new TableTableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(p1);
table.addView(tr);

Categories

Resources