I have a activity
in which I load a fragment
. Lets say I have two sections inside fragment layout
(section A and B) , but while I switch to landscape then I have to show only section A . How will i know the orientation change inside fragment.
Asked
Active
Viewed 80 times
0

Aditya Vyas-Lakhan
- 13,409
- 16
- 61
- 96

bhaskar
- 991
- 1
- 15
- 37
-
http://stackoverflow.com/questions/18268218/change-screen-orientation-programatically-using-a-button – Apurva Apr 11 '16 at 08:52
3 Answers
0
you can override onConfigurationChanged
to be notified when the orientation changed add code below in your fragment class:
@Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//landscape
}else{
//portrait
}
}

Kosh
- 6,140
- 3
- 36
- 67
0
When the orientation of the screen is changed the Fragment
will be destroyed and created again, so onDestroy()
and onCreate()
are called. in your onCreate()
or onCreateView()
(or wherever you need to) you can check for the current screen rotation with the following code:
int screenRotation = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if(screenRotation == Surface.ROTATION_90 || screenRotation == Surface.ROTATION_270) {
//LANDSCAPE
} else {
//PORTRAIT
}

Jeffalee
- 1,080
- 1
- 7
- 15
0
For this functionality android provides Sliding pane layout SlidingPaneLayout
and watch this tutorials also official google developers tutorial https://www.youtube.com/watch?v=Jl3-lzlzOJI&t=1007s

Adeel Turk
- 897
- 8
- 23