UNIT-3 Android User Interface
UNIT-3 Android User Interface
layout_marginTop Specifies extra space on the top side of the View or ViewGroup
layout_marginBottom Specifies extra space on the bottom side of the View or ViewGroup
layout_marginLeft Specifies extra space on the left side of the View or ViewGroup
layout_marginRight Specifies extra space on the right side of the View or ViewGroup
layout_weight Specifi es how much of the extra space in the layout should be allocated to the View
Syntax:
<activity android:name="package_name.Your_ActivityName"
android:screenOrientation="orientation_type">
</activity>
Adapting to Display Orientation
• States of Screen orientation
There are various possible screen orientation states for any android application, such
as:
• ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
• ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
• ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
• ActivityInfo.SCREEN_ORIENTATION_USER
• ActivityInfo.SCREEN_ORIENTATION_SENSOR
• ActivityInfo.SCREEN_ORIENTATION_BEHIND
• ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
• ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
• ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
• ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
change Screen orientation
• We will create two activities of different screen orientation.
• The first activity will be as “portrait” orientation and
• Second activity as “landscape” orientation state.
1.Creating the activities: There will be two activities and hence two
XML files, one for each activity.activity_main.xml: XML file for first
activity consist of constraint layout with Button and Text View in it.
This activity is in Landscape state.
2.activity_next.xml: XML file for second activity consist of constraint
layout with Text View in it. This activity is in Landscape state.
Anchoring Views
• As ARCore’s environmental understanding updates throughout
an AR experience, virtual objects can appear to drift away from
where they were placed. This can impact your app's realism and
user experience.
• Anchors ensure that objects appear to stay at the same position
and orientation in space, helping you maintain the illusion of
virtual objects placed in the real world.
anchors work
• To use anchors in your scene, your code should:
• Create anchors in the context of a Trackable (such as a Plane) or
the ARCore Session.
• Attach one or more objects to the anchor.
• Anchors can support different kinds of positional behavior in your
scene's objects.
• Determining anchor context and how many anchors you need for
your scene's objects depends on the positional behavior that your
AR scene requires. See the following sections for more
information.
Resizing and Repositioning
• Anchoring – This is the easiest way to anchor views to the four edges of the
screen. When the screen orientation changes, the views can anchor neatly to the
edges.
• Resizing and repositioning – These are simple techniques to ensure that views can
be handle changes in screen orientation.
• Layout_alignParentLeft – This property aligns the view to the left of the parent view.
• Layout_alignParentRight – This property aligns the view to the right of the parent
view.
• Layout_alignParentTop – This property aligns the view to the top of the parent view.
• Layout_alignParentBottom – This property aligns the view to the bottom of the
parent view.
• Layout_centerVertical – This property aligns the view at the center vertically within its
parent view.
• Layout_centerHorizontal – This property aligns the view at the center horizontally
within its parent view.
Managing Changes to Screen Orientation
• Sometimes handling the orientation changes for your Activity,
Fragment or AsyncTasks becomes most frustrating things to deal. If
orientation changes is not handle properly then it results unexpected
behavior of the application.
• When such changes occurs, Android restarts the running Activity
means it destroy and again created.
Why Screen Orientation
• When configurations changed during run time (such as screen
orientation, keyboard availability, and language), Android usually
destroys application’s existing Activity or Fragment and recreate it.
• Android does this so that application can reload its resources based on
the new configuration. The restart behavior helps application to adapt
new configurations by automatically reloading the application with
alternative resources that match the new device configuration.
• Proper handling of orientation changes makes rich user experience
(not lost UI state) for the application and it also avoiding memory
leaks.
Handle Screen Orientation
• To handle these configuration changes, Android provides callbacks to
save your application state before destroying either Activity or
Fragment. In the same it also provides to restore the application state
when it is recreating them.
• There are a different options to handle the orientation changes:
• 1. Lock screen orientation
• 2. Prevent Activity to recreated
• 3. Save basic state
• 4. Save complex objects
Lock screen orientation
• To lock the screen orientation change of any screen (activity) of your android application
makes your activity display only in one mode i.e. either Landscape or Portrait. This is the
simplest way to handle screen orientation but not generally recommended.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<application android:name="com.example.test.TestApplication"
android:label="@string/app_name"> <activity
android:name="com.example.test.activity.MainActivity"
android:screenOrientation="portrait"/>
<activity
android:name="com.example.test.activity.HomeActivity"
android:screenOrientation="landscape"/>
</application>
</manifest>
Prevent Activity to recreated
• Another most common solution to dealing with orientation changes by
setting the android:configChanges flag on your Activity in
AndroidManifest.xml. Using this attribute your Activities won’t be
recreated and all your views and data will still be there after
orientation change.
<activity
android:name="com.example.test.activity.MainActivity“
android:configChanges="orientation|screenSize|keyboardHidden"/>
Save basic state
• This is the most common situation to save the basic data of your Activity
or Fragment during orientation change. You can save Primitive data such
as String, Boolean, Integers or Parcelable objects in a Bundle during the
orientation change and read the same data when Activity recreated.
• Saving and restoring the data works using two Activity lifecycle methods
called onSaveInstanceState() and onRestoreInstanceState().
• To save the state information override onSaveInstanceState() method and
add key-value pairs to the Bundle object that is saved in the event that
your activity is destroyed unexpectedly. This method gets called
before onStop().
Save complex objects
• Override onRetainNonConfigurationInstance() and
getLastNonConfigurationInstance()
• Prior to Honeycomb’s release, the recommended means of transferring
active objects across Activity instances was to override
the onRetainNonConfigurationInstance()
and getLastNonConfigurationInstance() methods.
• After API level 13 these methods have been deprecated in favor of the
more Fragment’s setRetainInstance(boolean) capability, which
provides a much cleaner and modular means of retaining objects
during configuration changes.
Persisting State Information during
Changes in Configuration
1.onPause() – This method is always fired, whenever an activity is killed
or pushed into the background.