I created an application in which I dynamically add and remove textView to LinearLayout every time I click a button.
My problem is that when the screen orientation changes, which restarts the activity, all added text elements disappear. I do not know how to maintain the inflated state of LinearLayout .
This is the part of the code where I initialize the views and buttons:
private LayoutInflater inflater; private LinearLayout ll; View view; Button add; Button delete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inflater = getLayoutInflater(); ll = (LinearLayout)findViewById(R.id.ll); add = (Button)findViewById(R.id.bAdd); delete = (Button)findViewById(R.id.bDelete); add.setOnClickListener(this); delete.setOnClickListener(this);
and with the onClick method onClick I add or remove textViews:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.bAdd: { view = inflater.inflate(R.layout.sublayout,ll,true); break; } case R.id.bDelete: { int childSize = ll.getChildCount(); if(0 != childSize) { ll.removeViewAt(childSize -1); } Log.i("InflateLayout", "childsize: " +childSize); } } }
source share