Search code examples
androidlogcattablelayout

TableLayout gets empty after phone position is changed


I got problem with my TableLayout using code - adding rows dynamically.

Whenever I change position of my phone from horizontal to vertical or vertical to horizontal all the data that was inputted to my table is getting removed and table is empty. The only things that are left are column headers (TextViews) with names coded in my program.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TableLayout tableOne = (TableLayout) findViewById(R.id.dataTable1);
    tableOne.setId(110);
    tableOne.setBackgroundColor(Color.WHITE);

    TableRow namesRow = new TableRow(MainActivity.this);
    namesRow.setId(111);
    namesRow.setBackgroundColor(Color.GRAY);
    namesRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


    TextView productName = new TextView(MainActivity.this);
    productName.setId(120);
    productName.setPadding(2, 0, 40, 5);
    productName.setTextSize(14);
    productName.setTextColor(Color.BLACK);
    productName.setText("Product Name");
    namesRow.addView(productName);

    TextView concentration = new TextView(MainActivity.this);
    concentration.setId(130);
    concentration.setPadding(0, 0, 40, 5);
    concentration.setTextColor(Color.BLACK);
    concentration.setTextSize(14);
    concentration.setText("Concentration");
    namesRow.addView(concentration);

    TextView packages = new TextView(MainActivity.this);
    packages.setId(140);
    packages.setPadding(0, 0, 40, 5);
    packages.setTextSize(14);
    packages.setTextColor(Color.BLACK);
    packages.setText("Packages");
    namesRow.addView(packages);

    TextView volume = new TextView(MainActivity.this);
    volume.setId(150);
    volume.setPadding(0, 0, 40, 5);
    volume.setTextSize(14);
    volume.setTextColor(Color.BLACK);
    volume.setText("Volume");
    namesRow.addView(volume);

    TextView totalamount = new TextView(MainActivity.this);
    totalamount.setId(160);
    totalamount.setPadding(0, 0, 40, 5);
    totalamount.setTextSize(14);
    totalamount.setTextColor(Color.BLACK);
    totalamount.setText("Total Mass");
    namesRow.addView(totalamount);

    TextView sacksnumber = new TextView(MainActivity.this);
    sacksnumber.setId(170);
    sacksnumber.setPadding(0, 0, 40, 5);
    sacksnumber.setText("Number of Sacks");
    sacksnumber.setTextColor(Color.BLACK);
    sacksnumber.setTextSize(14);
    namesRow.addView(sacksnumber);

    tableOne.addView(namesRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

The above code is fine and never disappears but below code when I create each row from data from EditText is disappearing once I change position of the code. Kind of like it resets...

Button sncbtn1 = (Button) findViewById(R.id.sncbtn);

    sncbtn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText dataName = (EditText) findViewById(R.id.productField);
            EditText dataConc = (EditText) findViewById(R.id.concField);
            EditText dataPack = (EditText) findViewById(R.id.packageField);
            EditText dataVol = (EditText) findViewById(R.id.volField);

            String prodName = dataName.getText().toString();
            String concValue = dataConc.getText().toString();
            String packValue = dataPack.getText().toString();
            String volValue = dataVol.getText().toString();

            double concDoub = Double.parseDouble(concValue);
            double packDoub = Double.parseDouble(packValue);
            double volDoub = Double.parseDouble(volValue);

            double massResult = volDoub * concDoub;
            double sacksResult = massResult / packDoub;

            System.out.println(prodName + " | " + concValue + " | " + packValue + " | " + volValue);

            TableRow dataRow = new TableRow(MainActivity.this);

            dataRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            if(tableOne.getChildCount() % 2 == 0) dataRow.setBackgroundColor(Color.LTGRAY);

            TextView prodTable = new TextView(MainActivity.this);
            prodTable.setText(prodName);
            prodTable.setTextSize(14);
            prodTable.setTextColor(Color.BLACK);
            prodTable.setGravity(Gravity.CENTER);
            dataRow.addView(prodTable);

            TextView concTable = new TextView(MainActivity.this);
            concTable.setText(concValue);
            concTable.setTextColor(Color.BLACK);
            concTable.setTextSize(14);
            concTable.setGravity(Gravity.CENTER);
            dataRow.addView(concTable);

            TextView packTable = new TextView(MainActivity.this);
            packTable.setText(packValue);
            packTable.setTextSize(14);
            packTable.setTextColor(Color.BLACK);
            packTable.setGravity(Gravity.CENTER);
            dataRow.addView(packTable);

            TextView volTable = new TextView(MainActivity.this);
            volTable.setText(volValue);
            volTable.setTextSize(14);
            volTable.setTextColor(Color.BLACK);
            volTable.setGravity(Gravity.CENTER);
            dataRow.addView(volTable);

            TextView massTable = new TextView(MainActivity.this);
            massTable.setText(new DecimalFormat("0.00").format(massResult));
            massTable.setTextSize(14);
            massTable.setTextColor(Color.BLACK);
            massTable.setGravity(Gravity.CENTER);
            dataRow.addView(massTable);

            TextView sacksTable = new TextView(MainActivity.this);
            sacksTable.setText(new DecimalFormat("0.00").format(sacksResult));
            sacksTable.setTextColor(Color.BLACK);
            sacksTable.setGravity(Gravity.CENTER);
            sacksTable.setTextSize(14);
            dataRow.addView(sacksTable);

            tableOne.addView(dataRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        }
    });

Also on my LogCat I'm getting error: getExtractedText on inactive InputConnection

I have tried to rewrite app with some tutorials/research etc thinking that I might find whats wrong but after 2 days I'm done and hopefully someone can help me.


Solution

  • Your onCreate gets called whenever your screen orientation is changed, one way to stop that is to add this line under your relevant activity tag in your manifest file:

    android:configChanges="orientation|screenSize"
    

    This way Android OS won't recreate the UI whenever the screen is rotated and leave it up to you.