How to fill the LinearLayout row? - android

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>

Related

Why are programmatically set weights for Android linear layout columns not working correctly?

I am attempting to create a key for an above pie chart within this Linear layout. However, the weights aren't working properly, and the three columns are split up equally. Does anyone know what may be causing this? Thanks!
Layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout1"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<TableRow android:gravity="center"
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:weightSum="4">
<ImageButton
android:layout_width="0dp"
android:layout_height="100dp"
android:id="#+id/aiButton"
android:layout_gravity="center_horizontal"
android:src="#drawable/barchartwhite"
android:background="#CCAF00"
android:layout_weight="1"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
android:text="TP Activity"
android:id="#+id/textView2"
android:layout_gravity="center|center_vertical"
android:textSize="40sp"
android:background="#CCAF00"
android:layout_weight="3"
android:layout_alignParentTop="true"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:gravity="center_vertical" />
</TableRow>
<TableRow android:gravity="center"
android:id="#+id/tableRowSub"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage of Activity per TP"
android:id="#+id/textViewSub"
android:layout_gravity="center"
android:textSize="16sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip">
<com.shannonsystemsllc.ediconnect.PieChartView
xmlns:chart="http://schemas.android.com/apk/res-auto"
android:id="#+id/piechart"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:layout_width="300sp"
android:layout_height="300dp"/>
</TableRow>
</LinearLayout>
Code for creating key:
for (int i = 0; i < customers.size(); i++) {
int c[] = {Color.parseColor("#FF4B66"),Color.parseColor("#00A9AC"),Color.parseColor("#70A200"),Color.parseColor("#FAB448"),Color.parseColor("#BFBFBF"),Color.GREEN,custColor,Color.CYAN,Color.BLUE};
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams tl = new TableLayout.LayoutParams();
final float scale = getActivity().getResources().getDisplayMetrics().density;
int pixels = (int) (30 * scale + 0.5f); //set pixels to dp
TableRow row = new TableRow(activity);
row.setWeightSum(9);
//first row
tl.weight = 1;
lp.weight = 6;
lp.height = pixels;
row.setLayoutParams(lp);
TextView tv1a = new TextView(getActivity());
tv1a.setTextSize(22);
tv1a.setText(customers.get(i));
tv1a.setLayoutParams(lp);
tv1a.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
TextView tv1b = new TextView(getActivity());
tv1b.setTextSize(24);
Float perFloat = ((float)pieChartValues[i]/dataTotal*100);
remainderTotal = remainderTotal - perFloat.intValue();
String percentage;
if (perFloat.intValue() == 0)
{
percentage = ("1%");
}
else
{
percentage = (perFloat.intValue() + "%");
}
lp.weight = 2;
tv1b.setText(percentage);
tv1b.setLayoutParams(lp);
tv1b.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
lp.weight = 1;
ImageView tv1c = new ImageView(getActivity());
tv1c.setLayoutParams(lp);
tv1c.setBackgroundColor(c[i]);
row.setWeightSum(3);
row.addView(tv1b);
row.addView(tv1c);
row.addView(tv1a);
row.setId(i);
ll.addView(row);
}
Screenshot:
Set the weight of the contents inside each row as follows:
- For the percentage TextView, set the weight to 3.
- For the ImageView, set the weight to 8.
- For the name Textview, set the weight to 7.
This will result to the entire row to look slightly offset from the centre. Make sure all three views are set to match parent width.
Once you set weights for the views call:
yourLinearLayout.invalidate()
Try this out and see if it works.

How can I programatically create a ScrollView with RelativeLayout boxes like this?

I am trying to create an Activity with a ScrollView in it. In the ScrollView will be "boxes" (sort of like a button) that is just a bit smaller than the width of the parent view and spaced slightly apart vertically. Because the boxes will have a couple different size text in a couple areas on them, I think I need to make the out of RelativeLayouts. (I would post a picture but can't with my reputation point so hopefully this makes sense).
I tried create an XML for the activity and than programatically adding the relativeviews. I got it to work, sort of, but am having trouble with the box spacing.
My test XML looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- scrolling section -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
My Activity looks like this...
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
I get the scrollable section, no problem. Problem is my blue "boxes" appear to be touching one another and I can't figure out how to make them look like buttons with padding left and right and between them. I tried
rlView.setPadding(20,20,20,20);
right after the setBackground color. All that appears to do is pad the text... not the RelativeViews.
Any suggestions no how to get this to work?
Thanks
===========
NEW INFORMATION:
Here is some additional information after following some suggestions and trial an error. I was also able to post a pic for easier reference.
===========
What I am trying to achieve, programmatically, is something that looks like this.
I achieved this example using the straight XML which looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
// individual groups start here - this section will be controlled by a database
// #1 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp1"
>
<TextView
android:id="#+id/text1"
android:text="Item #1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext1"
android:text="123.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp" />
</RelativeLayout>
// #2 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp2"
>
<TextView
android:id="#+id/text2"
android:text="Item #2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/leftext2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext2"
android:text="456.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I took this apart and created an XML file and and activity that will programmatically create the middle groups.
The modified XML (activity_main2) looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
I started rewriting my activity code to test some of the suggestions. It currently looks like this...
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(10,10,10,0);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
// other textviews will go here
llGroups.addView(rlView,rlp);
}
}
}
This compiles and runs, but the setMargins is not working. My screen has the "UPDATE ALL" button at the top like it should and (19) "Text #" with a blue background, but they are full width and touching one another so it is a solid blue background.
I am getting close, but not sure how to get the margins to set correctly for the rlView relativeview (not the textviews).
Hopefully this helps explain what I am seeing now..
SOLUTION
After suggestions, and some trial and error... here is what I had to do to achieve the desired layout in case someone else is interested.
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout mTvContainer = (LinearLayout) findViewById(R.id.llAccts);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.BLUE);
RelativeLayout rl = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setPadding(5, 5, 5, 5);
tv.setText("Test #" + i);
tvp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(5, 5, 5, 5);
tv2.setText(String.valueOf(i));
tv2.setTextSize(30);
tvp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, tvp);
rl.addView(tv2,tvp2);
ll.addView(rl);
llp.setMargins(32,16,32,16);
mTvContainer.addView(ll,llp);
}
}
For each entry in the FOR, I had to use a LINEARLAYOUT to be able to set the margins properly, and in that LINEARLAYOUT I had to use a RELATIVELAYOUT to use the addRules to get the proper positioning of the TextViews.
If there is an easier way, let me know. Thanks for everyones help!!
You are using too much nested layouts needed, here is stripped version.
xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
Activity code:
mTvContainer = (LinearLayout) findViewById(R.id.ll_main);
int p = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setBackgroundColor(Color.BLUE);
tv.setTextColor(Color.WHITE);
tv.setPadding(p, p / 2, p, p / 2);
tv.setText("TextView " + i);
TextView tv2 = new TextView(this);
tv2.setBackgroundColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(p, p / 2, p, p / 2);
tv2.setText("TextView " + i);
ll.addView(tv);
ll.addView(tv2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(p, p / 2, p, p / 2);
mTvContainer.addView(ll, params);
}

Defining ImageView to be aligned right

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

How to get layout working programmatically

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

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.

Categories

Resources