Dynamically added views disappear on orientation in Android
android, dynamic, layout-inflater, orientation, view
Solution
Add this line in your manifest.xml Tags.
android:configChanges="keyboardHidden|orientation|screenSize"
This will avoid your activity from recreating on orientation change.
Problem
I have created the views to be added in dynamically to a layout from a button click, but when I rotate the device landscape, the views disappear. Could someone please look at my code and explain how to stop this from happening. Here is the code: ``` public class TestContainerActivity extends Activity implements OnClickListener { LinearLayout containerLayout; Button testButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_container); testButton = (Button)findViewById(R.id.testContainerButton1); testButton.setOnClickListener(this); containerLayout = (LinearLayout)findViewById(R.id.testContainerLayout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.test_container, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v==testButton){ createNewLayout(); } } public void createNewLayout(){ LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View addView = layoutInflater.inflate(R.layout.container, null); TextView textviewTest = (TextView)addView.findViewById(R.id.containerTextView4); textviewTest.setText("TextView"); containerLayout.addView(addView); } } ```