Skip to content

Instantly share code, notes, and snippets.

@pwittchen
Last active December 21, 2015 03:58
Show Gist options
  • Save pwittchen/6245526 to your computer and use it in GitHub Desktop.
Save pwittchen/6245526 to your computer and use it in GitHub Desktop.
Examplary code showing how to change Fragment layout on orientation change in Android.
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
// Find your buttons in view, set up onclicks, set up callbacks to your parent fragment or activity here.
// You can create ViewHolder or separate method for that.
// example of accessing views: TextView textViewExample = (TextView) view.findViewById(R.id.text_view_example);
// textViewExample.setText("example");
return view;
}
@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.
// You can create ViewHolder or separate method for that.
// example of accessing views: TextView textViewExample = (TextView) view.findViewById(R.id.text_view_example);
// textViewExample.setText("example");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment