Removing TableRows programmatically - android

I have successfully added a TableRow to the TableLayout,now I would like to programmatically remove the TableRow and add the original row to the TableLayout.Here is the code that I use to generate the TableRow:
public TableRow getTableRow(String text,String hint,boolean addCollapseOption)
{
TableRow tr=new TableRow(getBaseContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
TextView temp=new TextView(getBaseContext());
temp.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
temp.setText(text);
tr.addView(temp);
tempId="edit_"+count;
count++;
EditText edit_temp=new EditText(getBaseContext());
TableRow.LayoutParams edit_params=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1f);
edit_params.setMargins(10, 0, 10, 0);
edit_temp.setLayoutParams(edit_params);
edit_temp.setHint(hint);
tr.addView(edit_temp);
if(addCollapseOption)
{
ImageButton btn_less=new ImageButton(getBaseContext());
btn_less.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
btn_less.setImageDrawable(getResources().getDrawable(R.drawable.icon_less));
btn_less.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
shouldCollapse=true;
}
});
tr.addView(btn_less);
}
return tr;
}
The Activity crashes when I try to add this TableRow to the TableLayout:
TableRow tr=getTableRow("First Name","Enter first name here",true);
I guess you cannot remove a View from the Layout and still be able to use the click event in it(that crashes the activity),layout and shouldCollapse are global variables,if set to true:
if(shouldCollapse)
{
layout.removeAllViews();
layout.addView(originalRow);
}
EDIT:Changing the implementation to use removeChildAt does not work:
if(shouldCollapse)
{
int childCount=layout.getChildCount();
for(int i=1;i<childCount;i++)
layout.removeViewAt(i);
originalRow.setVisibility(TableRow.VISIBLE);
shouldCollapse=false;
}
Now,clicking on the btn_less does absolutely nothing.This is the Logcat's opinion of the problem:
Less clicked
The value of shouldCollapse true
So,this means that even though shouldCollapse is set to true but this method is never called...strange.

You can use this code to remove the programmatically generated TableRows:
public void onClick(View v)
{
final TableRow parent = (TableRow) v.getParent();
tr.removeView(parent);
}
Or if you want to use this out side the onClick, you must pass the value of View v to whatever function it is.

Related

update dynamic tablelayout in scrollview using a button

I have created dynamically a LinearLayout and inside a HorizontalScrollView and I have placed insite a TableLayout (my table is too big an I need to scroll). When first created my table has a row as a header and a row for the user to input data. Outside the HorizontalScrollView but inside the LinearLayout I want to add 2 buttons. One for adding a row and one for deleting rows. I setup onclickListener but it did not work. My code is :
LinearLayout linearLayout = new LinearLayout(findViewById(R.id.internal_nodes_layout).getContext());
HorizontalScrollView scrollView1 = new HorizontalScrollView(findViewById(R.id.opaque_scroll_view).getContext());
TableLayout in_opaque_table = new TableLayout(findViewById(R.id.opaque_table).getContext());
LinearLayout opaquebuttonsLayout = new LinearLayout(findViewById(R.id.opaque_buttons_layout).getContext());
TableRow in_opaque_table_HeaderRow = new TableRow(this);
in_opaque_table_HeaderRow.setLayoutParams(tlparams);
//.............. code for header...............
createRowInOpaqueTable(in_opaque_table, in_opaque_table_num_of_rows, tlparams);
scrollView1.addView(in_opaque_table);
opaquebuttonsLayout.addView(addInOpaque);
opaquebuttonsLayout.addView(removeInOpaque);
linearLayout.addView(scrollView1);
linearLayout.addView(opaquebuttonsLayout);
//setup buttons and onClickListeners
Button addInOpaque = new Button(this);
addInOpaque.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addInOpaqueRow(view);
}
});
addInOpaque.setText(R.string.add_line);
addInOpaque.setAllCaps(false);
Button removeInOpaque = new Button(this);
removeInOpaque.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
removeInOpaqueRow(view);
}
});
public void addInOpaqueRow(View view) {
createRowInOpaqueTable(in_opaque_table, in_opaque_table_num_of_rows, tlparams);
in_opaque_table_num_of_rows++;
}
public void removeInOpaqueRow(View view) {
if (in_opaque_table_num_of_rows > 0) {
in_opaque_table.removeViewAt(in_opaque_table_num_of_rows);
in_opaque_table_num_of_rows--;
} else {
Toast.makeText(this, "Δεν υπάρχουν γραμμές στον πίνακα", Toast.LENGTH_SHORT).show();
}
}
Although it appears the way I want to the add and remove buttons won't work.
I get this message:
E/libfingersense_wrapper: libfingersense.so was not loaded
but I see that rows count correct when I use add and remove buttons. Any ideas??

Android layout doesn't refresh after calling removeAllViews()

I have a Linearlayout called "resultView". I then dynamically added many textViews in it after clicking a button.
I want to remove all the textviews that I just created when I click the button again.
btn_search.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View view) {
resultView.removeAllViews();
String strFileName = et_fileName.getText().toString();
searchFiles(strFileName);
}
});
public void searchFiles(String strFileName){
....
for (int i = 0; i < fileList.size(); i++) {
TextView textView = new TextView(this);
textView.setText(fileList.get(i).getName());
textView.setPadding(5, 5, 5, 5);
if (fileList.get(i).isFile())
resultView.addView(textView);
}
}
App Screenshot, All the listed results are dynamically created textViews.
My XML, the id "view" is my resultView.
But resultView.removeAllViews(); doesn't work. The results are still appened.
Calling resultView.invalidate(); after that doesn't work either.
What should I do to make the layout refresh?
Use .invalidate() to upddate UI.

setOnClickListener not working

I've tried to follow the several other times this question has been asked but all other reports of what's working seem to be the same as mine, except mine doesn't work. It compiles and runs and doesn't crash, but it simply does not do what it's supposed to do. In the code, the "Alpha" error log DOES NOT occur, so it is not recognizing that it's clicked.
Here's the relevant snippet of code. Any ideas?
for(int i=0; i<[big long statement]; ++i)
{
final TextView resourceText= new TextView(ctx);
resourceText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
resourceText.setText([big long statement]);
resourceText.setTextSize(18);
resourceText.setClickable(true);
resourceText.setFocusable(false);
resourceText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Alpha", "Alpha");
resourceText.setTextColor(Color.RED);
}
});
scrollLinearLayout.addView(resourceText);
}
This is where scrollLinearLayout is created
//Add Linear Layout for the scrollview
scrollLinearLayout = new LinearLayout(ctx);
scrollLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
scrollLinearLayout.setOrientation(LinearLayout.VERTICAL);
//scrollLinearLayout.setId(MyR.Ids.ROOMDIALOGFRAGMENTLL_ID);
resourceScrollView.addView(scrollLinearLayout);
Thanks
Maybe you should move the listener away from loop :
for(int i=0; i<[big long statement]; ++i)
{
final TextView resourceText= new TextView(ctx);
resourceText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
resourceText.setText([big long statement]);
resourceText.setTextSize(18);
resourceText.setClickable(true);
resourceText.setFocusable(false);
resourceText.setOnClickListener(onClickListener);
scrollLinearLayout.addView(resourceText);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Alpha", "Alpha");
((Button)v).setTextColor(Color.RED);
}
};
it is because your linearlayout encompasses the textview, resourceText. If you allow the LinearLayout to consume the clicks, then the textview will never see a click. You need to Try adding android:descendantFocusability="afterDescendents" to the LinearLayout

How to click an specific TableRow within a TableLayout

I made my own compound control that display a data grid using TableLayout and adding programmatically Tablerows within a loop depending of the Array of Object that im binding to it and now i want to select an specific row with its specific data in order to be used by a method. So how can i select an specific row retrieving its data to delegate a method?
hi you can try something like this,
// create a new TableRow
TableRow row = new TableRow(this);
row.setClickable(true); //allows you to select a specific row
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
System.out.println("Row clicked: " + v.getId());
//get the data you need
TableRow tablerow = (TableRow)v.getParent();
TextView sample = (TextView) tablerow.getChildAt(2);
String result=sample.getText().toString();
}
});
For more info refer Android TableRow
I tried Parth Doshi's answer and found it to be not quite correct. The view parameter in onClick is a TableRow, so when v.getParent() is called, it returns a TableLayout object, and so an exception is thrown when casting it to TableRow. As such the code that works for me is:
tableRow.setClickable(true); //allows you to select a specific row
tableRow.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result=sample.getText().toString();
Toast toast = Toast.makeText(myActivity, result, Toast.LENGTH_LONG);
toast.show();
}
});

How to delete a row in TableLayout dynamically

I am having a TableLayout for which I added rows dynamically.In each of the row there are 2 elements of which one is TextView other is Button.When I click the button that is present in a row,that row should be deleted.How can this be done in Android ? How to find rowid and how to delete a row dynamically.
can anyone help me in sorting out this issue.
Thanks in Advance,
Try this kind of approach:
TableRow tableRow;
TextView tv;
Button button;
//...
tableRow.addView(tv)
tableRow.addView(button);
//somewhere where you want to delete
ViewGroup parent=button.getParent();
if(parent instanceof TableRow)
//do deleting task
Try this:
public void onClick(View v) {
// TODO Auto-generated method stub
TableRow t = (TableRow) v.getParent();
TextView firstTextView = (TextView) t.getChildAt(0);
code = firstTextView.getText().toString();
System.out.println("code>>>>>>" + code);
View row = (View) v.getParent();
// container contains all the rows, you could keep a
// variable somewhere else to the container which you
// can refer to here
ViewGroup container = ((ViewGroup) row.getParent());
// delete the row and invalidate your view so it gets
// redrawn
container.removeView(row);
container.invalidate();
}
You can assign tags or id's when you adding a row. Then just use that tag/id to delete that row.
TableLayout table; // global access, probably initialized in onCreate()
// initialization, etc.
Create element that will be added to the TableLayout, a TableRow with the TextView and the Button then call addDeleteClick(yourButton, uniqueTag) before adding it to the TableLayout.
// example of adding a text view and a button the the TableLayout
void addToTableLayout(String text, String uniqueTag) {
TableRow tr = new TableRow(yourActivity);
// set the unique tag that will be used when deleting this row
tr.setTag(uniqueTag);
// do what you need with the button and textview
TextView tv = new TextView(yourActivity);
tv.setText(text);
Button bt = new Button(yourActivity);
// add delete click capability to the button
addDeleteClick(bt, uniqueTag);
table.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
// Adds the delete on click capability to a button
// to delete a row from TableLayout based on its tag
void addDeleteClick(Button bt, final String tag) {
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d("TableLayout", " deleting row with tag " + tag);
deleteRow(tag);
}
});
}
// delete a row from TableLayout based on its tag
void deleteRow(String tag) {
View removedRow = table.findViewWithTag(tag);
table.removeView(removedRow);
table.invalidate();
}

Categories

Resources