Android Screen Orientation Change
Android Screen Orientation Change
Know everything about Android Screen Orientation Change (Screen Rotation Change) Example
with Code Decription! "e will dicu all poible Android Screen Orientation Change in
detail with help o# a imple Android Activity Example! Alo you can learn how to do android
creen orientation loc$ here! Alo you can change your creen orientation any time you want in
an Android application!
%elow are the poible screen orientation states #or any android application!
Activity&n#o!SCREEN_ORIENTATION_UNSPECIFIED
Activity&n#o!SCREEN_ORIENTATION_LANDSCAPE
Activity&n#o!SCREEN_ORIENTATION_PORTRAIT
Activity&n#o!SCREEN_ORIENTATION_USER
Activity&n#o!SCREEN_ORIENTATION_BEHIND
Activity&n#o!SCREEN_ORIENTATION_SENSOR
Activity&n#o!SCREEN_ORIENTATION_NOSENSOR
Activity&n#o!SCREEN_ORIENTATION_SENSOR_LANDSCAPE
Activity&n#o!SCREEN_ORIENTATION_SENSOR_PORTRAIT
Activity&n#o!SCREEN_ORIENTATION_REVERSE_LANDSCAPE
Activity&n#o!SCREEN_ORIENTATION_REVERSE_PORTRAIT
Activity&n#o!SCREEN_ORIENTATION_FULL_SENSOR
'ote( )hee tate are already preent in the de#ault Android Cla called ActivityInfo. So
don*t worry+ we will ,ut call the above parameter when we want to et the creen orientation o#
any activity at anytime! "e will dicu it latter in thi chapter!
How to check screen orientation (screen rotation) on Emulator
&# you don*t have Android device and you want to chec$ landcape and portrait mode #or your
application+ then you can do it! %elow i the hortcut $ey combination #or changing landcape to
portrait mode in emulator!
Ke Com!ination" -re Ctrl+F12.
.ere we will dicu regarding below poible point on Android Android Screen Orientation
Change (Screen Rotation) with help o# an example! %elow are the point!
#$ %ock screen orientation change in Android
&# you want to loc$ the creen orientation change o# any creen (activity) o# your android
application then add below line in your pro,ect AndroidManifest.xml. Add the below line along
with your activity entry in the Android/ani#et #ile!
%ock &or landscape mode
0our creen will alway diplay in Landscape mode+ when you rotate your device+ no change will apply #or the
current activity!
1 android(creenOrientation23landcape3
Complete code #or the activity entry in the attached example! 'ote 0ou can earch your activity
entry in the /ani#et #ile and add the above line li$e below
1
4
5
6
7
8
9
:
;
<activity
android(name23com!techblogon!creenorientationexample!/ainActivity3
android(creenOrientation23landcape3
android(label23=tring>app?name3 @
<intentA#ilter@
<action android(name23android!intent!action!/A&'3 >@
<category android(name23android!intent!category!BAC'C.ER3 >@
<>intentA#ilter@
<>activity@
%ock &or portrait mode
0our creen will alway diplay in portrait mode+ when you rotate your device+ no change will
apply #or the current activity!
1 android(creenOrientation23portrait3
Complete code #or the activity entry in the attached example! 'ote 0ou can earch your activity
entry in the /ani#et #ile and add the above line li$e below
1
4
5
6
7
8
9
:
;
<activity
android(name23com!techblogon!creenorientationexample!/ainActivity3
android(creenOrientation23portrait3
android(label23=tring>app?name3 @
<intentA#ilter@
<action android(name23android!intent!action!/A&'3 >@
<category android(name23android!intent!category!BAC'C.ER3 >@
<>intentA#ilter@
<>activity@
'$ %ock screen orientation (screen rotation) programmaticall in Android
)hi option i little tric$y! "hat we need to do i+ we will get the creen orientation change
(rotation change) event+ then both #or landcape and portrait mode event+ we will et ether
landcape or portrait mode a per our reDuirement! %elow i the code nippet! "e will et portrait
mode alway+ o the creen will alway tay in portrait mode!
1
4
5
6
7
8
9
:
;
1E
11
14
>> Chec$ creen orientation or creen rotate event here
=Override
public void onCon#igurationChanged(Con#iguration newCon#ig) F
uper!onCon#igurationChanged(newCon#ig)G
>> Chec$ the orientation o# the creen #or landcape and portrait and et portrait mode
alway
i# (newCon#ig!orientation 22 Con#iguration!OR&E')A)&O'?BA'DSCA-E) F
etReDuetedOrientation (Activity&n#o!SCREE'?OR&E')A)&O'?-OR)RA&))G
H ele i# (newCon#ig!orientation 22 Con#iguration!OR&E')A)&O'?-OR)RA&))F
etReDuetedOrientation (Activity&n#o!SCREE'?OR&E')A)&O'?-OR)RA&))G
H
H
($ Change screen orientation programmaticall at antime in Android
&# you want to change your activity* orientation mode at any time a per your reDuirement+ then
below i the ,ava code #or doing the ame! A we dicu all the Android creen orientation
mode above+ we can et any o# the mode at any time #rom the ,ava code! %elow i the example
#or both landscape and portrait mode! %ut you can et all available mode a the ame way! put
the below line o# code anywhere in your activity ,ava #ile+ where you want to change the creen
orientation o# your activity! Ior example i# want to et a particular orientation be#ore howing the
creen+ then you can add the code in onCreate( #unction o# your activity!
)or %andscape mode
1 etReDuetedOrientation(Activity&n#o!SCREE'?OR&E')A)&O'?BA'DSCA-E)G
)or *ortrait +ode
1 etReDuetedOrientation(Activity&n#o!SCREE'?OR&E')A)&O'?-OR)RA&))G
,ote" "hile adding thi code+ don*t #orget to import the pac$age import
android.content.pm.Actiit!In"o# in to !o$r code.
-$ How to get screen orientation at runtime
Sometime+ you want to get the creen orientation o# the current creen programmatically at
runtime ! )o get the ame you can try the below ,ava code in your activity!,ava #ile!
1
4
5
6
7
8
9
:
;
1E
11
>>Jet current creen orientation
Diplay diplay 2 (("indow/anager)
getSytemService("&'DO"?SERK&CE))!getDe#aultDiplay()G
int orientation 2 diplay!getOrientation()G
witch(orientation) F
cae Con#iguration!OR&E')A)&O'?-OR)RA&)(
etReDuetedOrientation (Activity&n#o!SCREE'?OR&E')A)&O'?BA'DSCA-E)G
brea$G
cae Con#iguration!OR&E')A)&O'?BA'DSCA-E(
etReDuetedOrientation (Activity&n#o!SCREE'?OR&E')A)&O'?-OR)RA&))G
brea$G
H
'ote( Don*t #orget to import below pac$age #or the above code!
1
4
5
import android!view!DiplayG
import android!content!re!Con#igurationG
import android!view!"indow/anagerG
.$ )ind screen orientation change e/ent in Android
&# you want to get automatic event when any creen orientation change occurred in the device!
)hat mean when your device moved #rom landcape to portrait and viceAvera+ then you will get
an automatic event in your ,ava code+ o that you can ta$e action accordingly! %elow i the code
nippet #or the ame!
Step #"
Add below line in your Android/ani#et #ile along with your activity entry!
1 android(con#igChange23orientationLcreenSiMeL$eyboard.idden3
,ote" Caution" %eginning with Android 5!4 (A-& level 15) or above+ the above code will only
wor$+ i# you are wor$ing with below Android 5!4 A-& level 15)+ then you might
ue android%con"i&C'an&e()*orientation+,e!-oardHidden* intead
o# android%con"i&C'an&e()*orientation+(creenSi.e+,e!-oardHidden*! 'ote( the
attribute (creenSi.e i omitted here.
Complete code #or the activity entry in the attached example! 'ote 0ou can earch your activity
entry in the /ani#et #ile and add the above line li$e below
1
4
5
6
7
8
9
:
;
<activity
android(name23com!techblogon!creenorientationexample!/ainActivity3
android(con#igChange23orientationLcreenSiMeL$eyboard.idden3
android(label23=tring>app?name3 @
<intentA#ilter@
<action android(name23android!intent!action!/A&'3 >@
<category android(name23android!intent!category!BAC'C.ER3 >@
<>intentA#ilter@
<>activity@
Step '"
Override de#ault android method onCon"i&$rationC'an&ed/0 #or android creen orientation
change! Add below code nippet in your Activity Cla! 'ote( "e are howing toat meage
while getting android creen orientation event #rom ytem!
1
4
5
6
7
8
9
:
;
1E
11
14
>> Chec$ creen orientation or creen rotate event here
=Override
public void onCon#igurationChanged(Con#iguration newCon#ig) F
uper!onCon#igurationChanged(newCon#ig)G
>> Chec$ the orientation o# the creen #or landcape and portrait
i# (newCon#ig!orientation 22 Con#iguration!OR&E')A)&O'?BA'DSCA-E) F
)oat!ma$e)ext(thi+ 3landcape3+ )oat!BE'J).?S.OR))!how()G
H ele i# (newCon#ig!orientation 22 Con#iguration!OR&E')A)&O'?-OR)RA&))F
)oat!ma$e)ext(thi+ 3portrait3+ )oat!BE'J).?S.OR))!how()G
H
H
'ote( 'ow+ when one o# thee con#iguration change+ 0our Activity doe not retart! &ntead+
0our Activity receive a call to onConfigurationChanged()!
0$ Android screen orientation %i&eccle
"e might have ome con#uion on the android creen orientation change li#ecycle o# an Android
activity! Sometime you might oberve that your activity getting retarted+ while the you rotate
the device! Sometime nothing happen! %elow i the imple rule #or android creen
orientation change li#ecycle!
1! &# you already =Override the onCon"i&$rationC'an&ed/0 #unction in your ,ava code( in your Android activity
cla) #or handle Android Screen Orientation Change! )hen your activity will never retart #or any creen orientation
change! A i explained it in pointA6 above!
4! &# you do not =Override onCon"i&$rationC'an&ed/0 #unction a above+ then your running
activity will get retart every time #or any creen orientation change happen in your device! )hat
mean your activity will detroy #irt by calling onDe(tro!/0 A-& and then onCreate/0 method
will call again #or your running activity to retart it!
5! )o properly handle your activity* retart tate+ you need to retore it previou tate through
the normal Activity li#ecycle+ in which Android OS call onSaeIn(tanceState/0 method be#ore it
detroy your activity! So that you can ave your data(variable value)and your application tate!
0ou can then retore the previou tate during onCreate/0 or onRe(toreIn(tanceState/0 method
call o# your activity!
*ro1ect Creation" ScreenOrientationExample
Create a new pro,ect in eclipe! Ce below detail while creating the tet pro,ect+ o that you can
ync with the decription here! 'ote( &n the attached example+ i have only included the screen
orientation change e/ent a active code+ how ever commented all other code+ i# you want it you
can unAcomment and ue a per your reDuirement! Alo the implet way to tet all poible
Android creen orientation change mode i adding above code nippet in your code a decribed
above!
1! Create a new pro,ect with pro,ect name( !creen"rientation#xample
4! Iill Application 'ame( !creen"rientation#xample
5! Iill the -ac$age 'ame a( com.tec$%lo&on.screenorientationexample
6! Iill the activity name (i# a$ing while creating thi pro,ect) a( MainActivity
7! Iill the xml layout name (i# a$ing while creating thi pro,ect) a( activity'main.xml
8! & have ued SDK verion Android (.).* and Ec1ip(e Ver(ion Indi&o. B$t !o$ can $(e an!
er(ion.
9! Add below ,ava #ile (2ainActiit!.3aa) in you pro,ect* >src #older! 'ote( 0ou can copy and
pate below code to your /ainActivity!Nava #ile
+ainActi/it$2a/a
1
4
5
6
7
8
9
:
;
1E
11
14
15
16
17
18
19
1:
pac$age com!techblogon!creenorientationexampleG
import android!o!%undleG
import android!app!ActivityG
import android!content!pm!Activity&n#oG
import android!view!/enuG
import android!view!DiplayG
import android!content!re!Con#igurationG
import android!view!"indow/anagerG
import android!widget!)oatG
public cla /ainActivity extend Activity F
=Override
protected void onCreate(%undle aved&ntanceState) F
uper!onCreate(aved&ntanceState)G
etContentKiew(R!layout!activity?main)G
>> programmitically et creen orientation o# activity (landcape)
1;
4E
41
44
45
46
47
48
49
4:
4;
5E
51
54
55
56
57
58
59
5:
5;
6E
61
64
65
66
67
68
69
6:
6;
7E
71
74
75
76
77
78
79
7:
>>etReDuetedOrientation(Activity&n#o!SCREE'?OR&E')A)&O'?BA'DSCA-E)G
>> programmitically et creen orientation o# activity (portrait)
>>etReDuetedOrientation(Activity&n#o!SCREE'?OR&E')A)&O'?-OR)RA&))G
>O
>>Jet current creen orientation
Diplay diplay 2 (("indow/anager)
getSytemService("&'DO"?SERK&CE))!getDe#aultDiplay()G
int orientation 2 diplay!getOrientation()G
witch(orientation) F
cae Con#iguration!OR&E')A)&O'?-OR)RA&)(
etReDuetedOrientation (Activity&n#o!SCREE'?OR&E')A)&O'?BA'DSCA-E)G
brea$G
cae Con#iguration!OR&E')A)&O'?BA'DSCA-E(
etReDuetedOrientation (Activity&n#o!SCREE'?OR&E')A)&O'?-OR)RA&))G
brea$G
H
O>
H
>> Chec$ creen orientation or creen rotate event here
=Override
public void onCon#igurationChanged(Con#iguration newCon#ig) F
uper!onCon#igurationChanged(newCon#ig)G
>> Chec$ the orientation o# the creen
i# (newCon#ig!orientation 22 Con#iguration!OR&E')A)&O'?BA'DSCA-E) F
)oat!ma$e)ext(thi+ 3landcape3+ )oat!BE'J).?S.OR))!how()G
H ele i# (newCon#ig!orientation 22 Con#iguration!OR&E')A)&O'?-OR)RA&))F
)oat!ma$e)ext(thi+ 3portrait3+ )oat!BE'J).?S.OR))!how()G
H
H
=Override
public boolean onCreateOption/enu(/enu menu) F
>> &n#late the menuG thi add item to the action bar i# it i preent!
get/enu&n#later()!in#late(R!menu!activity?main+ menu)G
return trueG
H
H
:! Add below xml layout #ile (actiit!_main.4m1) in your pro,ect* res+layo,t #older! 'ote( 0ou
can copy and pate the below code nippet in your actiit!_main.4m1 #ile!
acti/it3main$xml
1
4
5
6
7
8
9
:
;
1E
11
14
15
16
<RelativeBayout xmln(android23http(>>chema!android!com>ap$>re>android3
xmln(tool23http(>>chema!android!com>tool3
android(layout?width23match?parent3
android(layout?height23match?parent3
tool(context23!/ainActivity3 @
<)extKiew
android(layout?width23wrap?content3
android(layout?height23wrap?content3
android(layout?center.oriMontal23true3
android(layout?centerKertical23true3
android(text23=tring>hello?world3 >@
<>RelativeBayout@
;! Add the below /ani#et #ile (Android/ani#et!xml) in your pro,ect* root #older! 'ote( 0ou
can copy and pate the below code nippet in your Android/ani#et #ile!
Android+ani&est$xml
1
4
5
6
7
8
9
:
;
1E
11
14
15
16
17
18
<Pxml verion231!E3 encoding23ut#A:3P@
<mani#et xmln(android23http(>>chema!android!com>ap$>re>android3
pac$age23com!techblogon!creenorientationexample3
android(verionCode2313
android(verion'ame231!E3 @
<ueAd$
android(minSd$Kerion23:3
android(targetSd$Kerion23183 >@
<application
android(allow%ac$up23true3
android(icon23=drawable>ic?launcher3
android(label23=tring>app?name3
android(theme23=tyle>App)heme3 @
<activity
19
1:
1;
4E
41
44
45
46
47
48
49
4:
android(name23com!techblogon!creenorientationexample!/ainActivity3
android(con#igChange23orientationLcreenSiMeL$eyboard.idden3
android(label23=tring>app?name3 @
<intentA#ilter@
<action android(name23android!intent!action!/A&'3 >@
<category android(name23android!intent!category!BAC'C.ER3 >@
<>intentA#ilter@
<>activity@
<>application@
<>mani#et@
1E! %uild and Run the application! 0ou can ee a toat meage #or each landcape and portrait
mode change+when you rotate your device!