Sarmad Aijaz Sarmad Aijaz - 24 days ago 7
Android Question

ANDROID: Delete textviews which are created dynamically

I have added multiple

TextViews
dynamically in a layout,

for(int x=4;x<result.length();x++)
{
JSONObject collegeData = result.getJSONObject(x);
Log.i("Classlist",""+x);
TextView tv = new TextView(this);
Animation animation = AnimationUtils.loadAnimation(student_profile.this, android.R.anim.slide_in_left);
tv.startAnimation(animation);
tv.setTag(tag);
tv.setLayoutParams(lparams);
tv.setText(collegeData.getString("date") + " " + collegeData.getString("day_name"));
tv.setTextSize(17);
this.linearLayout_top5classes.addView(tv);

}


This loop adds
textViews
according to the data received by the url,Now i want to remove the textviews which were created in this loop and i cant find a proper method to do so....I only want to remove these textviews and not all the textviews

UPDATE

First i used

int prv=0;


then

String tag ="textView_"+x;
prv++;


in the first loop to generate multiple tags
then i removed them with

for(int x=4;x<prv;x++)
{
View view = this.linearLayout_top5classes.findViewWithTag("textView_"+x);
this.linearLayout_top5classes.removeView(view);
Log.i("prv value",prv+"");
}

Answer

Of course there is a way. Just look for child views with tag:

View view = this.linearLayout_top5classes.findViewWithTag(tag);
this.linearLayout_top5classes.removeView(view);

If you add ID's to child views, then:

View view = this.linearLayout_top5classes.findViewById(id);
this.linearLayout_top5classes.removeView(view);