Filling a container with text area - android

My main activity window is looking really ugly. Since i tried and worked over a tutorial now my final part is to set the size to be full of the screen (portrait/landscape)
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="521dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner">
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical">
<TableLayout
android:id="#+id/table_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:stretchColumns="0">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
Adding rows in this table layout is going dynamically and the code is shown bellow :
// border
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.WHITE); // Changes this drawbale to use a single
color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
// draw table header
final TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
tv0.setText(" Type ");
tv0.setPadding(5, 15, 0, 15);
tv0.setTextColor(Color.BLACK);
tv0.setBackground(gd);
tv0.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv0.setGravity(Gravity.CENTER);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Year ");
tv1.setPadding(5, 15, 0, 15);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv1.setGravity(Gravity.CENTER);
tv1.setBackground(gd);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setPadding(5, 15, 0, 15);
tv2.setText(" Month ");
tv2.setTextColor(Color.BLACK);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv2.setGravity(Gravity.CENTER);
tv2.setBackground(gd);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setPadding(5, 15, 0, 15);
tv3.setText(" Week ");
tv3.setTextColor(Color.BLACK);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv3.setGravity(Gravity.CENTER);
tv3.setBackground(gd);
tbrow0.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setPadding(5, 15, 0, 15);
tv4.setText(" Amount ");
tv4.setTextColor(Color.BLACK);
tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv4.setGravity(Gravity.CENTER);
tv4.setBackground(gd);
tbrow0.addView(tv4);
stk.addView(tbrow0);
Even its so much code the control still looks like wrong placed like shown on picture bellow :
Can anyone take a look on my main activity and tell me what am i doing wrong here.
The final output should be the table well placed from edge to edge with same row width and height size

Just by changing activity to this solved the issue
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="613dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/spinner"
tools:layout_editor_absoluteX="0dp">
<TableLayout
android:id="#+id/table_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>

Related

Android table layout adding single column row that spans table width in java

I am trying to add a single textview row to a table in android
the row should merge and center the width of the table
I am able to achieve this using XML
<TableLayout>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/red_cell_shape"
android:text="code"
android:textAlignment="center"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/red_cell_shape"
android:padding="5dp"
android:text="desc"
android:textAlignment="center"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/red_cell_shape"
android:text="status"
android:textAlignment="center"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tremptyrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/red_cell_shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/no_fg_scanned"
android:padding="5dp"
android:layout_span="6"
android:textAlignment="center"
android:textColor="#color/red" />
</TableRow>
</TableLayout>
this is what it should look like
but I want to do it dynamically in java and tried this but did not work
You can implement the above layout programmatically like below:
1.Create an empty TableLayout in xml like below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
2.Get the TableLayout reference from xml and create two TableRows each one having TextView children:
//get the TableLayout
TableLayout tableLayout = findViewById(R.id.tableLayout);
//create the first TableRow
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
tableRow1.setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
//add 3 TextViews in TableRow1 and add TableRow1 to TableLayout
tableRow1.addView(getTextView(android.R.color.holo_purple, android.R.color.black, "Code"));
tableRow1.addView(getTextView(android.R.color.holo_red_light, android.R.color.black,"Desc"));
tableRow1.addView(getTextView(android.R.color.holo_green_dark, android.R.color.black,"Status"));
tableLayout.addView(tableRow1);
//create the second TableRow
TableRow tableRow2 = new TableRow(this);
tableRow2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
tableRow2.setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
//add 1 TextView in TableRow2 and add TableRow2 to TableLayout
tableRow2.addView(getTextView(android.R.color.black, android.R.color.holo_red_light,"No records"));
tableLayout.addView(tableRow2);
with the below helper function to create each TextView in TableRow programmatically. The key point here is to use layoutParams.weight = 1 on TextView TableRow.LayoutParams to be able to get equal width between all columns in TableRow:
private TextView getTextView(int backgroundColorId, int textColorId, String text){
TextView tv = new TextView(this);
tv.setBackgroundColor(ContextCompat.getColor(this, backgroundColorId));
tv.setTextColor(ContextCompat.getColor(this, textColorId));
tv.setText(text);
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
tv.setPaddingRelative(padding, padding, padding, padding);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
layoutParams.weight = 1; //this is mandatory to get equal width between all columns in TableRow
tv.setLayoutParams(layoutParams);
return tv;
}
Result:

How to stretch programmatically created columns with HorzontalScroll View

Hello I been looking around for an answer but for some reason most of the answers that i find do not help me. I have created a tablelayout on my fragment and I am adding a simple tablerow with a text view programmatically. Currently there are 5 or more columns which seem to be packed into the left corner Is there any way to stretch accross the view? thank you for you help!
Update: removing the Horizontal view fixes the problem, however is there a way to make the table stretch with horizontal scrolling? or would I have to manually add a width for each cell?
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#3d455b">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/entries"
android:stretchColumns="*">
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/row_cell">
</TableRow>
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
JAVA:
valuesTable.removeAllViews();
//add header
TableRow tbrow0 = new TableRow(ctx);
tbrow0.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv0 = new TextView(ctx);
tv0.setText("Id");
tv0.setTextSize(20);
tv0.setTextColor(Color.WHITE);
tv0.setPadding(10, 10, 10, 10);
tbrow0.addView(tv0);
TextView tv0 = new TextView(ctx);
tv0.setText("name");
tv0.setTextSize(20);
tv0.setTextColor(Color.WHITE);
tv0.setPadding(10, 10, 10, 10);
tbrow0.addView(tv0);
TextView tv0 = new TextView(ctx);
tv0.setText("color");
tv0.setTextSize(20);
tv0.setTextColor(Color.WHITE);
tv0.setPadding(10, 10, 10, 10);
tbrow0.addView(tv0);
valuesTable.addView(tbrow0);
//Add row information
TableRow tbrow = new TableRow(ctx);
tbrow.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(String value : row.getValues()){
TextView t1v = new TextView(ctx);
t1v.setText(value);
t1v.setTextSize(17);
t1v.setTextColor(getResources().getColor(R.color.darkgray));
t1v.setPadding(10, 10, 10, 10);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
}
valuesTable.addView(tbrow);

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.

android programmatic layout

I'm building a simple two-column list in android, and need to populate the number of rows dynamically. The xml layout works fine, but when i try to build the layout programmatically the second column doesn't show. Any ideas?
private void addRow(String text, String value) {
RelativeLayout line = new RelativeLayout(this);
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
line.setPadding(0, 0, 0, 15);
TextView caption = new TextView(this);
caption.setLayoutParams(rlp);
caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
caption.setText(text);
rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
TextView val = new TextView(this);
val.setLayoutParams(rlp);
val.setTextColor(0x05b4e4);
val.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
val.setText(value);
line.addView(caption);
line.addView(val);
mLayout.addView(line);
}
The comparable xml for a single row (that works) is this
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="15px" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="What"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/txtClubPath"
android:textColor="#05b4e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="stuff"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Again - the rows populate, but the second column isn't visible. Here's what it's going into:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:keepScreenOn="true">
<LinearLayout
android:id="#+id/viewRoot"
android:paddingTop="20px"
android:paddingBottom="40px"
android:paddingLeft="20px"
android:paddingRight="20px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
The layout params of the view you are adding should be of type LinearLayout.LayoutParams because the parent will be of that type.
This line
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
should become
line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
However, I think this should cause an exception if anything.
It is important for the RelativeLayout children to have a unique non-zero id.
You could, on API 17+, use
myView.setId(View.generateViewId());
Or if you have to support older API levels, you could use (and increment each time) a simple static AtomicInteger value.

How to fill the LinearLayout row?

I using LinearLayout as a table that need to contain 3 column.
I need to feel up the row an i don't know how to do it from the code in run time.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/Cell1"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/Cell2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView android:id="#+id/Cell3"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
This is the code that produces layout equivalent to what you have declared in your .xml
You could put it somewhere in your activity (so this refers to your activity)
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
row.setPadding(0, 4, 0, 6);
row.setOrientation(LinearLayout.HORIZONTAL);
// creating and setting the cells now
// cell 1
TextView cell1 = new TextView(this);
// convert dip into pixels for LayoutParams constructor
// as it only understands px
int widthPix1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
50, getResources().getDisplayMetrics());
row.addView(cell1, new LinearLayout.LayoutParams(
widthPix1, LinearLayout.LayoutParams.WRAP_CONTENT));
// cell 2
TextView cell2 = new TextView(this);
int widthPix2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
70, getResources().getDisplayMetrics());
row.addView(cell2, new LinearLayout.LayoutParams(
widthPix2, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
// cell 3
TextView cell3 = new TextView(this);
int widthPix3 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
60, getResources().getDisplayMetrics());
row.addView(cell3, new LinearLayout.LayoutParams(
widthPix3, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
As you can see the amount of code is significantly bigger than when your layout is in .xml and you simply inflate with
LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row, null);
And I can hardly imagine a situation when you want to do it the way you asked for.
Anyways, enjoy.
My advice if I got you right:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/Cell1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
<TextView android:id="#+id/Cell2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"/>
<TextView android:id="#+id/Cell3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"/>
</LinearLayout>

Categories

Resources