Dio Baccay Dio Baccay - 26 days ago 7
Android Question

How to put scrollbar in a table layout

I made a program and I made use of table layout as my layout,I want to put scrollbar int it, I used table.setVerticalScrollBarEnabled(true); but I did not work,what is my best remedy?
Here are my codes:

TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);// i used this but it did not work
table.setPadding(10, 10, 10, 10);


TableRow tableRow = new TableRow (getApplicationContext());

TextView txt = new TextView (getApplicationContext());
TextView txt2 = new TextView (getApplicationContext());
TextView txt3 = new TextView (getApplicationContext());
TextView txt4 = new TextView (getApplicationContext());
TextView txt5 = new TextView (getApplicationContext());
TextView txt6 = new TextView (getApplicationContext());

tableRow.addView(txt);
tableRow.addView(txt2);
tableRow.addView(txt3);
tableRow.addView(txt4);
tableRow.addView(txt5);
tableRow.addView(txt6);



tableRow.setBackgroundColor(Color.GRAY);

txt.setText("Question ");
txt2.setText("Excellent ");
txt3.setText("Best ");
txt4.setText("Better ");
txt5.setText("Good ");
txt6.setText("Poor ");

txt.setTextColor(Color.BLACK);
txt2.setTextColor(Color.BLACK);
txt3.setTextColor(Color.BLACK);
txt4.setTextColor(Color.BLACK);
txt5.setTextColor(Color.BLACK);
txt6.setTextColor(Color.BLACK);


table.addView(tableRow);

int j=0;
for(j = 1; j<=count; j++){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));


tableRow = new TableRow (getApplicationContext());
TextView name = new TextView (getApplicationContext());
EditText et2 = new EditText (getApplicationContext());
EditText et3 = new EditText (getApplicationContext());
EditText et4 = new EditText (getApplicationContext());
EditText et5 = new EditText (getApplicationContext());
EditText et6 = new EditText (getApplicationContext());



et2.setBackgroundColor(color);
et3.setBackgroundColor(color);
et4.setBackgroundColor(color);
et5.setBackgroundColor(color);
et6.setBackgroundColor(color);

name.setText("Q#"+Integer.toString(j));





tableRow.addView(name);
tableRow.addView(et2);
tableRow.addView(et3);
tableRow.addView(et4);
tableRow.addView(et5);
tableRow.addView(et6);
table.addView(tableRow);

}
TableRow tableRow1 = new TableRow (getApplicationContext());

Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);

table.addView(tableRow1);



setContentView(table);

Answer

you may create it as below:

ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));

table.addView(tableRow1);
scroll.addView(table);
   setContentView(scroll);