Weight of programmatically added elements in TableRow doesn't work - android

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

Related

Button disappears

I have next Layout (TOP: Two Spinner Half: TableLayout Down: Button) , I have filled the TableLayout with WebService with...
"for (int i = 0; i < ljsonArray.length(); i++)"
But when the last record of WebService show in the TableLayout, the button disappeared, what's wrong?
Regards
//Click botton
public void consultar(View v){
// Building Parameters to call WebService
List<NameValuePair> params = new ArrayList<NameValuePair>();
String url = "http://futcho7.com.mx/MiScore/WebService/gettablapos.php";
params.add(new BasicNameValuePair("id_cliente", numCte.get(0)));
params.add(new BasicNameValuePair("id_sucursal", numSuc.get(0)));
params.add(new BasicNameValuePair("id_torneo", Integer.toString(idTorneo)));
params.add(new BasicNameValuePair("id_jornada", Integer.toString(idJornada)));
new AsyncConector(this, url, params, this).execute();
}
Method to fill the TableLayOut
public void showTablaPos(){
TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
//Create table row header
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
//tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//Se crean los header
TextView label_equipo = new TextView(this);
label_equipo.setId(20);
label_equipo.setText("EQUIPO");
label_equipo.setTextColor(Color.WHITE);
label_equipo.setPadding(5, 5, 5, 5);
tr_head.addView(label_equipo);
TextView label_jj = new TextView(this);
label_jj.setId(20);
label_jj.setText("JJ");
label_jj.setTextColor(Color.WHITE);
label_jj.setPadding(5, 5, 5, 5);
tr_head.addView(label_jj);
TextView label_jg = new TextView(this);
label_jg.setId(20);
label_jg.setText("JG");
label_jg.setTextColor(Color.WHITE);
label_jg.setPadding(5, 5, 5, 5);
tr_head.addView(label_jg);
TextView label_je = new TextView(this);
label_je.setId(20);
label_je.setText("JE");
label_je.setTextColor(Color.WHITE);
label_je.setPadding(5, 5, 5, 5);
tr_head.addView(label_je);
TextView label_jp = new TextView(this);
label_jp.setId(20);
label_jp.setText("JP");
label_jp.setTextColor(Color.WHITE);
label_jp.setPadding(5, 5, 5, 5);
tr_head.addView(label_jp);
TextView label_gf = new TextView(this);
label_gf.setId(20);
label_gf.setText("GF");
label_gf.setTextColor(Color.WHITE);
label_gf.setPadding(5, 5, 5, 5);
tr_head.addView(label_gf);
TextView label_ge = new TextView(this);
label_ge.setId(20);
label_ge.setText("GE");
label_ge.setTextColor(Color.WHITE);
label_ge.setPadding(5, 5, 5, 5);
tr_head.addView(label_ge);
TextView label_dif = new TextView(this);
label_dif.setId(20);
label_dif.setText("DIF");
label_dif.setTextColor(Color.WHITE);
label_dif.setPadding(5, 5, 5, 5);
tr_head.addView(label_dif);
TextView label_ptos = new TextView(this);
label_ptos.setId(20);
label_ptos.setText("PTOS.");
label_ptos.setTextColor(Color.WHITE);
label_ptos.setPadding(5, 5, 5, 5);
tr_head.addView(label_ptos);
tablaPos.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Integer count = 0;
Integer cuantos = ljsonArray.length();
try{
for (int i = 0; i < ljsonArray.length(); i++) {
ljsonObject = ljsonArray.getJSONObject(i);
String equipo = (ljsonObject.optString("equ_nombre"));
Integer jj = (ljsonObject.optInt("jj"));
Integer jg = (ljsonObject.optInt("jg"));
Integer je = (ljsonObject.optInt("je"));
Integer jp = (ljsonObject.optInt("jp"));
Integer gf = (ljsonObject.optInt("gf"));
Integer ge = (ljsonObject.optInt("ge"));
Integer dif = (ljsonObject.optInt("dif"));
Integer pto = (ljsonObject.optInt("puntos"));
TableRow tr = new TableRow(this);
if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
tr.setId(100+count);
//tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//Create columns to add as table data
// Create a TextView to add equipo
TextView labelEquipo = new TextView(this);
labelEquipo.setId(200+count);
labelEquipo.setText(equipo);
labelEquipo.setPadding(2, 0, 5, 0);
labelEquipo.setTextColor(Color.WHITE);
tr.addView(labelEquipo);
TextView labelJj = new TextView(this);
labelJj.setId(200+count);
labelJj.setText(Integer.toString(jj));
labelJj.setPadding(2, 0, 5, 0);
labelJj.setTextColor(Color.WHITE);
labelJj.setGravity(Gravity.CENTER);
tr.addView(labelJj);
TextView labelJg = new TextView(this);
labelJg.setId(200+count);
labelJg.setText(Integer.toString(jg));
labelJg.setPadding(2, 0, 5, 0);
labelJg.setTextColor(Color.WHITE);
labelJg.setGravity(Gravity.CENTER);
tr.addView(labelJg);
TextView labelJe = new TextView(this);
labelJe.setId(200+count);
labelJe.setText(Integer.toString(je));
labelJe.setPadding(2, 0, 5, 0);
labelJe.setTextColor(Color.WHITE);
labelJe.setGravity(Gravity.CENTER);
tr.addView(labelJe);
TextView labelJp = new TextView(this);
labelJp.setId(200+count);
labelJp.setText(Integer.toString(jp));
labelJp.setPadding(2, 0, 5, 0);
labelJp.setTextColor(Color.WHITE);
labelJp.setGravity(Gravity.CENTER);
tr.addView(labelJp);
TextView labelGf = new TextView(this);
labelGf.setId(200+count);
labelGf.setText(Integer.toString(gf));
labelGf.setPadding(2, 0, 5, 0);
labelGf.setTextColor(Color.WHITE);
labelGf.setGravity(Gravity.CENTER);
tr.addView(labelGf);
TextView labelGe = new TextView(this);
labelGe.setId(200+count);
labelGe.setText(Integer.toString(ge));
labelGe.setPadding(2, 0, 5, 0);
labelGe.setTextColor(Color.WHITE);
labelGe.setGravity(Gravity.CENTER);
tr.addView(labelGe);
TextView labelDif = new TextView(this);
labelDif.setId(200+count);
labelDif.setText(Integer.toString(dif));
labelDif.setPadding(2, 0, 5, 0);
labelDif.setTextColor(Color.WHITE);
labelDif.setGravity(Gravity.CENTER);
tr.addView(labelDif);
TextView labelPtos = new TextView(this);
labelPtos.setId(200+count);
labelPtos.setText(Integer.toString(pto));
labelPtos.setPadding(2, 0, 5, 0);
labelPtos.setTextColor(Color.WHITE);
labelPtos.setGravity(Gravity.CENTER);
tr.addView(labelPtos);
// finally add this to the table row
tablaPos.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
count++;
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
XML LayOut
<?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:background="#color/verde"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1.14"
android:text="#string/tit_torneo"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/blanco"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.10"
android:text="#string/tit_jornada"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/blanco"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="#+id/torneo"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:prompt="#string/spinner_prompt_torneo" />
<Spinner
android:id="#+id/jornada"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.98" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="horizontal" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TableLayout
android:id="#+id/tabla_pos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>
<Button
android:id="#+id/consulta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/blanco"
android:onClick="consultar"
android:text="Consultar"
android:textColor="#color/negro"
android:textStyle="bold" />
</LinearLayout>
Your button is actually on the right of your tableLayout.
Change the orientation of the linearlayout to Vertical instead of horizontal.
Moreover the buttonView should be part of the scrollview if you want to see it at the end of the scrollview.
You can also nested all your xml in a Relative layout et add the attribute "android:layout_alignParentBottom="true" to the button and the button will always be visible.
Hope this helps.
EDIT-1:
Have you tried something like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:background="#color/verde"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1.14"
android:text="#string/tit_torneo"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/blanco"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.10"
android:text="#string/tit_jornada"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/blanco"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="#+id/torneo"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:prompt="#string/spinner_prompt_torneo" />
<Spinner
android:id="#+id/jornada"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.98" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="horizontal" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TableLayout
android:id="#+id/tabla_pos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/consulta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/blanco"
android:onClick="consultar"
android:text="Consultar"
android:textColor="#color/negro"
android:textStyle="bold"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
It works for me when I add rows directly in xml.

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.

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 dispaly result list into TableLayout row in android?

I have a little bit problem that displaying list data to TextView in Android. My scenario is I have a TableLayout with Default One TableRow. Inside table row, I has been created new LinearLayout. Then, Four TextView created inside LinearLayout. I adding some default value to this textview. My default value is
1, 2014-02-05, S02, 20
Here my code snippet for above scenari0.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/sync_history_table_color"
android:id="#+id/history_table"
android:orientation="vertical"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="1dp"
android:id="#+id/row_start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp" android:id="#+id/row_linear" android:layout_weight="1"
android:background="#color/isp_home_color">
<TextView android:layout_width="0dp" android:layout_height="match_parent" android:text="#string/sync_default_no"
android:id="#+id/history_display_no" android:layout_weight="0.165"
android:gravity="center_vertical|left" android:paddingLeft="10dp"
android:textColor="#color/sync_history_text_color"/>
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_date"
android:id="#+id/history_display_date"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_order"
android:id="#+id/history_display_orderid"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_qty"
android:id="#+id/history_display_quantity" android:layout_weight="0.5" android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
</LinearLayout>
</TableRow>
</TableLayout>
Actually, I want to create dynamically TableRow, LinearLayout and TextView. Then, I put the list result to add these. The list result come from sqlite db. But, I can't get what I want. Here my code for looping the list and display the result. But, I got my some default value. Please pointing to me how can I get this. Thanks.
Here snippet for display list.
public void displayHistory(){
List<IspHistoryListObject> historyList = sqlaccess.getAllItems();
TableLayout showRow = (TableLayout) findViewById(R.id.history_table);
int count = 0;
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(this);
TextView tv_sync_date = new TextView(this);
TextView tv_sync_orderid = new TextView(this);
TextView tv_sync_qty = new TextView(this);
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
}
try this way here I gave sample or demo code you have to modify as per your requirement and still have problem let me know
main layout
1. table.xml
<?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>
table row layout
2. 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/history_display_no"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_date"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_orderid"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_quantity"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
activity class
3. Activity
public class MyActivity extends Activity {
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
for (int i=0;i<5;i++){
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView history_display_no = (TextView) tableRow.findViewById(R.id.history_display_no);
TextView history_display_date = (TextView) tableRow.findViewById(R.id.history_display_date);
TextView history_display_orderid = (TextView) tableRow.findViewById(R.id.history_display_orderid);
TextView history_display_quantity = (TextView) tableRow.findViewById(R.id.history_display_quantity);
history_display_no.setText(""+(i+1));
history_display_date.setText("2014-02-05");
history_display_orderid.setText("S0"+(i+1));
history_display_quantity.setText(""+(20+(i+1)));
tableLayout.addView(tableRow);
}
}
}
check out context hierachy
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(showRow.getContext());
LinearLayout ll = new LinearLayout(tr.getContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(ll.getContext());
TextView tv_sync_date = new TextView(ll.getContext());
TextView tv_sync_orderid = new TextView(ll.getContext());
TextView tv_sync_qty = new TextView(ll.getContext());
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
With listview you can easily do this:
ActivityMain.java
ListView lst = (ListView) findViewById(R.id.listView);
View header = getLayoutInflater().inflate(R.layout.list_header, null);
lst.addHeaderView(header);
lst.setAdapter(new CustomerListAdapter(this,4));
list_header.xml is header layout

Setting margin to TextView makes it disappear

I'm a beginner in Android devt (started 3 days ago) and have a problem declaring a View's Layout. I have checked everywhere on how to set a TextView's margins programatically and so far, none of them work. The TextView always disappears when I apply the Layout.
Here's my code:
TableLayout tView = (TableLayout)findViewById(R.id.AllDocumentsTable);
TableRow trView = buildRow();
TextView tViewProjTitle = buildCell();
tViewProjTitle.setText(doc.project);
TextView tViewDocTitle = buildCell();
tViewDocTitle.setText(doc.document);
trView.addView(tViewProjTitle);
trView.addView(tViewDocTitle);
try {
tView.addView(trView, i);
}
catch (Exception e) {
Log.e("adding tablerow", e.getMessage());
}
buildRow()..
private TableRow buildRow(){
TableRow retRow = new TableRow(this);
retRow.setBackgroundColor(Color.WHITE);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
rowLayout.setMargins(2, 2, 2, 2);
retRow.setLayoutParams(rowLayout);
return retRow;
}
buildCell()..
private TextView buildCell(){
TextView retTView = new TextView(this);
retTView.setBackgroundColor(Color.WHITE);
retTView.setGravity(0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(2, 2, 2, 2);
retTView.setLayoutParams(params);
return retTView;
}
My Activity's layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TableLayout android:layout_height="wrap_content"
android:id="#+id/AllDocumentsTable"
android:layout_width="match_parent"
android:background="#ffffff">
<TableRow android:layout_margin="2dp"
android:background="#000000">
<TextView android:text="Test Text1." android:layout_margin="2dp" android:background="#ffffff"></TextView>
<TextView android:text="Test Text2" android:layout_margin="2dp" android:background="#ffffff"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Help!!! :)
you can set the margins like this:
LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)textview.getLayoutParams();
lp.topMargin=10;
lp.leftMargin=10;

Categories

Resources