How to add radiobutton to each cell in table layout - android

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

Related

Select only one table row and Deselect others

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

TableLayout - The specified child already has a parent

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

Unable to get row id of dynamically added rows in table layout of android

Here is my code to add table rows dynamically,`
for (int j = 0; j < list.size(); j++) {
TableRow tableRow = new TableRow(getActivity());
table_layout.addView(tableRow);
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow.setId(j);
tableRow.setClickable(true);
tableRow.setOnClickListener(tablerowOnClickListener);
TextView textView0 = new TextView(getActivity());
textView0.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
textView0.setText(currencySymbol.get(j));
textView0.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CLIP_VERTICAL);
tableRow.addView(textView0);
TextView textView1 = new TextView(getActivity());
textView1.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
textView1.setGravity(Gravity.RIGHT);
tableRow.addView(textView1);
}
and here is onclicklistener
OnClickListener tablerowOnClickListener = new OnClickListener() {
public void onClick(View v) {
//GET TEXT HERE
//String currenttext = ((TextView)v).getText().toString());
Toast.makeText(getActivity(), "row selected" + View.getId , 30).show();
}
};
however i am not getting the row id by this approach, i am referring this question and this one also.
You need to use v.getId() to get ID of clicked view inside OnClickListner.
Replace this
Toast.makeText(getActivity(), "row selected" + View,getId , 30).show();
By this one
Toast.makeText(getActivity(), "row selected --> " + v.getId() , 30).show();

tablelayout delete tablerow in android

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

table row selection error in scrollview

i am creating a table layout with radio group by dynamically adding radiobuttons to it and i want to know which row is selected containing the radio group and i want to retrive the texview data also in the same row .how can i do that any suggestion will be a great help for me.
public class TabActivity extends Activity {
TableLayout table;
RadioGroup mRadioGroup;
ArrayList<String> list_name;
int color_blue = -16776961;
int color_gray = -7829368;
int color_black = -16777216;
int color_white = -1;
final int CHECK_BUTTON_ID = 982301;
int ids_check[];
boolean bool_check[];
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
table = (TableLayout) findViewById(R.id.tableLayout1);
list_name = new ArrayList<String>();
list_name.add("Close");
list_name.add("Cristiano");
list_name.add("David");
list_name.add("Fernando");
list_name.add("Messi");
list_name.add("Kaka");
list_name.add("Wayne");
list_name.add("use");
list_name.add("e");
list_name.add("eff");
list_name.add("euyr");
list_name.add("ejjyytuty");
list_name.add("madre");
list_name.add("yuir");
list_name.add("eyrty");
list_name.add("etytr");
list_name.add("ewrrtt");
bool_check = new boolean[list_name.size()];
ids_check = new int[list_name.size()];
createTableRows();
}
public void createTableRows()
{
for (int i = 0; i < list_name.size(); i++)
{
final TableRow table_row = new TableRow(this);
TextView tv_name = new TextView(this);
Button btn_check = new Button(this);
ImageView img_line = new ImageView(this);
table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table_row.setBackgroundColor(color_black);
table_row.setGravity(Gravity.CENTER_HORIZONTAL);
// table_row.setFocusable(true);
mRadioGroup = new RadioGroup(this);
// test adding a radio button programmatically
final RadioButton[] mbutton=new RadioButton[7];
for(int l=0;l<7;l++){
mbutton[l]=new RadioButton(this);
mbutton[l].setText("test"+l);
mRadioGroup.addView(mbutton[l]);
}
// LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
// RadioGroup.LayoutParams.WRAP_CONTENT,
// RadioGroup.LayoutParams.WRAP_CONTENT);
// mRadioGroup.addView(newRadioButton);
tv_name.setText((CharSequence) list_name.get(i));
tv_name.setTextColor(color_blue);
tv_name.setTextSize(30);
tv_name.setTypeface(Typeface.DEFAULT_BOLD);
tv_name.setWidth(150);
btn_check.setLayoutParams(new LayoutParams(30, 30));
btn_check.setBackgroundResource(R.drawable.small_checkbox_unchecked);
img_line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
img_line.setBackgroundResource(R.drawable.separater_line);
table_row.addView(tv_name);
table_row.addView(btn_check);
table_row.addView(mRadioGroup);
table.addView(table_row);
table.addView(img_line);
//table.addView(mRadioGroup);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
for(int i=0; i<mRadioGroup.getChildCount(); i++) {
RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
int t=table.indexOfChild(table_row);
System.out.println(t);
if(btn.getId() == checkedId) {
String text = btn.getText().toString();
// do something with text
Log.d(text," event1");
return;
}
}
}
});
}
}
}
Why are you not using ListView ? there's nothing in the code that cannot be done by a list view?
You can add you layout by setting the custom adapter.

Categories

Resources