How To Correctly Make A Fragment Rotate
Solution 1:
I finally found the answer Change Screen Orientation programmatically using a Button
but since it is a fragment I wish to rotate not the whole screen the code is like this:
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mView != null) return mView; //I use this because I use the same fragment , if you use different fragments remove this
mView = (LinearLayout) inflater.inflate(R.layout.fragment_customer_card_view, container, false);
intw= container.getWidth();
inth= container.getHeight();
mView.setRotation(90);
mView.setTranslationX((w - h) / 2);
mView.setTranslationY((h - w) / 2);
ViewGroup.LayoutParamslp= mView.getLayoutParams();
lp.height = w;
lp.width = h;
mView.requestLayout();
return mView;
}
hope it helps someone in the future
Solution 2:
Use this in the fragment where u want want orientation .
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If you many fragments and only one fragment u wantin portrait others should be compatible based on user then make sure to give above in fragment where u want portrait orientation always and in other fragmetns use
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Solution 3:
Does setting orientation="horizontal"
not do the trick?
More generally, recreating the Activity shouldn't be a huge deal for you. You should just be able to persist all of your changes and then pull them out of the Bundle that is passed to onCreate
. If that is a large problem I would reconsider some of the design choices you're making in your app.
Post a Comment for "How To Correctly Make A Fragment Rotate"