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();
}
}
}
Related
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.
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);
In my android app, I have a table layout. I want to dynamically insert rows. But the problem is the rows are being added horizontally. I want each tablerow to be stacked vertically. I have this so far:
TableLayout mytable = (TableLayout)findViewById(R.id.studentContainer);
TableRow tablerow = new TableRow(this);
tablerow.setOrientation(TableRow.VERTICAL);
EditText g = new EditText(this);
g.setText("AAAAAAAAA");
g.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText g2 = new EditText(this);
g2.setText("BBBBBBBBBB");
g2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tablerow.addView(g);
tablerow.addView(g2);
mytable.addView(tablerow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
TableRow tablerow2 = new TableRow(this);
tablerow2.setOrientation(TableRow.VERTICAL);
EditText g4 = new EditText(this);
g4.setText("AAAAAAAAA");
g4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText g5 = new EditText(this);
g5.setText("BBBBBBBBBB");
g5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tablerow.addView(g4);
tablerow.addView(g5);
mytable.addView(tablerow2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
XML
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/studentContainer" >
</TableLayout >
Does anyone know whats wrong here?
Thanks
Try to put the first row in the xml and rotate the entire table, edit the text_views if necessary. Like this
XML
<TableLayout
android:id="#+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:rotation="-90"
android:stretchColumns="0,1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column1"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column2"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column3"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column4"
android:textColor="#000000"/>
</TableRow>
</TableLayout>
and then code like this
TableRow tr = new TableRow(this);
tr.setPadding(0,20,0,20);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
TextView tv_item4 = new TextView(this);
tv_item1.setText(M_Number);
tv_item1.setGravity(Gravity.CENTER);
tv_item2.setText(M_Number);
tv_item2.setGravity(Gravity.CENTER);
tv_item3.setText(M_Number);
tv_item3.setGravity(Gravity.CENTER);
tv_item4.setText(M_Number);
tv_item4.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
tr.addView(tv_item4);
Table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Better you have a table row item in XML, inflate in as a new view and set data accordingly.
then add that view to the table layout
I'm trying to add table rows programmatically following the code here
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
But but nothing is drawn to the screen. (same as one mentioned here)
When i added tr.setBackgroundResource(R.drawable.sf_gradient_03);, the row with the background image is drawn but not the button
Here is my Layout
<!-- Sale Order Lines START -->
<LinearLayout android:id="#+id/SaleOrderLinesArea" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dip" android:background="#drawable/sf_gradient_11" >
<LinearLayout android:id="#+id/subHeaderArea" android:padding="10dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="#+id/screenTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#color/title_sub" android:textStyle="bold"
android:text="Order Lines" />
</LinearLayout>
<TableLayout android:id="#+id/SaleOrderLines"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dip" android:stretchColumns="*">
<TableRow
android:background="#drawable/sf_gradient_13" android:padding="10dip"
>
<TextView android:id="#+id/order_ref_label"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#color/fg_prime" android:text="bLA bLA" />
<TextView android:id="#+id/product_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:textColor="#color/fg_title"
android:text="#string/product" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:textColor="#color/fg_title"
android:text="#string/product_quantity" />
</TableRow>
<TableRow android:background="#drawable/sf_gradient_03"
android:paddingLeft="10dip" android:paddingRight="10dip"
>
<TextView android:id="#+id/order_ref_label"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#color/fg_prime" android:text="Fooo" />
<Spinner android:id="#+id/product_spinner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:prompt="#string/customer_prompt">
</Spinner>
<EditText android:id="#+id/product_uom_qty"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:fadingEdge="horizontal" />
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingTop="5dip" android:paddingBottom="5dip">
<Button android:id="#+id/add_button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add Lines" />
</LinearLayout>
</LinearLayout>
<!-- Sale Order Lines END -->
Got it, every LayoutParams should be of android.widget.TableRow.LayoutParams except one that supplied to tl.addView(...)
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
Please do note that, may be your code is perfect but the classes you are using are not proper. You may have used import android.view.ViewGroup.LayoutParams , where as the correct class is available from following import statement :
import android.widget.TableRow.LayoutParams
In my case, I changed the width from TableRow.LayoutParams.WRAP_CONTENT to TableRow.LayoutParams.MATCH_PARENT, then it works.
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));