Adding Scrollbar to Dynamic Table-View in Relative Layout - android

I have created dynamic table-layout dynamically in Relative-Layout. I am adding text-views dynamically. I have 9 fields to display. How can i put horizontal and vertical scroll bar in it ?
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
// Show the Up button in the action bar.
setupActionBar();
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;
............................
............................
............................
for (int i = 0; i < rows; i++)
{ //4 rows
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < columns; j++)
{ //3 columns
textView = new TextView(getApplicationContext());
textView.setText(temp[k++]);
textView.setPadding(15, 15, 15, 15);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
setContentView(scrollView);
setContentView(tableLayout);

Create your layout.xml file like this
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res/com.smartcloud.podcast_he"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_iphone" >
<ScrollView
android:id="#+id/ScrollView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headingtitle2" >
</ScrollView>
</RelativeLayout>
And then in code
setContentView(R.layout.activity_view);
// Show the Up button in the action bar.
setupActionBar();
ScrollView scrollview = (ScrollView) findViewById(R.id.ScrollView03);
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;
............................
............................
............................
for (int i = 0; i < rows; i++)
{ //4 rows
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < columns; j++)
{ //3 columns
textView = new TextView(getApplicationContext());
textView.setText(temp[k++]);
textView.setPadding(15, 15, 15, 15);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
scrollview.addView(tableLayout );
Remove this 2 lines
setContentView(scrollView);
setContentView(tableLayout);

Make your top level layout a ScrollView
scrollView.addView(tableLayout);
setContentView(scrollView);

Related

How to compare a textview outside of the table to a textview in a table row?

public void filter(TableLayout tl, String regex) {
TableRow tr;
TextView tv;
Pattern p;
Matcher m;
p = Pattern.compile(regex);
int n = tl.getChildCount();
for (int i = 0; i < n; i++) {
tr = (TableRow) tl.getChildAt(i);
tv = (TextView) tr.getChildAt(0);
m = p.matcher(tv.getText());
if (m.find()) {
tr.setVisibility(View.VISIBLE);
} else {
tr.setVisibility(View.GONE);
}
I have tried to do filter for a tablelayout on aftertextchanged . The value of the text is passed into filter method perfectly but tv = (TextView) tr.getChildAt(0); gives me android.widget.LinearLayout cannot be cast to android.widget.TextView. As most other forums show that there is an id for the textview as it is hardcoded , I did not manage to find one that solves the error of textview that is put in dynamically. Below is an example of what I add to the table layout which works peerfectly fine.
public void addData(ArrayList<Transactions> Transactions) {
tl.removeAllViewsInLayout();
addHeader();
int j = 1;
for (Iterator i = Transactions.iterator(); i.hasNext(); j++) {
Transactions p = (Transactions) i.next();
if (p.getAdminNo().equals(adminNo)) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(String.valueOf(j));
label.setId(p.getId());
acd = String.valueOf(p.getId());
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label, params);
tr.addView((View) Ll); // Adding textView to tablerow.
This is my xml file. When i put a value into my edittext, it will trigger filter method.
<?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" >
<EditText
android:layout_width="134dp"
android:layout_height="wrap_content"
android:id="#+id/search"
/>
<Spinner
android:id="#+id/spinner"
android:layout_width="244dp"
android:layout_height="44dp"
android:textColor="#00f0ff"
/>
<ScrollView
android:id="#+id/layout"
android:layout_height="match_parent"
android:scrollbars="horizontal|vertical"
android:layout_width="match_parent"
android:layout_marginTop="5dip"
android:scrollbarStyle="outsideInset"
android:fillViewport="true">
<HorizontalScrollView
android:id="#+id/horizontalView"
android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical"
android:layout_width="wrap_content"
android:layout_marginTop="5dip">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/maintable" >
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
The reason its throwing a cast exception is because the first item you are adding to the table view is the linearlayout
Ll.addView(label, params); //adding textview to linearlayout
tr.addView((View) Ll); // Adding linearlayout to tablerow.
Since its just an item you want to add to the tablerow, I dont think you need the linearlayout. What you can do is this:
public void addData(ArrayList<Transactions> Transactions) {
tl.removeAllViewsInLayout();
addHeader();
int j = 1;
for (Iterator i = Transactions.iterator(); i.hasNext(); j++) {
Transactions p = (Transactions) i.next();
if (p.getAdminNo().equals(adminNo)) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(String.valueOf(j));
label.setId(p.getId());
acd = String.valueOf(p.getId());
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
tr.addView((label); // Adding textView to tablerow.
}
}
But if you must add the linearlayout, you should get back the textview by doing this
public void filter(TableLayout tl, String regex) {
TableRow tr;
TextView tv;
Pattern p;
Matcher m;
p = Pattern.compile(regex);
int n = tl.getChildCount();
for (int i = 0; i < n; i++) {
tr = (TableRow) tl.getChildAt(i);
LinearLayout ll = (LinearLayout) tr.getChildAt(0);
tv = (TextView) ll.getChildAt(0);
m = p.matcher(tv.getText());
if (m.find()) {
tr.setVisibility(View.VISIBLE);
} else {
tr.setVisibility(View.GONE);
}
This should work. Hopefully it helps, goodluck

Programmatically generated TextViews print on each other

I'm generating TextViews in a loop and add them to a Relative layout like this :
RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++)
{
TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw);
for(int j = 0; j < 10; j++)
{
TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
relativeLayout2.addView(textvw2);
}
}
When I do this, all textViews print on the same place. I want them to appear below each other. I mean first one will be on the top, secone one below the top and like that. How can I do that? Thanks.
Use Linear layout instead of Relative layout and set the orientation to vertical if you want it one below the other.
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++) { TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw); for(int j = 0; j < 10; j++) { TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
linearLayout2.addView(textvw2); } }

how can i get Values from dynamically created andorid TableRow?

How can i get the data from the Table Row on the button Click Event from the selected Radio Button.
How i can set the Radio Group in a Radio button also when i write
rg.addView(rb); My Android Application is Closed Unfortunately.
My XML File code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dynamictablelayout.Round_Trip_Search_Result" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:id="#+id/tl"
android:stretchColumns="*" >
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tl"
android:id="#+id/ll"
android:orientation="vertical" >
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll"
android:id="#+id/tl1"
android:stretchColumns="*" >
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/ll1"
android:orientation="vertical" >
</LinearLayout>
my activity file
TextView tv;
RadioGroup rg, rg1;
RadioButton rb;
TableRow tr;
int cnt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_round__trip__search__result);
String[] str = { "Airindia", "Ai-101", "3.00", "2000", "Go Air",
"GA-101", "4.00", "2500" };
String[] str1 = { "Air India", "Ai-100", "5.00", "2500",
"Etihad Airways", "EI-200", "12.00", "3500" };
TableLayout tl = (TableLayout) findViewById(R.id.tl);
TableLayout tll = (TableLayout) findViewById(R.id.tl1);
LinearLayout l1 = (LinearLayout) findViewById(R.id.ll);
LinearLayout l11 = (LinearLayout) findViewById(R.id.ll1);
int k = 0;
int tr_id = 100;
for (int i = 0; i < str.length; i += 4) {
tr = new TableRow(this);
tr.setId(tr_id);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
tv = new TextView(this);
tv.setText(str[k]);
tr.setId(tr_id);
tr.addView(tv);
k++;
}
rg = new RadioGroup(this);
rb = new RadioButton(this);
//rg.addView(rb);
rb.setId(tr_id);
tr.addView(rb);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr_id++;
}
TextView rtn = new TextView(this);
rtn.setText("Flight from XXX to YYY");
l1.addView(rtn);
int h = 0;
for (int i = 0; i < str1.length; i += 4) {
tr = new TableRow(this);
tr.setId(tr_id);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
tv = new TextView(this);
tv.setText(str1[h]);
tr.setId(tr_id);
tr.addView(tv);
h++;
}
rg1 = new RadioGroup(this);
rb = new RadioButton(this);
// rg1.addView(rb);
rb.setId(tr_id);
tr.addView(rb);
tll.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr_id++;
}
Button b = new Button(this);
b.setText("book");
l11.addView(b);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Click",
Toast.LENGTH_SHORT).show();
int id = rb.getId();
Toast.makeText(getApplicationContext(), "ID =>" + id,
Toast.LENGTH_SHORT).show();
}
});
LogCat Error Occurs when i remove the line rg.addView(rb);:
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addView(ViewGroup.java:3249)
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addView(ViewGroup.java:3194)
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addView(ViewGroup.java:3170)
03-20 10:25:32.085: E/AndroidRuntime(6235): at com.example.dynamictablelayout.Round_Trip_Search_Result.onCreate(Round_Trip_Search_Result.java:61)
rg1.addView(rb);
rb.setId(tr_id);
tr.addView(rb);
In first and third lines above you are adding parents to the same element "rb" without removing its parent. A child(rb) cannot have two parents. You must call removeView() on the child's parent first.

How to create a full screen TableLayout programmatically (table height collapses unintendedly)

The following TableLayout matches exactly my requirements (it fills its parent and columns are stretched evenly):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7434a6" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#836492" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#103456" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#958453" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#756aa9" />
</TableRow>
</TableLayout>
However, as I want to have the number of rows being configurable I started to create my layout programmatically:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT, 1f);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(context);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}
Up to a certain point this works not bad as well. Only issue I have is, that my table collapses vertically:
What is wrong here? Thought my code is doing the same as my XML. I searched and tried quite a lot but didn't get the right idea.
Try this..
LinearLayout.LayoutParams tableParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams rowParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
LinearLayout.LayoutParams itemParams =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1f);
LinearLayout tableLayout = new LinearLayout(context);
tableLayout.setLayoutParams(tableParams);
tableLayout.setOrientation(LinearLayout.VERTICAL);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
LinearLayout tableRow = new LinearLayout(context);
tableRow.setOrientation(LinearLayout.HORIZONTAL);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(context);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
OR In your rowParams change TableRow to TableLayout and try.
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT, 1f);
TableLayout.LayoutParams rowParams =
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}

Adding rows and cells into TableLayout

How can I do the following:
Create a row in a TableLayout programmatically
Create three cells in the row
Add arbitrary text in each cell
Thanks for your help!
Try this..
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
tableLayout.setWeightSum(3);
for (int i = 0; i < 3; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
for (int j = 0; j < 3; j++) {
TextView button = new TextView(this);
final int buttonNumber = (j + i * 4);
button.setText("" + buttonNumber);
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
main_lay.addView(tableLayout);
TableLayout table = (TableLayout) findViewById(R.id.TableName);
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView cell1= new TextView(getApplicationContext());
cell1.setText("one");
cell1.setTextColor(Color.BLACK);
cell1.setTextSize(16f);
cell1.setTypeface(tf);
TextView cell2= new TextView(getApplicationContext());
cell2.setText("two");
cell2.setTextColor(Color.BLACK);
cell2.setTextSize(16f);
cell2.setTypeface(tf);
TextView cell3= new TextView(getApplicationContext());
cell3.setText("three");
cell3.setTextColor(Color.BLACK);
cell3.setTextSize(16f);
cell3.setTypeface(tf);
tableRow.addView(cell1);
tableRow.addView(cell2);
tableRow.addView(cell3);
talbe.addView(tableRow , new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
If you want you can use a function like this to update your tableLayout which adds a new row with some text.
private void addRowToTableLayout(TableLayout tableLayout, String text){
TableRow tr = new TableRow(MyActivity.this);
//if you want you can customize the table row
tr.setBackgroundColor(getResources().getColor(R.color.backgroundColor));
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView textView = new TextView(MyActivity.this);
//if you want you can customize the textview also
textView.setText(text);
tr.addView(textView);
tableLayout.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}

Categories

Resources