Changing the layout of a fragment when changing orientation

I have the following problem:

I have a TabActivity that shows FragmentActivity on one of its tabs.

That FragmentActivity adds a ListFragment , when you click on an element of this ListFragment , a fragment is added (also to the backstack) and displayed.

Now I need to change the layout of this Fragment to change when switching to landscape orientation.

But I absolutely do not know where to implement this change. I already created to fix the layout in the layout-earth folder. But where to install it correctly?

+4
source share
2 answers

Warning: this may be the answer before the candy.

A Fragment will not be overestimated when changing the configuration, but you can achieve the effect as follows by creating it using FrameLayout and (re), which will be entered manually:

 public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { FrameLayout frameLayout = new FrameLayout(getActivity()); populateViewForOrientation(inflater, frameLayout); return frameLayout; } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); LayoutInflater inflater = LayoutInflater.from(getActivity()); populateViewForOrientation(inflater, (ViewGroup) getView()); } private void populateViewForOrientation(LayoutInflater inflater, ViewGroup viewGroup) { viewGroup.removeAllViewsInLayout(); View subview = inflater.inflate(R.layout.my_fragment, viewGroup); // Find your buttons in subview, set up onclicks, set up callbacks to your parent fragment or activity here. } } 

I'm not particularly happy with getActivity() and the calls related here, but I don't think there is another way to get hold of these things.

Update: Removed non- ViewGroup to FrameLayout and uses LayoutInflater.from() , and the third parameter is inflate() instead of an explicit view.

+20
source

I believe that if you have layouts designed for specific device orientations, then you only need to specify the same name , but put them in the appropriate resource directory. This link provides some explanation. The Android system will then take care of choosing the appropriate resource, but you can handle it yourself if necessary.

0
source

All Articles