Reputation: 663
I want to replace a View in a LinearLayout programmatically. I tried the following method:
public void drawView() {
//remove previous view, if exists
if(((LinearLayout) findViewById(R.id.chartView)).getChildCount() > 0) {
((LinearLayout) findViewById(R.id.chartView)).removeAllViewsInLayout();
}
//generate new view
CustomView view = new CustomView(this,parameters);
//add new view to layout
((LinearLayout) findViewById(R.id.linLayout)).addView(view);
}
The LinearLayout that this method refers to is defined in XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linLayout">
</LinearLayout>
The view is drawn correctly the first time (when there is no view yet in the LinearLayout. But the second time drawView is called, the previous View gets removed, but no new view added. How can I replace this programmatically generated view programmatically?
Upvotes: 2
Views: 1220
Reputation: 3863
you are removing from this layout ((LinearLayout) findViewById(R.id.chartView)).removeAllViewsInLayout();
but adding to this: ((LinearLayout) findViewById(R.id.linLayout)).addView(view);
replace R.id.linLayout
with R.id.chartView
Upvotes: 3