Question

I have three textviews which need to be aligned to the screen. I cant align them, see below:

https://www.dropbox.com/s/ab8pdhrpmjv2aql/Screenshot_2014-02-24-15-18-54.png

I need to be able to programmatically move them and try to match them to the static column headings.

I create the rows by:

               for (int j=0; j<events.size(); j++){
                String time = events.get(j).getTime();
                String event = events.get(j).getEvent();
                String location = events.get(j).getLocation();




                TableRow row = new TableRow(getActivity());

                TextView t = new TextView(getActivity());
                t.setText(time);
                row.addView(t);


                TextView e = new TextView(getActivity());
                e.setText(event);
                row.addView(e);

                TextView loc = new TextView(getActivity());
                t.setText(location);
                row.addView(loc);

                table.addView(row); 



            }

and the xml layout is:

  <TableLayout
      android:id="@+id/EventTable"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:stretchColumns="0"
      android:layout_below="@+id/dateSpinner"
      android:layout_marginTop="15dp">


      <TableRow>       
<!-- Column 1 -->      
<TextView         
android:id="@+id/columnTime"         
android:layout_width="0dip"         
android:layout_height="wrap_content"        
android:background="#CBCBCB"         
android:textColor="#000000"         
android:padding="10dip"         
android:layout_margin="4dip"         
android:layout_weight="1"         
android:text="@string/eventTableTime" />                  
<!-- Column 2 -->      
<TextView         
android:id="@+id/columnEvent"         
android:layout_width="0dip"         
android:layout_height="wrap_content"         
android:background="#CBCBCB"         
android:textColor="#000000"         
android:padding="10dip"         
android:layout_margin="4dip"         
android:layout_weight="1"         
android:text="@string/eventTableEvent" />                  
<!-- Column 3 -->      
<TextView         
android:id="@+id/columnLocation"         
android:layout_width="0dip"         
android:layout_height="wrap_content"         
android:background="#CBCBCB"         
android:textColor="#000000"         
android:padding="10dip"         
android:layout_margin="4dip"         
android:layout_weight="1"         
android:text="@string/eventTableLocation" />     
</TableRow> 


  </TableLayout >

any ideas?

thanks

Was it helpful?

Solution

Try to define the layout params for each row:

TableRow row = new TableRow(getActivity());

TableRow.LayoutParams lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
lp.setMargins(4, 4, 4, 4);


TextView t = new TextView(getActivity());
t.setPadding(10, 10, 10, 10);
t.setText("17:00");

row.addView(t, lp);

t = new TextView(this);
t.setText("Event 1");
t.setPadding(10, 10, 10, 10);
t.setEllipsize(TruncateAt.MARQUEE);
t.setSingleLine(true);
t.setSelected(true);
row.addView(t, lp);

t = new TextView(getActivity());
t.setText("Location 1");
t.setEllipsize(TruncateAt.MARQUEE);
t.setSelected(true);
t.setSingleLine(true);
t.setPadding(10, 10, 10, 10);
row.addView(t, lp);


table.addView(row);

...just as suggestion, the best to achieve this result (especially for many records) would be using a ListView (or ListFragment) with eventually a AsyncLoader + Adapter (with eventually a ViewHolder)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top