Getting To Know The Android Ui: UNIT-3
Getting To Know The Android Ui: UNIT-3
Attributes:
• 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
Anchoring…
2. Resizing and repositioning: ultimate technique is
resizing each and every view according to the current
screen orientation by creating a separate res/layout
folder containing the XML files for the UI of each
orientation.
-To support landscape mode, you can create a new folder
in the res folder and name it as layout-land
Additional attributes:
Layout_top_middle
Layout_bottom_middle
Resizing and repositioning
Managing changes to screen orientation
• Discusses what happens to an activity’s state
when the device changes orientation .
• The current activity is destroyed and
recreated,so data may be lost
Cont…
• Persisting state information during changes in configuration.
-When activity is killed 2 methods are called
• onPause() — This method is always fired whenever an activity is killed or pushed
into the background.
• onSaveInstanceState() — This method is also fired whenever an activity is about
to be killed or put into the background.
-unlike onPause() ,this method is not called when user pressed the back button
because its not necessary to restore
-this method provides a Bundle object as an argument so that it is used save our
activity’s state.
@Override
public void onSaveInstanceState(Bundle outState) {
//---save whatever you need to persist---
outState.putString(“ID”, “1234567890”);
super.onSaveInstanceState(outState);
}
Cont…
-When activity is recreated the oncreate() method is first
fired,followed by onRestoreInstancaState() method,
which enables you to retrieve the state that you saved
previously in onSaveInstance method through bundle
object.
@ Override
public void onRestoreInstanceState(Bundle
savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//---retrieve the information persisted earlier---String ID =
savedInstanceState.getString(“ID”);
}
-To save complex data structures, bundle object is not sufficient, so to handle this we use
onRetainNonConfiguarationInstance() method.
@Override
public Object onRetainNonConfigurationInstance() {
//---save whatever you want here; it takes in an Object type---
return(“Some text to preserve”);
}
-this method returns an Object type, which allows us to return any data type.
-to extract data in oncrete() method it uses getLastNonConfiguarationInstance() method.
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(“StateInfo”, “onCreate”);
String str = (String) getLastNonConfigurationInstance();
}
Cont.
• Detecting Orientation Changes:
-if we need to know the device’s current
orientation during runtime. To determine that,
you can use the WindowManager class.
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Cont..
• Controlling the Orientation of the Activity
-we might want to ensure that your application is displayed in
only a certain orientation.
- For example, you may be writing a game that should be
viewed only in landscape mode.
-In this case, you can programmatically force a change in
orientation using the setRequestOrientation() method
• //---change to landscape mode---
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION
_LANDSCAPE);
UTILIZING THE ACTION BAR
• Newer feature introduced in Android 3 and 4
is the Action Bar.
• In place of the traditional title bar located at
the top of the device’s screen.
• the Action Bar displays the application icon
together with the activity title.
• Optionally, on the right side of the Action Bar
are action items.
Customizing the Action Items and
Application Icon
• The menu items are displayed without the text.
If you want to display the text for the action item
together with the icon, you could use the “|”
operator together with the
MenuItem.SHOW_AS_ACTION_WITH_TEXT
constant:
CREATING THE USER INTERFACE
PROGRAMMATICALLY
• So far we have seen how UI has been created
using XML.
• Beside that we can also create UI using Code
• This helps to generate UI dynamically during
runtime.
• Refer the code in page number 146
CREATING THE USER INTERFACE
PROGRAMMATICALLY
• How it works:
• In this example, you first commented out the setContentView()
statement so that it does not load the
• UI from the main.xml fi le.
• You then created a LayoutParams object to specify the layout
parameter that can be used by other
• views (which you will create next):
//---param for views---
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);