I can't set array textview in same row programatically - android

I have a string, comma separated, from mysql database witch I split into an array.
My problem is that when I try to put them in a tablerow programatically it display each split value in a new row. I want to display it in same row with 4 columns.
This is my code:
String currentString = Order_list;
String[] separated = currentString.split(",");
TableLayout secondTbl = findViewById(R.id.secondTable);
for(int i=0;i<separated.length;i++){
TableRow row= new TableRow(getApplicationContext());
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setBackgroundResource(R.drawable.row_drawable);
txtOrdprod = new TextView(getApplicationContext());
row.addView(txtOrdprod);
TableRow.LayoutParams params = (TableRow.LayoutParams) txtOrdprod.getLayoutParams();
params.leftMargin = 18;
params.rightMargin = 10;
params.topMargin = 10;
txtOrdprod.setLayoutParams(params);
txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
txtOrdprod.setTextColor(Color.BLACK);
txtOrdprod.setTypeface(null, Typeface.BOLD);
txtOrdprod.setText(separated[i]);
secondTbl.addView(row,i);
}

You have to create a row, and put your columns (seperated strings) into that row. For example:
String orders = "2, Product, 100 Tablets, 50 Eur, 3, Product, 100 Tablets, 75 Eur";
int k = 0;
int startPoint = 0;
List<String> orderList = new ArrayList<>();
for(int i = 0; i < orders.length(); i++)
{
if(orders.charAt(i) == ',')
{
k++;
if(k == 4)
{
String ab = orders.substring(startPoint, i);
orderList.add(ab);
startPoint = i + 1;
k = 0;
}
}
}
TableLayout secondTbl = findViewById(R.id.secondTable);
int rowIndex = 0;
for(String order : orderList)
{
String[] separated = order.split(",");
TableRow row = new TableRow(getApplicationContext());
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setBackgroundResource(R.drawable.row_drawable);
secondTbl.addView(row, rowIndex);
for(int i = 0; i < separated.length; i++)
{
txtOrdprod = new TextView(getApplicationContext());
row.addView(txtOrdprod);
TableRow.LayoutParams params = (TableRow.LayoutParams)
txtOrdprod.getLayoutParams();
params.leftMargin = 18;
params.rightMargin = 10;
params.topMargin = 10;
txtOrdprod.setLayoutParams(params);
txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
txtOrdprod.setTextColor(Color.BLACK);
txtOrdprod.setTypeface(null, Typeface.BOLD);
txtOrdprod.setText(separated[i]);
}
rowIndex++;
}
But keep in mind that the orders string needs to be formatted like the on in the example, otherwise it will not work. It would be better if you have a POJO for an order and work with this one.

Related

TextView FILL_PARENT doesn't work

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

Could not create multiple rows with header(of column and row) in Android

I have problem with creating header of column and row with respective cell values. Only show last position value in both column and row.
ScrollView sv = new ScrollView(this);
TableLayout ll = new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6", "Row 7" };
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6" };
int nor = row.length;
int noc = column.length;
TextView tv = new TextView(this);
tv.setText("Matrix Implemention Test");
for (int i = 0; i < nor; i++)
{ // for rows
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++)
{ // for columns
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
} else if (j == 0) {
for (int c = 0; c < noc; c++) {
tv1.setText(column[c]);
Log.d("TAG", "j==0->"+row[c]);
}
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);
Output like this: I want of respective header of both column and row. first row 1, row 2.... and column 1, column 2....and so on. but only last position value display above code.
change your code to:
for (int i = 0; i < nor; i++) { // for row
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++) { // for column
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
tv1.setText(column[j]);
} else if (j == 0) {
tv1.setText(row[i]);
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
First thing you seems to have confused columns with rows so i corrected that.
And second notice this for loop:
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
is missing.
Here you are first setting row[0] value to tv1 and then row[1] ... so on.
And in the end tv1 has row[6] as the last value.

Display the array in android

How to get an array items like this
["String1", "string2", "string3",.....,"Stringn"]
but if we use Arrays.toString(array) it will display like [string1, string2, string3] but i want like above.
Thnaks in advance
This is how I did it:
private void btn3() {
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
layout.removeAllViewsInLayout();
layout.setPadding(15, 15, 15, 15);
tv2 = new TextView[list.size()];
for (int i = 0; i < list.size(); i++) {
tv2[i] = (TextView) new TextView(Random.this);
}
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
for (int i = 0; i < list.size(); i++) {
tv2[i].setLayoutParams(lparams);
tv2[i].setText((i + 1) + " " + list.get(i));
layout.addView(tv2[i]);
}
}

Table doesn't fill the containing layout's width

I want this table to extend as possible to fill the remaining (empty) area of the linear layout that contains it
I generate the table using the following method
public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){
//clearing previous views
tableLayout.setVisibility(View.VISIBLE);
tableLayout.setGravity(Gravity.CENTER);
int chids = tableLayout.getChildCount();
if (chids > 0){
tableLayout.removeAllViews();
}
if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
return;
}
TableLayout table = new TableLayout(context);
// table.setStretchAllColumns(true);
// table.setShrinkAllColumns(true);
//Rendering the table title
TextView tableTitleView = new TextView(context);
tableTitleView.setText(tableTitle);
tableTitleView.setTextColor(Color.WHITE);
tableTitleView.setGravity(Gravity.CENTER);
tableTitleView.setTextSize(18);
tableTitleView.setTextAppearance(context, R.style.boldText);
tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
tableTitleView.setPadding(2, 2, 2, 10);
tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ScrollView scroll1 = new ScrollView(context);
HorizontalScrollView scroll2 = new HorizontalScrollView(context);
int rows = data.length;
int columns = data[0].length;
if(headerTitles!=null){
//Rendering the header
TableRow headerRow = new TableRow(context);
for (int i = 0; i < columns; i++) {
TextView t = new TextView(context);
t.setTextSize(17);
t.setTextAppearance(context, R.style.boldText);
t.setPadding(5, 5, 5, 5);
t.setText(headerTitles[i]);
t.setGravity(Gravity.CENTER);
t.setTextColor(Color.WHITE);
t.setBackgroundResource(R.drawable.text_border_header_1);
headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(headerRow, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
//Rendering the Table Data
TableRow row;
for (int current = 0; current < rows ; current++) {
row = new TableRow(context);
TextView t;
for(int i =0 ; i < columns ; i++){
t = new TextView(context);
t.setTextSize(15);
t.setPadding(5,5,5,5);
t.setText(data[current][i]);
t.setGravity(Gravity.CENTER);
if (type == 1) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd);
} else {
t.setBackgroundResource(R.drawable.text_border_even);
}
}else if (type == 2) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_2);
} else {
t.setBackgroundResource(R.drawable.text_border_even_2);
}
}else if (type == 3) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_3);
} else {
t.setBackgroundResource(R.drawable.text_border_even_3);
}
}
t.setTextColor(Color.BLACK);
row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.setPadding(2, 20, 2, 20);
tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
}
I couldn't figure out exactly the problem with my code.
EDIT :
when I added the table directly to the linear layout instead of the scroll views the table fits with the linear layout width. It seems that my problem is with the scroll view
I found the answer Here (thanks to Felix)
Just a single line of code to be added to the scroll views
scrollView.setFillViewPort(true);
try this on your TableLayout object.
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);
Edit:
TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);
regards,
Aqif

Request focus doesn't work in ScrollView Android

I have a scroolView containing a table with buttons, I'm trying to request focus to one of the buttons but with no success
here is the code (some lines were removed to avoid floating)
private void reDraw()
{
ll = new LinearLayout(this);
ll.setOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ll.setGravity(Gravity.FILL);
ll.setBackgroundResource(R.drawable.modern_background);
ll.setPadding(20, 20, 20, 20);
sv = new ScrollView(this);
sv.setPadding(0, 20, 0, 0);
tl = new TableLayout(this);
tl.setGravity(Gravity.FILL);
tl.setStretchAllColumns(true);
get_start_and_end_of_month(Start.globalDate);
day = 1;
int dayInWeek = startOfTheMonth.get(java.util.Calendar.DAY_OF_WEEK);
String temp;
tr = new TableRow(this);
tr.setGravity(Gravity.FILL_HORIZONTAL);
for(int k = 0; k < 7; k++)
{
b = new Button(this);
b.setGravity(Gravity.CENTER);
b.setBackgroundResource(R.drawable.widget_cell_day);
b.setText(Start.hebLayout ? daysHebrew[k] : daysEnglish[k]);
tr.addView(b);
}
tl.addView(tr);
for(int i = 0; i < 5; i++)
{
tr = new TableRow(this);
tr.setGravity(i == 0 ? Gravity.RIGHT : Gravity.LEFT);
for(int j = 1; j < 8; j++)
{
if(!(i == 0 && j < dayInWeek))
{
db = new DateButton(this, startOfTheMonth.getTime());
db.setGravity(Gravity.CENTER);
if(new HdateNew(Start.globalDate).get_hd_day() == day)
db.setFocusableInTouchMode(true);
db.setId(day);
tr.addView(db);
if(day == new HdateNew(endOfTheMonth.getTime()).get_hd_day())
break;
day++;
startOfTheMonth.add(java.util.Calendar.DATE, 1);
}
}
tl.addView(tr);
}
sv.addView(tl);
ll.addView(sv);
setContentView(ll);
db = (DateButton)findViewById(new HdateNew(Start.globalDate).get_hd_day());
db.setFocusable(true);
db.requestFocus();
}
Now as you can see I did set the button to be focusable but it ain't working for me
//try set content view # last
db = (DateButton)findViewById(new HdateNew(Start.globalDate).get_hd_day());
db.setFocusable(true);
db.requestFocus();
setContentView(ll);

Categories

Resources