I have a table layout with table rows being added dynamically from the database. I need to programmatically choose only one table row and deselect the previous choosen. Similar to a listview onItemClick. For some reasons I can't use a ListView.
You could add click listener on table rows. See the code below:
private int selectedIndex = -1;
private TableLayout tableLayout;
private void initTableLayout() {
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer index = (Integer) v.getTag();
if (index != null) {
selectRow(index);
}
}
};
tableLayout = (TableLayout) findViewById(R.id.table_layout);
final TableRow tr = new TableRow(this);
tr.setTag(0);
tr.setOnClickListener(clickListener);
// Add views on tr
tableLayout.addView(tr, 0, new TableLayout.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
final TableRow tr2 = new TableRow(this);
tr2.setTag(1);
tr2.setOnClickListener(clickListener);
// Add views on tr2
tableLayout.addView(tr2, 1, new TableLayout.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
....
}
private void selectRow(int index) {
if (index != selectedIndex) {
if (selectedIndex >= 0) {
deselectRow(selectedIndex);
}
TableRow tr = (TableRow) tableLayout.getChildAt(index);
tr.setBackgroundColor(Color.GRAY); // selected item bg color
selectedIndex = index;
}
}
private void deselectRow(int index) {
if (index >= 0) {
TableRow tr = (TableRow) tableLayout.getChildAt(index);
tr.setBackgroundColor(Color.TRANSPARENT);
}
}
Related
I am trying to create a simple table programmatically. But the code crashes when row2.addView is called. With this error:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Here is my code:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.contnt_2);
TableLayout tableLayout = new TableLayout(this);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
TableRow row3 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("DDDDDDDDDDDDD");
text1.setBackgroundColor(Color.GRAY);
TextView text2 = new TextView(this);
text2.setText("DDDDDDDDDDDDD");
text2.setBackgroundColor(Color.GRAY);
TextView text3 = new TextView(this);
text3.setText("DDDDDDDDDDDDD");
text3.setBackgroundColor(Color.GRAY);
row1.addView(text1, tableRowParams);
row1.addView(text2, tableRowParams);
row1.addView(text3, tableRowParams);
row2.addView(text1, tableRowParams);
row2.addView(text2, tableRowParams);
row2.addView(text3, tableRowParams);
row3.addView(text1, tableRowParams);
row3.addView(text2, tableRowParams);
row3.addView(text3, tableRowParams);
tableLayout.addView(row1);
tableLayout.addView(row2);
tableLayout.addView(row3);
setContentView(tableLayout);
}
I can't find a reason why it happens. how correctly to add rows to table ? Can you help me out?
This is because you created three TextViews and added them to your first TableRow with this code:
row1.addView(text1, tableRowParams);
row1.addView(text2, tableRowParams);
row1.addView(text3, tableRowParams);
Then tried adding that same TextView that you already created to Row 2.
You can't add the same TextView to multiple TableRows, because as the error says, each TextView can only have one parent.
You need to create three different TextViews for EACH TableRow.
The main problem with your approach: A view can't be a child of more than one parent in Android.
Here's a tutorial on how to create a TableLayout programmatically, maybe you get some ideas for improvements to your logic:
http://www.prandroid.com/2014/05/creating-table-layout-dynamically-in.html
I'll paste it here as well just in case:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
"Row 7" };
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
"COLUMN5", "COLUMN6" };
int rl=row.length; int cl=column.length;
Log.d("--", "R-Lenght--"+rl+" "+"C-Lenght--"+cl);
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
// make it black
textView.setBackgroundColor(Color.BLACK);
}
public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex){
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
textView.setText("Hello");
}
private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount) {
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j= 0; j < columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>"+id);
if (i ==0 && j==0){
textView.setText("0==0");
} else if(i==0){
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j-1]);
}else if( j==0){
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i-1]);
}else {
textView.setText(""+id);
// check id=23
if(id==23){
textView.setText("ID=23");
}
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
I want to add radiobuttons for every cell in given below table and get their position based on row and column, how to achieve it? Kindly share your views. Thanks in advance. I have commented the code where i was adding text to each cells, may be at the same place we can add radiobuttons
Below is working code :
public class MainActivity extends Activity {
RelativeLayout rl;
RadioGroup rg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] row = { "AA", "BB", "CC", "DD", "EE", "FF", "GG" };
String[] column = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 6" };
int rl = row.length;
int cl = column.length;
Log.d("--", "R-Lenght--" + rl + " " + "C-Lenght--" + cl);
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column, rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView) tableRow.getChildAt(columnIndex);
// make it black
textView.setBackgroundColor(Color.BLACK);
}
public void setHeaderTitle(TableLayout tableLayout, int rowIndex,
int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView) tableRow.getChildAt(columnIndex);
textView.setText("Hello");
}
private TableLayout createTableLayout(String[] rv, String[] cv,
int rowCount, int columnCount) {
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
final RadioButton[] rb = new RadioButton[10];
rl=(RelativeLayout) findViewById(R.id.rl);
rg=new RadioGroup(this);
for (int j = 0; j < columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>" + id);
if (i == 0 && j == 0) {
textView.setText("0==0");
} else if (i == 0) {
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j - 1]);
} else if (j == 0) {
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i - 1]);
} else {
/*textView.setText("" + id);
// check id=23
if (id == 23) {
textView.setText("ID=23");
}*/
//Add Radiobuttons here
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
please use listview instead of table layout
ListView listView = (ListView) view.findViewById(R.id.listview);
// values is a StringArray holding some string values.
CustomAdapter customAdapter = new CustomAdapter (getActivity(), values);
listView.setAdapter(customAdapter );
Use multi-dimensional array of Radio Button or group or whatever you want...
RadioButton[][] rb = new RadioButton[][];
Use above statement for declaring it and for accessing it use two loops which are nested. One loop is nested in another.
for(int i=0;i<=5;i++)
{
for (int j=0;j<=5;j++)
{
rb[i][j] = new RadioButton(this);
}
}
Hello i used a code to generate some buttons in a tablelayout. The buttons are generated by this layout, so none of them has an id yet. So here is my question. How can i give a specific button an id? (For example the button in the first row, second colum should have the id "button2")
public class MainActivity extends ActionBarActivity {
private static final int NUM_ROWS = 3;
private static final int NUM_COLS = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateButtons();
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
for (int row =0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COLS; col++) {
Button button = new Button(this);
tableRow.addView(button);
}
}
}
You can use View.setId(), however I guess you don't have to find the Views by id because you already know them.
I am getting data from server and create the tablelayout dynamically for that response, now I need to show only 5 rows per page, I am using setvisible(view.invisible) to hide all the table rows, How to set the 5 rows setvisible(view.visible) to show the rows like pagination ?
The code I used to create the table dynamically is below.
TableLayout tableLayout = (TableLayout) findViewById(R.id.tablenew);
TableRow tableRow = new TableRow(this);
tableRow.setId(1);
tableRow.setBackgroundColor(Color.LTGRAY);
tableRow.setPadding(0,0,1,1);
tableRow.setLayoutParams(new TableLayout.LayoutParams());
tableRow.setVisibility(View.INVISIBLE);
TextView text = new TextView(this);
text.setText(name);
text.setBackgroundColor(Color.WHITE);
text.setPadding(5, 5, 5, 5);
text.setTextSize(30);
text.setLayoutParams(new TableRow.LayoutParams());
final Button button = new Button(this);
button.setPadding(5, 5, 5, 5);
button.setTextSize(30);
button.setTag(value);
button.setText("Install");
button.setLayoutParams(new TableRow.LayoutParams());
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableLayout tableLayout = (TableLayout) findViewById(R.id.tablenew);
final TableRow parent = (TableRow) v.getParent();
tableLayout.removeView(parent);
String id=(String) v.getTag();
}
});//button click listener
tableRow.addView(text);
tableRow.addView(button);
tableLayout.addView(tableRow);
Try this, they have explained with example code..
try this code:
tableraw.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tablenew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/next"
android:layout_alignParentTop="true" >
</TableLayout>
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="next" />
</RelativeLayout>
TableViewDemo.java
package com.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TableViewDemo extends Activity {
int index = 5;
Button nextButton;
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tableraw);
tableLayout = (TableLayout) findViewById(R.id.tablenew);
nextButton = (Button) findViewById(R.id.next);
for (int i = 1; i <= 20; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setTag(i);
tableRow.setBackgroundColor(Color.LTGRAY);
tableRow.setPadding(0, 0, 1, 1);
tableRow.setLayoutParams(new TableLayout.LayoutParams());
TextView text = new TextView(this);
text.setText("name" + i);
text.setBackgroundColor(Color.WHITE);
text.setPadding(5, 5, 5, 5);
text.setTextSize(30);
text.setLayoutParams(new TableRow.LayoutParams());
final Button button = new Button(this);
button.setPadding(5, 5, 5, 5);
button.setTextSize(30);
button.setTag("value");
button.setText("Install" + i);
button.setLayoutParams(new TableRow.LayoutParams());
tableRow.addView(text);
tableRow.addView(button);
if (i < 6) {
tableRow.setVisibility(View.VISIBLE);
} else {
tableRow.setVisibility(View.GONE);
}
tableLayout.addView(tableRow);
}
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < 20; i++) {
TableRow parent = (TableRow) tableLayout.getChildAt(i);
// tableLayout.removeView(parent);
Integer id = ((Integer) parent.getTag());
if (id > index && id <= (index + 5)) {
parent.setVisibility(View.VISIBLE);
} else {
parent.setVisibility(View.GONE);
}
}
index = index + 5;
}
});
}
}
prev.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableLayout tl = (TableLayout) findViewById(R.id.tablenew);
for (int i = 10; i < tl.getChildCount(); i--)
{
if(i>0)
{
TableRow row = (TableRow) tl.getChildAt(i);
Integer id = ((Integer) row.getTag());
if (id < 11 && id <= (10 - 5)) //index - 5)) {
{
Log.d("iiiiiiiiiiiiiiiii",Integer.toString(i));
row.setVisibility(View.VISIBLE);
}
else
{
Log.d("aaaaaaaaaaaaaaaaaaa",Integer.toString(i));
row.setVisibility(View.GONE);
}
}
}
//index = index - 5;
}
});
prev.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableLayout tl = (TableLayout) findViewById(R.id.tablenew);
for (int i = 6; i > 0; i--)
{
Log.d("aaaaaaaaaaaaaaaaaaa",Integer.toString(index));
TableRow row = (TableRow) tl.getChildAt(i);
Integer id = ((Integer) row.getTag());
if (id < index && id <= (index - 3)) //index - 3)) {
{
Log.d("iiiiiiiiiiiiiiiii",Integer.toString(i));
row.setVisibility(View.VISIBLE);
}
else
{
Log.d("aaaaaaaaaaaaaaaaaaa",Integer.toString(i));
row.setVisibility(View.GONE);
}
}
index = index - 3;
}
});
public class Tabledemo extends Activity {
int index = 5;
int i,preval,preival;
int nexval=1;
Button prev,next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabledemo);
session = new SessionManager(getApplicationContext());
prev = (Button) findViewById(R.id.pre);
next = (Button) findViewById(R.id.nex);
prev.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableLayout tl = (TableLayout) findViewById(R.id.tablenew);
for (i = preval ; i > preival; i--)
{
TableRow row = (TableRow) tl.getChildAt(i);
Integer id = ((Integer) row.getTag());
if (id < index && id <= (index - 5))
{
row.setVisibility(View.VISIBLE);
nexval=i;
}
else
{
row.setVisibility(View.GONE);
}
}
preival=preival-5;
index = index - 5;
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
TableLayout tl = (TableLayout) findViewById(R.id.tablenew);
for (i = nexval; i < tl.getChildCount() ; i++)
{
TableRow row = (TableRow) tl.getChildAt(i);
Integer id = ((Integer) row.getTag());
if (id > index && id <= (index + 5))
{
row.setVisibility(View.VISIBLE);
preval=i;
}
else
{
row.setVisibility(View.GONE);
}
}
preival=index;
preival=preival-5;
index = index + 5;
}
});
final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}