Reputation: 5238
I want to rearrange my views in onConfigurationChange
when screen orientation changes. Activity restart and views recreation are too consuming; so I have to keep views.
There is one LinearLayout I want to change orientation of. If orientation is changed, I want to swap width and height values of all children elements. (In my case, they can be 0dp
, wrap_content
and match_parent
.)
Of course, I can keep old orientation and iterate over children swapping their width and heights. But this task looks essential and not unique for me. Is there easier way to so that? Maybe it's already implemented somewhere?
Upvotes: 0
Views: 565
Reputation: 36035
It is implemented but only with activity restarts.
One method that doesn't involve iteration would be to recreate the View yourself with the landscape layout which is essentially what the Android system would do. The benefit though is you don't have to recreate the rest of your Activity. The downside is you have to unbind all your listeners that you've applied to your Views (onClickListener, onItemClickListener, etc). Not doing so would create a memory leak.
However, there is a simple method to doing this. Have your layout handled be a single Fragment style that only controls the Views. All clicks and user interaction is handled by this fragment that sends the actions back up to the Activity. When onConfigurationChange
is called, simply remove this Fragment from the FragmentManager
, then add a new Fragment
.
Upvotes: 2