|
||||||||||
Android: Orientation Issues
Android Orientations There are 2 orientations: portrait (vertical) and landscape (horizontal)
Detecting the current Orientation
Change in Orientation of Device (user rotates it) (TIP: in emulator hit Cntrl+F11 to change orientation) OR
|
viewed in portrait |
viewed in landscape |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Top Left" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Top Right" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Bottom Left" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Bottom Right" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Middle" />
</RelativeLayout> |
main.xml in res/layout (portrait) <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Top Left" /><Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Top Right" /><Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="Bottom Left" /><Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Bottom Right" /><Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Middle" /></RelativeLayout> main.xml in res/layout-land (landscape)--added 2 buttons <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Top Left" /><Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Top Right" /><Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="Bottom Left" /><Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Bottom Right" /><Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Middle" /><Button android:id="@+id/button6" android:layout_width="180px" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentTop="true" android:text="Top Middle" />
<Button android:id="@+id/button7" android:layout_width="180px" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentBottom="true" android:text="Bottom Middle" />
</RelativeLayout>
Static Setting of Orientation
OPTION 1- For application can set the orientation statically in AndroidManifest.xml file
Example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns******"
><used-sdk ********/>
<application
***>
<activity
android:screenOrientation="landscape">
</activity>
</application>
</manifest>
OPTION 2-setup in the layout file
Example<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xlmns:android="http:******"
android:orientation="vertical" >********
</LinearLayout> bu
Dynamically Changing Orientation (in code)
Inside the Activity do the following---in whatever method you want (here we did in onCreate) call setRequestedOrientation()
import android.content.pm.ActivityInfo;
class Activity *****
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//go to landscape
setRequestedOrientation(ActifiyInfo.SCREEN_ORIENTATION_LANDSCAPE);}