I made an application where I dynamically add and delete a textView to a LinearLayout
every time a button is pressed.
My problem is that when the screen orientation changes, which re-starts the activity, all the textViews added disappear. I don't know how to retain the LinearLayout
inflated state.
This is the part of the code where I initialize the views and the 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 on the onClick
method I add or delete the 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);
}
}
}