From Dhanu Adapting To Display Orientation
From Dhanu Adapting To Display Orientation
From Dhanu Adapting To Display Orientation
<Button
android:id=@+id/button1
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=Top Left Button
android:layout_alignParentLeft=true
android:layout_alignParentTop=true
/>
Anchoring Views - Example
<Button
android:id=@+id/button2
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=Top Right Button
android:layout_alignParentTop=true
android:layout_alignParentRight=true
/>
<Button
android:id=@+id/button3
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=Bottom Left Button
android:layout_alignParentLeft=true
android:layout_alignParentBottom=true
/>
Anchoring Views - Example
<Button
android:id=@+id/button4
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=Bottom Right Button
android:layout_alignParentRight=true
android:layout_alignParentBottom=true
/>
<Button
android:id=@+id/button5
android:layout_width=fill_parent
android:layout_height=wrap_content
android:text=Middle Button
android:layout_centerVertical=true
android:layout_centerHorizontal=true
/>
</RelativeLayout>
Anchoring Views - Example
Observe the following attributes found in the various
Button views:
layout_alignParentLeft
Aligns the view to the left of the parent view
layout_alignParentRight
Aligns the view to the right of the parent view
layout_alignParentTop
Aligns the view to the top of the parent view
layout_alignParentBottom
Aligns the view to the bottom of the parent view
layout_centerVertical
Centers the view vertically within its parent view
layout_centerHorizontal
Centers the view horizontally within its parent view
Figure shows the activity when viewed
in portrait mode.
When the screen orientation changes to
landscape mode
the four buttons are aligned to the four edges of
the screen, and
the center button is centered in the middle of the
screen with its width fully stretched
see Next Figure
Figure shows the activity when viewed
in landscape mode.
2. Resizing and Repositioning
Apart from anchoring your views to the four
edges of the screen, an easier way to customize
the UI based on screen orientation is to create a
separate res/layout folder containing the XML
files for the UI of each orientation.
<EditText
android:id=@+id/txtField1
android:layout_width=fill_parent
android:layout_height=wrap_content />
<EditText
android:layout_width=fill_parent
android:layout_height=wrap_content />
</LinearLayout>
package net.learn2develop.Orientations;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
For example, the user may change orientation while entering some
text into an EditText view.
When this happens, any text inside the EditText view will be
persisted and restored automatically when the activity is re-
created.
setRequestedOrientation(ActivityInfo.SCREE
N_ORIENTATION_PORTRAIT);
Besides using the setRequestOrientation()
method, you can also use the
android:screenOrientation attribute on the
<activity> element in AndroidManifest.xml as
follows to constrain the activity to a certain
orientation:
<?xml version=1.0 encoding=utf-8?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android
package=net.learn2develop.Orientations
android:versionCode=1
android:versionName=1.0>
<application android:icon=@drawable/icon
android:label=@string/app_name>
<activity android:name=.MainActivity
android:label=@string/app_name
android:screenOrientation=landscape >
<intent-filter>
<action android:name=android.intent.action.MAIN />
<category android:name=android.intent.category.LAUNCHER />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion=9 />
</manifest>
The preceding example constrains the activity
to a certain orientation (landscape in this
case) and prevents the activity from being
destroyed; that is, the activity will not be
destroyed and the onCreate() event will not
be fired again when the orientation of the
device changes.
Following are two other values that you can
specify in the android:screenOrientation
attribute:
portrait Portrait mode
sensor Based on the accelerometer
Topic Ends