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.
Related
Here i created 8 text views four for the heading and other four are displaying the content of the sql lite database.I don't know its the necessary for creating text view while executing dynamic text view creation.
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#66C2E0"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<EditText
android:id="#+id/dat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="false"
android:inputType="date" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:background="#drawable/button"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:stretchColumns="0,1,2,3"
android:id="#+id/mt"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/soi1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/woc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/desc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/amount1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/soi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/woc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
java class:
I created dynamic textview creation it doesn't display anything in activity not even display the heading,But i exeute using Log,it displays the exact output in logcat.
search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addheaders();
adddata();
}
});
}
protected void addheaders() {
// TODO Auto-generated method stub
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
soi1 = new TextView(this);
soi1.setText("Income Source");
soi1.setTextColor(Color.BLUE);
soi1.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
soi1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
soi1.setPadding(5, 5, 5, 0);
tr.addView(soi1); // Adding textView to tablerow.
/** Creating another textview **/
woc1 = new TextView(this);
woc1.setText("Cash Mode");
woc1.setTextColor(Color.BLUE);
woc1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
woc1.setPadding(5, 5, 5, 0);
woc1.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(woc1); // Adding textView to tablerow.
/** Creating another textview **/
desc1 = new TextView(this);
desc1.setText("Description");
desc1.setTextColor(Color.BLUE);
desc1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
desc1.setPadding(5, 5, 5, 0);
desc1.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(desc1); // Adding textView to tablerow.
/** Creating another textview **/
amount1 = new TextView(this);
amount1.setText("Cash Mode");
amount1.setTextColor(Color.BLUE);
amount1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
amount1.setPadding(5, 5, 5, 0);
amount1.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(amount1); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
protected void adddata() {
// TODO Auto-generated method stub
Database db=new Database(Reportdwr.this);
SQLiteDatabase sb=db.getReadableDatabase();
Cursor cus=sb.rawQuery("select * from income",null);
cus.moveToFirst();
for(int i=0;i<cus.getCount();i++)
{
if(cus.getString(2).toString().equalsIgnoreCase(dat.getText().toString()))
{
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
soi = new TextView(this);
soi.setText(cus.getString(0));
soi.setTextColor(Color.BLACK);
soi.setGravity(Gravity.CENTER);
soi.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
soi.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
soi.setTextSize(17);
//tr.setMargins(5, 5, 5, 5);
soi.setPadding(5, 5, 5, 5);
soi.setVisibility(View.VISIBLE);
tr.addView(soi);
Log.i("output",cus.getString(0));
woc = new TextView(this);
woc.setText(cus.getString(1));
woc.setTextColor(Color.BLACK);
woc.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
woc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
woc.setPadding(5, 5, 5, 5);
tr.addView(woc);
desc = new TextView(this);
desc.setText(cus.getString(4));
desc.setTextColor(Color.BLACK);
desc.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
desc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
desc.setPadding(5, 5, 5, 5);
tr.addView(desc);
amount = new TextView(this);
amount.setText(cus.getString(5));
amount.setTextColor(Color.BLACK);
amount.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
amount.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
amount.setPadding(5, 5, 5, 5);
tr.addView(amount);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
cus.moveToNext();
}
}
}
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);
}
Tried to align imageview to right.. code crashes. why?
UPDATED: Here is all row im having problem with. I think i failed on determining layouts somewhere here
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tl.addView(tableRow1);
/* textView1 */
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new TableRow.LayoutParams(
LayoutParam
s.MATCH_PARENT, LayoutParams.MATCH_PARENT));
textView1.setText(names);
tableRow1.addView(textView1);
ImageView icon;
icon = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);
// icon.setLayoutParams(params);
icon.setImageResource(R.drawable.ic_done); //default value
switch (value) {
case "1":
icon.setImageResource(R.drawable.ic_done);
break;
case "0":
icon.setImageResource(R.drawable.ic_bad);
default:
break;
}
System.out.println("value : " + value);
icon.setPadding(densityToPixels(13), 0, 0, 0);
tableRow1.addView(icon);
try in xml
<RelativeLayout
android:id="#+id/beam_contact_entry_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/beam_contact_entry_invite"
android:background="?entry_background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_toRightOf="#+id/contact_info_avatar" >
<TextView
android:id="#+id/beam_contact_fragment_entry_text"
style="?primary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alexander Great"
android:textSize="18sp" />
<TextView
android:id="#+id/beam_contact_fragment_entry_text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-4dp"
android:text="mobile"
android:visibility="gone"
style="?secondary_text"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/beam_contact_entry_invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/beam_contact_entry_contact"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/beam_contact_entry_contact"
android:background="?entry_background"
android:orientation="horizontal" >
<ImageView
android:id="#+id/beam_contact_fragment_entry_right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?entry_background"
android:duplicateParentState="true"
android:src="#drawable/sv_svyaznoy_contact" />
</LinearLayout>
First you defined the ImageView dynamically like
ImageView icon = new ImageView(this);
then you are trying to get the LayoutParam through icon.getLayoutParams(); which doesn't have any LayoutPram because you haven't set any.
To make it work replace
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)icon.getLayoutParams();
with
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);
As my Android apps are growing I need to include dynamic characteristics that include growing/reducing tables.
Therefore, I have switched from XML defined to programmatic methods.
My main XML, containing the basic table layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_layout_abonos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="top" >
</TableLayout>
</LinearLayout>
And then I try to define the header of the table using a TableRow that contains a set of titles for each table row by means of different TextView.
I have implemented this as follows:
public void añadeCabeceraTablaAbonos() {
TableLayout layoutTablaAbonados = (TableLayout) findViewById(R.id.table_layout_abonos);
int rowHeight = getStandardPixels(40);
TableRowTitulos cabeceraTablaAbonados = new TableRowTitulos(this);
cabeceraTablaAbonados.setId(100);
/*
* android:id="#+id/cultivo_auxiliares" android:layout_width="400dp"
* android:layout_height="40dp" android:layout_marginLeft="40dp"
* android:gravity="center"
* android:text="#string/cultivo_parcela_auxiliar"
* android:textAppearance="?android:attr/textAppearanceMedium"
*
* <TableRow android:layout_marginBottom="10dp"
* android:background="#color/rojo_dupont" >
*/
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, getStandardPixels(10));
cabeceraTablaAbonados.setLayoutParams(tableRowParams);
header_abonado_cultivo = new TextView(this);
header_abonado_abono = new TextView(this);
header_abonado_fecha = new TextView(this);
header_abonado_dosis = new TextView(this);
header_abonado_dosificacion = new TextView(this);
header_abonado_metodo = new TextView(this);
header_abonado_deposito_A = new TextView(this);
header_abonado_deposito_B = new TextView(this);
header_abonado_deposito_C = new TextView(this);
header_abonado_deposito_D = new TextView(this);
header_botonera = new TextView(this);
header_abonado_cultivo.setId(101);
header_abonado_cultivo.setText("CULTIVO");
header_abonado_cultivo.setGravity(Gravity.CENTER);
header_abonado_cultivo
.setWidth(getStandardPixels(ANCHO_COLUMNA_CULTIVO_ABONO));
//header_abonado_cultivo.setHeight(rowHeight);
header_abonado_cultivo.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_abono.setId(102);
header_abonado_abono.setText("ABONO");
header_abonado_abono.setGravity(Gravity.CENTER);
header_abonado_abono
.setWidth(getStandardPixels(ANCHO_COLUMNA_ABONO_ABONO));
//header_abonado_abono.setHeight(rowHeight);
header_abonado_abono.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_fecha.setId(103);
header_abonado_fecha.setText("FECHA");
header_abonado_fecha.setGravity(Gravity.CENTER);
header_abonado_fecha
.setWidth(getStandardPixels(ANCHO_COLUMNA_FECHA_ABONO));
//header_abonado_fecha.setHeight(rowHeight);
header_abonado_fecha.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_metodo.setId(104);
header_abonado_metodo.setText("MÉTODO");
header_abonado_metodo.setGravity(Gravity.CENTER);
header_abonado_metodo
.setWidth(getStandardPixels(ANCHO_COLUMNA_METODO_ABONO));
//header_abonado_metodo.setHeight(rowHeight);
header_abonado_metodo.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_dosis.setId(105);
header_abonado_dosis.setText("DÓSIS");
header_abonado_dosis.setGravity(Gravity.CENTER);
header_abonado_dosis
.setWidth(getStandardPixels(ANCHO_COLUMNA_DOSIS_ABONO));
//header_abonado_dosis.setHeight(rowHeight);
header_abonado_dosis.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_dosificacion.setId(106);
header_abonado_dosificacion.setText("DOSIFICACIÓN");
header_abonado_dosificacion.setGravity(Gravity.CENTER);
header_abonado_dosificacion
.setWidth(getStandardPixels(ANCHO_COLUMNA_DOSIFICACION_ABONO));
//header_abonado_dosificacion.setHeight(rowHeight);
header_abonado_dosificacion.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_A.setId(107);
header_abonado_deposito_A.setText("DEP. A");
header_abonado_deposito_A.setGravity(Gravity.CENTER);
header_abonado_deposito_A
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_A.setHeight(rowHeight);
header_abonado_deposito_A.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_B.setId(108);
header_abonado_deposito_B.setText("DEP. B");
header_abonado_deposito_B.setGravity(Gravity.CENTER);
header_abonado_deposito_B
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_B.setHeight(rowHeight);
header_abonado_deposito_B.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_C.setId(109);
header_abonado_deposito_C.setText("DEP. C");
header_abonado_deposito_C.setGravity(Gravity.CENTER);
header_abonado_deposito_C
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_C.setHeight(rowHeight);
header_abonado_deposito_C.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_D.setId(110);
header_abonado_deposito_D.setText("DEP. D");
header_abonado_deposito_D.setGravity(Gravity.CENTER);
header_abonado_deposito_D
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_D.setHeight(rowHeight);
header_abonado_deposito_D.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_botonera.setId(111);
header_botonera.setText("");
header_botonera.setGravity(Gravity.CENTER);
//header_botonera
// .setWidth(getStandardPixels(ANCHO_COLUMNA_BOTONERA_ABONO));
//header_botonera.setHeight(rowHeight);
header_botonera.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
cabeceraTablaAbonados.addView(header_abonado_cultivo);
cabeceraTablaAbonados.addView(header_abonado_abono);
cabeceraTablaAbonados.addView(header_abonado_fecha);
cabeceraTablaAbonados.addView(header_abonado_metodo);
cabeceraTablaAbonados.addView(header_abonado_dosis);
cabeceraTablaAbonados.addView(header_abonado_dosificacion);
cabeceraTablaAbonados.addView(header_abonado_deposito_A);
cabeceraTablaAbonados.addView(header_abonado_deposito_B);
cabeceraTablaAbonados.addView(header_abonado_deposito_C);
cabeceraTablaAbonados.addView(header_abonado_deposito_D);
cabeceraTablaAbonados.addView(header_botonera);
//abonadoLayout
layoutTablaAbonados.addView(cabeceraTablaAbonados);
// TableRow.LayoutParams buttonParams = (TableRow.LayoutParams)header_abonado_cultivo.getLayoutParams();
// buttonParams.setMargins(15, 10, 10, 10);
}
Nevertheless, I am not able to make:
a) The header having a specific HEIGHT. I have tried replacing:
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
with
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
100); // For 100 pixels height
But the row remains the "standard" height that just wraps content.
Also, I have a similar issue with data rows (non-header) that are composed of a series of Buttons.
These buttons text will change dinamically and will stretch vertically to adapt to such string.
Nevertheless, the neighbour buttons won't stretch accordingly and so the aspect is very ugly with some buttons being taller than other ones (I won't paste more code on this as it is very similar to the other problem).
Here is an equivalent XML that does it correctly (grows all buttons accordingly to the taller one on the row):
(FIRST TABLE ROW IS HEADER AND NEXT ARE DATA)
<ScrollView
android:id="#+id/tab6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="top" >
<TableRow
android:layout_marginBottom="10dp"
android:background="#color/rojo_dupont" >
<TextView
android:id="#+id/cultivo_auxiliares"
android:layout_width="400dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/cultivo_parcela_auxiliar"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/nombre_plaga_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/nombre_plaga_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/nombre_insecto_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/nombre_insecto_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/fecha_suelta_label"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/fecha_suelta_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/cantidad_insecto_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/cantidad_insecto_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_marginBottom="5dp" >
<Button
android:id="#+id/cultivo_parcela_0"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#8ad5f0" />
<Button
android:id="#+id/nombre_plaga_0"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#8ad5f0" />
<Button
android:id="#+id/nombre_insecto_0"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint="" />
<Button
android:id="#+id/fecha_suelta_0"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#8ad5f0" />
<EditText
android:id="#+id/cantidad_insecto_0"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint=""
android:inputType="number" />
<ImageButton
android:id="#+id/insecto_0_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:contentDescription="#string/delete_content_description"
android:src="#drawable/discard_dark" />
<ImageButton
android:id="#+id/insecto_0_comentario"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:contentDescription="#string/delete_content_description"
android:src="#drawable/ic_dialog_alert_holo_dark" />
</TableRow>
MORE ROWS FOLLOW WITH THE SAME SCHEMA
...
I suppose I'm doing something wrong with layouts but I cannot figure it out even after reading a lot about it.
Could someone guide me with this or provide some working code?
Thanks in advance,
Jose.
Well, it seems I found the solution !!. The main problem was that I was selecting the wrong Layout for the Views inside the row.
Conceptually, the row must have a height that is WRAP_CONTENT so it will grow dynamically with the highest View it contains. Also, each View in the row must have its hight put to MATCH_PARENT, so they will grow as the row does. This is not very intuitive for me but it is it.
So here is a portion of the code that solved the problem for me, hope this can help someone:
TableRowAbonados nuevaFila = new TableRowAbonados(this);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin = 0;
int topMargin = 0;
int rightMargin = 0;
int bottomMargin = getStandardPixels(5);
tableRowParams.setMargins(leftMargin, topMargin, rightMargin,
bottomMargin);
nuevaFila.setLayoutParams(tableRowParams);
Button botonAbono = new Button(this);
Button botonFecha = new Button(this);
Button botonMetodo = new Button(this);
EditText celdaDosis = new EditText(this);
celdaDosis.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
nuevaFila.addView(botonAbono);
nuevaFila.addView(botonFecha);
nuevaFila.addView(botonMetodo);
nuevaFila.addView(celdaDosis);
botonAbono.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
botonFecha.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
botonMetodo.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
celdaDosis.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
layoutTablaAbonados.addView(nuevaFila);
When I create EditText programmatically I have no underline (in the second EditText). Like this: http://oi41.tinypic.com/2ut427d.jpg
When I create an EditText in XML everything is fine, see http://oi44.tinypic.com/nmxdnm.jpg. Where is the error?
This is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<include layout="#layout/input_title_layout" />
<TableLayout
android:id="#+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":" />
<EditText
android:id="#+id/diameterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:digits="0123456780."
android:inputType="numberDecimal" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
And code to add EditText programatically:
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams.bottomMargin = 0;
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
inputLinearLayout.addView(tableLayout);
TableRow tableRowEdit = new TableRow(context);
TextView titleTv = new TextView(context);
titleTv.setTextAppearance(context, R.style.tvTitleStyle);
titleTv.setText(context.getString(context.getResources().getIdentifier(inputTvs[i] , "string", context.getPackageName())));
TextView separatorTv = new TextView(context);
separatorTv.setGravity(Gravity.LEFT);
separatorTv.setPadding(5, 0, 5, 0);
separatorTv.setText(":");
separatorTv.setTextColor(Color.BLACK);
EditText editText = new EditText(context);
editText.setId(i);
tableRowEdit.addView(titleTv);
tableRowEdit.addView(separatorTv);
tableRowEdit.addView(editText);
tableLayout.addView(tableRowEdit);
It's hard to tell exactly what the issue might be given the code you supplied.
-Have you tried setting the layout parameters of the TableRow/TextView/EditText? The TextViews might be taking up all the visible space.
-Are you sure inputLinearLayout has been inflated?