auto adjust Android Tablerow Column Items
Question
I'm working on an app where a table is used. On portrait it is two columns, and than on landscape three columns. I know you can use /res/layout-land folder to change the view, but then unsaved data is reset. Is there another option than using /res/layout-land/mylayout.xml?
Solution
If you don want to use specific folders(like layout-land
) you could make a single layout file with the TableLayout
containing the three columns(all of them) and then hide/unhide one of the columns based on the current orientation of the phone. In the onCreate
method make the collapsed column appear/disappear if the orientation is set to landscape
/portrait
:
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// make the column appear, we're in landscape orientation
tl.setColumnCollapsed(2, false);
} else {
tl.setColumnCollapsed(2, true);
}
I don't know what you're trying to achieve with this, the activity will still be recreated again as usual when the phone it's turn.