something like this i want
for(i=0; i<10; i++) {
Textview tx[i] = new TextView(this);
tx[i].setText("Data")
TableRow tr[i] = new TableRow(this);
tr[i].addView(tx);
// and then adding table row in tablelayout
}
can somebody give me small example
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(i=0; i<10; i++) {
tx[i] = new TextView(this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText("Data")
tr[i] = new TableRow(this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
}
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(i=0; i<10; i++) {
tx[i] = new TextView(this);
tx[i].setText("Data")
tr[i] = new TableRow(this);
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
}
Related
I want to make that TextView fill parent but my code doesn't work.
Here is my code:
TableLayout tableLayout = new TableLayout(this);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
TableRow.LayoutParams param = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
param.span = 2;
param.setMargins(1, 1, 1, 1);
int trainIndex = 0;
int stationIndex = 1;
for(int i = 0; i < 6; i++){
TableRow row = new TableRow(this);
if(i == 0){
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text, tableRowParams);
for(int j = 0; j < 4; j++) {
TextView text1 = new TextView(this);
text1.setText(getInfo(0, stationIndex));
text1.setBackgroundColor(Color.GRAY);
row.addView(text1, param);
stationIndex++;
}
}else {
for (int j = 0; j <= 8; j++) {
TextView text = new TextView(this);
if(j == 0){
text.setText(getInfo(1,trainIndex));
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
trainIndex++;
}else {
text.setText(" train " + j);
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
}
}
}
tableLayout.addView(row);
}
setContentView(tableLayout);
When I apply layout params to textview it doesn't work and when text isn't very long it causes white space in cells, I want that all cells have grey background.
Here is the sample how it looks:
What am I doing wrong ?
Be sure that your container has the FILL_PARENT property
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
You forgot to set LayoutParams for the TextView. By default is uses WRAP_CONTENT for both width and height.
Also, use MATCH_PARENT instead of FILL_PARENT
EDIT
Try this:
TableRow row = new TableRow(this);
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text);
I am creating a number of buttons in a table row programmatically, but they always end up left-aligned. How can I center the entire row? I am trying this to create each row:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
You can simply center the row using a Gravity. Just set it to your row with
row.setGravity(Gravity.CENTER_HORIZONTAL);
Try this:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setLayoutParams(buttonParams);
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
I have this TableRow of EditText inserted in a tablelayout, this tablelayout is inserted in one Scrollview, this Scrollview is inserted in a HorizontalScrollView. HorizontalScrollView contains, Also, an image of the background. The problem is that I can't scroll properly and display all the EditText. Can you help me?
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
HSC.setBackgroundResource(R.drawable.lavagna);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
values = new EditText[r][c];
for (int i = 0; i < r; i++){
tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < c; j++){
values[i][j] = new EditText(this);
values[i][j].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
values[i][j].setHint("r: " + (i) + " " + "c: " + (j));
values[i][j].setPadding(10, 10, 10, 10);
tableRow.addView(values[i][j]);
}
Remove Gravity.CENTER parameters, or the scroller will scroll from the center of its content that is big enough. It's there, but you can't get it.
Correct various values
Code:
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
HSC.setBackgroundResource(R.drawable.lavagna);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// you may or may not change here and below
// values = new EditText[15][15];
for (int i = 0; i < 15; i++){
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < 15; j++){
EditText value = new EditText(this);
value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
value.setHint("r: " + (i) + " " + "c: " + (j));
value.setPadding(10, 10, 10, 10);
tableRow.addView(value);
}
tableLayout.addView(tableRow);
}
VSC.addView(tableLayout);
HSC.addView(VSC);
frameLayout.addView(HSC);
// ... add frameLayout
// ...
}
Here is my code
final RadioButton[] rb = new RadioButton[6];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
for(int i=0; i<6; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Dynamic Radio Button " + i);
rb[i].setId(i);
rb[i].getGlobalVisibleRect(null);
rg.addView(rb[i]);
}
ll.addView(rg);
I am trying to display these radio buttons programatically. I got 6 radio buttons but am unable to display these as rows and columns.
You can use TableLayout to arrange the Radio Button in Rows and Columns
Sample Code
LinearLayout ll = new LinearLayout(this);
TableLayout tl = new TableLayout(this);
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
for (int j = 0; j < 6; j++) {
RadioButton rb = new RadioButton(this);
rb.setText("Dynamic Radio Button " + i);
rb.setId(i);
rb.getGlobalVisibleRect(null);
tr.addView(rb);
}
tl.addView(tr);
}
ll.addView(tl);
setContentView(ll);
Screenshots link
I have a list of bitmap images, How can I add this bitmap to a tableLayout programmatically?
thanks.
Try something like this:
TableLayout table = new TableLayout(this);
for (int i = 0; i < mRows; i++) {
TableRow tr = new TableRow(mContext);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < mCols; j++) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.star_on)
tr.addView(view);
}
table.addView(tr);
}
This generates the whole table / grid programmatically. You can see how it instantiates an ImageView and sets the image.