I've looked through all the people that have asked the same thing and no one has ever answered correctly the question. For some reason I can't seem to find how to do this.
I need to add a margin between each of my dynamically made TableRows.
This is what I have now.
As you can see, the rows are way together, making the GUI not usable for a user.
The TableLayout is inside a ScrollView.
This is the code I use to create dynamically the TextView and the button. I have tried using margins but no luck so far
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
/* Margin. */
int leftMargin = 0;
int topMargin = 200;
int rightMargin = 0;
int bottomMargin = 0;
/* Set margins and create new textview. */
tableRowParams.setMargins(leftMargin, topMargin, rightMargin,
bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView encuestasTextView = new TextView(this);
encuestasTextView.setText("Esto es una prueba");
encuestasTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(encuestasTextView);
Button a = new Button(this);
a.setText("Abrir encuesta");
a.setLayoutParams(new TableRow.LayoutParams(width / 3,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(a);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
This is the XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Encuestas" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/button1"
android:layout_weight="1"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/TableLayout1">
</TableLayout>
</ScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:onClick="SyncServer"
android:text="Sync" />
</RelativeLayout>
What I'm missing?
I think this:
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
is wiping out the layout params you set earlier. Just use tl.addView(tr);
Related
They currently appear on the center.
Android Java Code
public void populateTable() {
for(DoctorBean post: beanPostArrayList){
String result = "";
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ImageView docImageView = new ImageView(getActivity());
docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
docImageView.setPadding(5, 5, 5, 5);
docImageView.setImageResource(R.drawable.ic_doctor);
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
result += "\n" + post.getHeroName();
textView.setGravity(Gravity.LEFT);
docImageView.setForegroundGravity(Gravity.LEFT);
row.setGravity(Gravity.LEFT);
textView.setText(result);
row.addView(docImageView);
row.addView(textView);
tableLayout.addView(row);
}
}
Layout Code
<?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:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*">
</TableLayout>
</ScrollView></LinearLayout>
So there I want to align the ImageView and the TextView to the left.
An alternative approach should solve your issue
You don't need to add explicit scroll for a TableView
TableView can be directly added to your main LinearLayout. As you have already specified the height of MapView
Suggested Modified xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
Corresponding code change
LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText("Content left");
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
//add table layout to linear layout
linearLayout.add(tableLayout);
NOTE:
Many values have been referred from resource files, You can either
neglect/replicate those. I have added two textviews. You may need to
change according to your requirement
what i am trying to do is to make a dynamic table layout .. i mean the rows will be filled dynamically with text views and with scrolling horizontal and vertical but each time when the activity open my table layout start showing from the left side even i set the gravity to be right .. why this is happening?
this is my xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_gravity="right" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right">
</TableRow>
</TableLayout>
</ScrollView>
</HorizontalScrollView>
</LinearLayout>
and here is my java code :
TableLayout ll = (TableLayout) findViewById(R.id.table1);
int razan=0;
int c=-1;
for(int s=1; s<=fixed;s++){
if(razan<=count){
for (int i = 0; i <text.length; i++) {
if(i>0 && i%7==0){
c = c+1;
TableRow row1= new TableRow(this);
TableRow.LayoutParams tr1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row1.setLayoutParams(tr1);
TextView emptyrow = new TextView(this);
emptyrow.setText("");
// emptyrow.setBackground(getResources().getDrawable(R.drawable.back));
row1.addView(emptyrow);
ll.addView(row1,i);
}
TableRow row= new TableRow(this);
TableRow.LayoutParams tr = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(tr);
TextView product = new TextView(this);
TextView product_j = new TextView(this);
product_j.setText(prodlist.get(razan));
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.back);
product_j.setBackgroundDrawable(drawable);
product_j.setGravity(Gravity.RIGHT);
product_j.setTextSize(18);
product.setText(text[i]);
// product.setBackgroundDrawable(drawable);
product.setGravity(Gravity.RIGHT);
product.setPadding(3, 0, 3, 0);
product.setTextSize(18);
row.addView(product_j);
row.addView(product);
razan++;
ll.addView(row,i);
//here is the code for adding an empty row after each seven inner rows !!!
}//inner for
}
}//outer fors
}
can anyone help me?
Make your root LinearLayouts Height and width as match_parent and try gravity to right on your TableLayout
I am trying to add rows to a table after a click to a button (so after the generation of the activity).
My XML code is:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:id="#+id/result_table">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
style="#style/MyHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col1" />
<TextView
style="#style/MyHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col2" />
</TableRow>
</TableLayout>
So when I load the activity I have my table with my only row. Then when I click on my button I do:
resultTable = (TableLayout)findViewById(R.id.result_table);
LayoutParams rowLayoutParams = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams cellLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow tableRow;
TextView cellValue;
for(Asdf a : as) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(rowLayoutParams);
cellValue = new TextView(this);
cellValue.setText(a.getText());
cellValue.setLayoutParams(cellLayoutParams);
cellValue.setTextAppearance(this, R.style.MyText);
tableRow.addView(cellValue);
cellValue = new TextView(this);
cellValue.setText("asdf");
cellValue.setLayoutParams(cellLayoutParams);
cellValue.setTextAppearance(this, R.style.MyText);
tableRow.addView(cellValue);
resultTable.addView(tableRow);
}
resultTable.invalidate();
But it doesn't add anything, and I am sure about that the loop iterates. I don't get any warning/exception in the logcat.
Can you help me please ??
Try to change the LayoutParams for the added Views like this:
TableLayout.LayoutParams rowLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams cellLayoutParams = TableRow.new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
I'm trying to build a part of a Android View Programmatically, but unfortunately I'm having some problems with the RelativeLayout. it makes my Elements overlay on eachother
This is my Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertnew_layout);
LinearLayout ll = (LinearLayout) findViewById(R.id.container);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// fill content
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView score = new TextView(this);
score.setText(""+i);
RelativeLayout.LayoutParams lpScore = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpScore.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
score.setLayoutParams(lpScore);
TextView description = new TextView(this);
description.setText("this is my description");
RelativeLayout.LayoutParams lpDescription = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
description.setLayoutParams(lpDescription);
CheckBox checkbox = new CheckBox(this);
RelativeLayout.LayoutParams lpCheckbox = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpCheckbox.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
checkbox.setLayoutParams(lpCheckbox);
rl.addView(score);
rl.addView(description);
rl.addView(checkbox);
tl.addView(rl);
}
ll.addView(tl);
This is what it looks like:
As you can see, the "description" is on top of of the "score".
Here is the same code in xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableLayoutTextEntry" >
<TableRow
android:id="#+id/tableRowTextEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="score" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/score"
android:text="description" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="select" />
</RelativeLayout>
</TableRow>
</TableLayout>
And here is what it looks like in xml:
As you can see, in xml, the toRightOf-command works as expected - the description is to the right of the score
How is that possible? Shouldnt the line
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
do the same thing?
At the moment you are calling it, score.getId() will return -1 (invalid), because it has not been added yet to the screen with the whole layout processed.
You will have to set the id manually with setId() before calling getId() and it should all work fine.
I'm trying to make my tablelayout which is nested in a relativeview scrollable.
Tried a couple of tutorials but nothing worked. Here is the xml and my java code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/white" android:padding="10px"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/textView1"
android:paddingBottom="10dp" android:text="Mijn Rooster"
android:textSize="20sp" android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/tablelayout"></TextView>
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableLayout android:id="#+id/tablelayout"
android:layout_width=
"fill_parent" android:layout_height="wrap_content"
android:stretchColumns="0" android:layout_below="#+id/textView1"
android:layout_above="#+id/btnsearch">
</TableLayout>
</ScrollView>
<Button android:id="#+id/btnsearch" android:layout_width="wrap_content"
android:text="Zoek op Datum" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"></Button>
</RelativeLayout>
And here is the JAVA code:
TableLayout tl = (TableLayout) findViewById(R.id.tablelayout);
if (date_selected == null) {
for (int i = 0; i < roostermap.size(); i++) {
TableRow trdaydatetime = new TableRow(this);
TableRow trinfo = new TableRow(this);
trdaydatetime.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
rowSpanLayout.span = 3;
TextView txtDay = new TextView(this);
TextView txtTime = new TextView(this);
TextView txtResult = new TextView(this);
// Set Text
txtDay.setText(roostermap.get(i).getDay(
roostermap.get(i).getRoster_date().getDay(),
roostermap.get(i).getRoster_date())
+ " " + df.format(roostermap.get(i).getRoster_date()));
txtTime.setText(dft.format(roostermap.get(i).getRoster_start())
+ " - " + dft.format(roostermap.get(i).getRoster_end()));
txtResult.setText(roostermap.get(i).getWorkplace_name());
// Day&Date Layout
txtDay.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtDay.setPadding(5, 5, 0, 0);
txtDay.setTextColor(Color.BLACK);
// Text Time layout
txtTime.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtTime.setTextColor(Color.BLACK);
txtTime.setPadding(0, 5, 10, 0);
txtTime.setGravity(Gravity.RIGHT);
// Text Result layout
txtResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtResult.setPadding(5, 0, 0, 5);
trdaydatetime.addView(txtDay);
trdaydatetime.setBackgroundResource(R.drawable.trup);
trdaydatetime.addView(txtTime);
trinfo.addView(txtResult, rowSpanLayout);
trinfo.setBackgroundResource(R.drawable.trdown);
tl.addView(trdaydatetime, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(trinfo, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
In order for the 'scrolling' to work you need the content of the ScrollView to be larger than the ScrollView itself. Since you have set the android:layout_height value to wrap_content, the height of the ScrollView matches the height of its content - which means no scrolling.
Fix the android:layout_height value of the ScrollView by either setting it to fill_parent (possibly with android:layout_weight) or a pixel value.
Eliminate the ScrollView. You cannot combine two Views which are both scrollable, especially if the scroll in the same direction.
A TableView itself handles scrolling if the content is larger then the height of the TableView.