In my android application I dynamically create one TableRow and I insert one textView and one editTextView in this tableRow.
there is one more filed which is spinner. Depending on the selection on spinner this TableRow is create or remove.
Here they mention how to remove fields from layout but the problem is how I get the view.
Here my code is:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
if (pos > 1) {
tableRowName = new TableRow(this);
tableRowName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textViewName = new TextView(this);
textViewName.setText("Name*");
textViewName.setTextColor(Color.WHITE);
textViewName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
editTextPersonName = new EditText(this);
editTextPersonName.setHint("Name");
editTextPersonName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tableRowName.addView(textViewName);
tableRowName.addView(editTextPersonName);
tableLayout.addView(tableRowName, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
tableRowName.removeView((View) v.getParent());
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(),
"onNothingSelected() method is called by Stage Spinner",
Toast.LENGTH_LONG).show();
}
});
This code able to create tableRow but not able to remove this tableRow.
You could better have the view already added in layout xml and then set visibility to GONE or VISIBLE dynamically.