Last active
December 21, 2015 03:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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