SlideShare a Scribd company logo
ActionBar
ActionBar
The Action Bar is the graphical component of each application
that identifies the navigated section, provide the user with the
possible actions for that section and defines the type of
navigation.
Action Bar
1. Icon that identifies the application
2. Available Shares
3. Menu overflow to other actions
ActionBar
The ActionBar has been added with Android 3.0. To make its
functionality available for all versions of Android, you must:
● Integrate the support library v7 and use:
import android.support.v7.app.ActionBar;
instead of:
import android.app.ActionBar;
● Extend ActionBarActivity class
● Declare a compatible theme for the Activity:
<activity android:theme="@style/Theme.AppCompat.Light" >
You can show or hide the ActionBar using:
getSupportActionBar().show();
getSupportActionBar().hide();
Support Action Bar
ActionBar
In order to add actions on the Action Bar actions must be defined
in /res/menu folder with an .xml file:
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"/>
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose" />
</menu>
In Activity class you must override the onCreateOptionMenu()
method:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions, menu);
return true;
}
Action Items
ActionBar
The management of the actions and their visibility are entrusted
to the system which calculates the available space and adds
them according to the definition of the xml file. Adding the
showAsAction attribute in the menu you can manage the actions:
●
never:it is never shown
●
ifRoom: it's shown if there is enaough space available,
otherwise it is added to the overflow
●
always: it's always shown
<item
android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:showAsAction="ifRoom"/>
Action Items
ActionBar
When the user interacts with the Action Items the
Activity.onOptionsItemSelected() method is invoked :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Action Items Selection
ActionBar
If the space for action is not enough, you can bring up an
extension on the bottom of the display in which Action Items are
shown.
To do so, you must add the following attribute to the Activity in the
Manifest:
android:uiOptions="splitActionBarWhenNarrow"
Split Action Bar
ActionBar
You can enable the up navigation by enabling the user to return to
a higher level of navigation. To do this, in onCreate() method it
must be entered:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This enables clicking on the home icon and show the arrow.
Pattern: up the navigation and click on the back of the system do
not always get the same result:
●Back navigation: it shows the order in reverse chronological
navigation
●Up navigation: it shows hierarchically the higher level navigation
Up Navigation
ActionBar
In order to define the result of the up navigation through Activity,
there are 2 alternatives:
● You can defines the ParentActivity in Manifest for each
Activity:
android:parentActivityName="com.example.myfirstapp.MainActivity"
● You can override:
@Override
public Intent getSupportParentActivityIntent() {
return super.getSupportParentActivityIntent();
}
Up Navigation
ActionBar
The Action View is a widget that replaces the icon when the user
interacts with it. In order to declare an Action View you must add
the following in the menu file:
● android:actionLayout="@layout/search_view"
● android:actionViewClass="android.support.v7.widget.Sea
rchView"
Action View
ActionBar
An Action Provider as a ActionView replaces the button, but it has
complete control of the behavior of the Action.
To insert a Action Provider on the Action Bar You must add the
following attribute inside the menu xml file (there are predefined
by the ActionProvider platform, as ShareActionProvider):
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
To define a custom Action Provider you must extend
ActionProvider. In these cases it is not necessary to enter the
event management in Activity.onOptionsItemSelected(), but you
can use the ActionProvider.onPerformDefaultAction().
Action Provider
ActionBar
In a Custom Action Provider you must:
● Pass the Context to the costructor:
public CustomActionProvider(Context context) {
super(context);
}
● Define the Action View:
@Override
public View onCreateActionView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.activity_main, null);
return view;
}
● Define the action:
@Override
public boolean onPerformDefaultAction() {
Intent intent = new Intent(Intent.ACTION_SEND);
getContext().startActivity(intent);
return true;
}
Action Provider
ActionBar
The Action Bar allows you to define a 3-tier Application
Navigation:
● Navigation Tabs: the user navigates between sections with
horizontal swype
● Drop-Down Menu: the user selects the title of the Action Bar
and a drop down menu appears with a list of available
sections
● Navigation Drawer: the user selects the application icon on
the Action Bar and an additional menu appears to the left
where the developer can put any type of View.
Navigation
ActionBar
The graphical management of the placement of the Tab is given
to the system that decides whether to include them as part of the
Action Bar or in a separate space.
Navigation - Tabs
To integrate the Activity with this type of navigation:
● Declare the Tab Navigation in Activity.onCreate():
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar
Navigation - Tabs
● Implement ActionBar.TabListener interface:
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
● Add Tabs to the Action Bar in Activity.onCreate():
Tab tab = getSupportActionBar().newTab().setText(R.string.example)
.setTabListener(this);
getSupportActionBar().addTab(tab);
ActionBar
Navigation – DropDown Menu
The DropDown menu (or Spinner) is used when it is not
frequently change the contents of the Activity.
ActionBar
Navigation – DropDown Menu
In order to integrate it:
● Declare List Navigation in Activity.onCreate():
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
● Create a SpinnerAdapter:
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.action_list,
android.R.layout.simple_spinner_dropdown_item);
● Implements ActionBar.OnNavigationListener:
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
return true;
}
● Set the callbacks to handle Action Bar click:
getSupportActionBar().setListNavigationCallbacks(spinnerAdapter,
navigationCallback);
ActionBar
Navigation – Drawer
The Navigation Drawer is a panel that appears to the left as the
user selects the icon for the Action Bar and shows you the
navigation options.
ActionBar
Navigation – Drawer
In order to integrate it:
● Insert a DrawerLayout as Activity layout root:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
ActionBar
Navigation – Drawer
● Retrieve Activity DrawerLayout and ListView:
DrawerLayout mDrawerLayout = (DrawerLayout)
findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
● Set an Adapter to the list:
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
● Handle the on item clicks of the list:
mDrawerList.setOnItemClickListener(this);
It is possible to handle the Navigation Drawer opening/closing
events:
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
ActionBar
Action Bar Style
In order to customize the Action Bar you can use styles in
/res/values file.
<resources>
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">@style/TabTextStyle</item>
<item name="android:actionMenuTextColor">@color/actionbar_text</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="actionBarTabTextStyle">@style/TabTextStyle</item>
<item name="actionMenuTextColor">@color/actionbar_text</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">@style/TitleTextStyle</item>
<item name="android:background">@drawable/actionbar_background</item>
<item name="android:backgroundStacked">@drawable/actionbar_background</item>
<item name="android:backgroundSplit">@drawable/actionbar_background</item>
<item name="titleTextStyle">@style/TitleTextStyle</item>
<item name="background">@drawable/actionbar_background</item>
<item name="backgroundStacked">@drawable/actionbar_background</item>
<item name="backgroundSplit">@drawable/actionbar_background</item>
</style>
<style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/actionbar_text</item>
</style>
<style name="TabTextStyle" parent="@style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">@color/actionbar_text</item>
</style>
</resources>

More Related Content

What's hot

JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
Eueung Mulyana
 
Android Services
Android ServicesAndroid Services
Android Services
Ahsanul Karim
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
Shakib Hasan Sumon
 
Dart ppt
Dart pptDart ppt
Dart ppt
Krishna Teja
 
Android resources
Android resourcesAndroid resources
Android resources
ma-polimi
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
ma-polimi
 
Android notification
Android notificationAndroid notification
Android notification
Krazy Koder
 
Android adapters
Android adaptersAndroid adapters
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
Nikmesoft Ltd
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
Reem Alattas
 
Android animation
Android animationAndroid animation
Android animation
Krazy Koder
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
Bui Kiet
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
Sergi Martínez
 
android layouts
android layoutsandroid layouts
android layouts
Deepa Rani
 
android menus
android menusandroid menus
android menus
Deepa Rani
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
Christoffer Noring
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 

What's hot (20)

JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
Android Services
Android ServicesAndroid Services
Android Services
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
Dart ppt
Dart pptDart ppt
Dart ppt
 
Android resources
Android resourcesAndroid resources
Android resources
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android notification
Android notificationAndroid notification
Android notification
 
Android adapters
Android adaptersAndroid adapters
Android adapters
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
 
Android animation
Android animationAndroid animation
Android animation
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 
android layouts
android layoutsandroid layouts
android layouts
 
android menus
android menusandroid menus
android menus
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 

Similar to Android App Development - 05 Action bar

Android 3
Android 3Android 3
Android 3
Robert Cooper
 
Android Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompatAndroid Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompat
cbeyls
 
Action bar
Action barAction bar
Action bar
Mu Chun Wang
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
Robert Cooper
 
Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
Dr. Ramkumar Lakshminarayanan
 
Advancing the UI — Part 1: Look, Motion, and Gestures
Advancing the UI — Part 1: Look, Motion, and GesturesAdvancing the UI — Part 1: Look, Motion, and Gestures
Advancing the UI — Part 1: Look, Motion, and Gestures
Samsung Developers
 
Android Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAndroid Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation Drawer
Agus Haryanto
 
Chapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barChapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action bar
Kalluri Vinay Reddy
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
Diego Grancini
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developers
Pavel Lahoda
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahoda
Droidcon Berlin
 
Adopting 3D Touch in your apps
Adopting 3D Touch in your appsAdopting 3D Touch in your apps
Adopting 3D Touch in your apps
Juan C Catalan
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and Smartphones
Michael Galpin
 
Android animations
Android animationsAndroid animations
Android animations
Kumar
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
Yekmer Simsek
 
Navigation Architecture Component(京都Devかふぇ バージョン)
Navigation Architecture Component(京都Devかふぇ バージョン)Navigation Architecture Component(京都Devかふぇ バージョン)
Navigation Architecture Component(京都Devかふぇ バージョン)
Yasutaka Kawamoto
 
A Single activity app with Jetpack's Navigation Component
A Single activity app with Jetpack's Navigation ComponentA Single activity app with Jetpack's Navigation Component
A Single activity app with Jetpack's Navigation Component
Boonya Kitpitak
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
Monir Zzaman
 
The Mighty Power of the Accessibility Service - Guy Griv, Pepper
The Mighty Power of the Accessibility Service - Guy Griv, PepperThe Mighty Power of the Accessibility Service - Guy Griv, Pepper
The Mighty Power of the Accessibility Service - Guy Griv, Pepper
DroidConTLV
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
RajthilakMCA
 

Similar to Android App Development - 05 Action bar (20)

Android 3
Android 3Android 3
Android 3
 
Android Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompatAndroid Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompat
 
Action bar
Action barAction bar
Action bar
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
 
Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
 
Advancing the UI — Part 1: Look, Motion, and Gestures
Advancing the UI — Part 1: Look, Motion, and GesturesAdvancing the UI — Part 1: Look, Motion, and Gestures
Advancing the UI — Part 1: Look, Motion, and Gestures
 
Android Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAndroid Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation Drawer
 
Chapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barChapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action bar
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developers
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahoda
 
Adopting 3D Touch in your apps
Adopting 3D Touch in your appsAdopting 3D Touch in your apps
Adopting 3D Touch in your apps
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and Smartphones
 
Android animations
Android animationsAndroid animations
Android animations
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Navigation Architecture Component(京都Devかふぇ バージョン)
Navigation Architecture Component(京都Devかふぇ バージョン)Navigation Architecture Component(京都Devかふぇ バージョン)
Navigation Architecture Component(京都Devかふぇ バージョン)
 
A Single activity app with Jetpack's Navigation Component
A Single activity app with Jetpack's Navigation ComponentA Single activity app with Jetpack's Navigation Component
A Single activity app with Jetpack's Navigation Component
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
 
The Mighty Power of the Accessibility Service - Guy Griv, Pepper
The Mighty Power of the Accessibility Service - Guy Griv, PepperThe Mighty Power of the Accessibility Service - Guy Griv, Pepper
The Mighty Power of the Accessibility Service - Guy Griv, Pepper
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
 

More from Diego Grancini

Android App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsAndroid App Development - 14 location, media and notifications
Android App Development - 14 location, media and notifications
Diego Grancini
 
Android App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgetsAndroid App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgets
Diego Grancini
 
Android App Development - 12 animations
Android App Development - 12 animationsAndroid App Development - 12 animations
Android App Development - 12 animations
Diego Grancini
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsAndroid App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Diego Grancini
 
Android App Development - 10 Content providers
Android App Development - 10 Content providersAndroid App Development - 10 Content providers
Android App Development - 10 Content providers
Diego Grancini
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 Storage
Diego Grancini
 
Android App Development - 08 Services
Android App Development - 08 ServicesAndroid App Development - 08 Services
Android App Development - 08 Services
Diego Grancini
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
Diego Grancini
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
Diego Grancini
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 Resources
Diego Grancini
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
Diego Grancini
 
Android App Development - 01 Introduction
Android App Development - 01 IntroductionAndroid App Development - 01 Introduction
Android App Development - 01 Introduction
Diego Grancini
 

More from Diego Grancini (12)

Android App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsAndroid App Development - 14 location, media and notifications
Android App Development - 14 location, media and notifications
 
Android App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgetsAndroid App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgets
 
Android App Development - 12 animations
Android App Development - 12 animationsAndroid App Development - 12 animations
Android App Development - 12 animations
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsAndroid App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
 
Android App Development - 10 Content providers
Android App Development - 10 Content providersAndroid App Development - 10 Content providers
Android App Development - 10 Content providers
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 Storage
 
Android App Development - 08 Services
Android App Development - 08 ServicesAndroid App Development - 08 Services
Android App Development - 08 Services
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 Resources
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Android App Development - 01 Introduction
Android App Development - 01 IntroductionAndroid App Development - 01 Introduction
Android App Development - 01 Introduction
 

Android App Development - 05 Action bar

  • 2. ActionBar The Action Bar is the graphical component of each application that identifies the navigated section, provide the user with the possible actions for that section and defines the type of navigation. Action Bar 1. Icon that identifies the application 2. Available Shares 3. Menu overflow to other actions
  • 3. ActionBar The ActionBar has been added with Android 3.0. To make its functionality available for all versions of Android, you must: ● Integrate the support library v7 and use: import android.support.v7.app.ActionBar; instead of: import android.app.ActionBar; ● Extend ActionBarActivity class ● Declare a compatible theme for the Activity: <activity android:theme="@style/Theme.AppCompat.Light" > You can show or hide the ActionBar using: getSupportActionBar().show(); getSupportActionBar().hide(); Support Action Bar
  • 4. ActionBar In order to add actions on the Action Bar actions must be defined in /res/menu folder with an .xml file: <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search"/> <item android:id="@+id/action_compose" android:icon="@drawable/ic_action_compose" android:title="@string/action_compose" /> </menu> In Activity class you must override the onCreateOptionMenu() method: @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.actions, menu); return true; } Action Items
  • 5. ActionBar The management of the actions and their visibility are entrusted to the system which calculates the available space and adds them according to the definition of the xml file. Adding the showAsAction attribute in the menu you can manage the actions: ● never:it is never shown ● ifRoom: it's shown if there is enaough space available, otherwise it is added to the overflow ● always: it's always shown <item android:id="@+id/action_compose" android:icon="@drawable/ic_action_compose" android:showAsAction="ifRoom"/> Action Items
  • 6. ActionBar When the user interacts with the Action Items the Activity.onOptionsItemSelected() method is invoked : @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: openSearch(); return true; case R.id.action_compose: composeMessage(); return true; default: return super.onOptionsItemSelected(item); } } Action Items Selection
  • 7. ActionBar If the space for action is not enough, you can bring up an extension on the bottom of the display in which Action Items are shown. To do so, you must add the following attribute to the Activity in the Manifest: android:uiOptions="splitActionBarWhenNarrow" Split Action Bar
  • 8. ActionBar You can enable the up navigation by enabling the user to return to a higher level of navigation. To do this, in onCreate() method it must be entered: getSupportActionBar().setDisplayHomeAsUpEnabled(true); This enables clicking on the home icon and show the arrow. Pattern: up the navigation and click on the back of the system do not always get the same result: ●Back navigation: it shows the order in reverse chronological navigation ●Up navigation: it shows hierarchically the higher level navigation Up Navigation
  • 9. ActionBar In order to define the result of the up navigation through Activity, there are 2 alternatives: ● You can defines the ParentActivity in Manifest for each Activity: android:parentActivityName="com.example.myfirstapp.MainActivity" ● You can override: @Override public Intent getSupportParentActivityIntent() { return super.getSupportParentActivityIntent(); } Up Navigation
  • 10. ActionBar The Action View is a widget that replaces the icon when the user interacts with it. In order to declare an Action View you must add the following in the menu file: ● android:actionLayout="@layout/search_view" ● android:actionViewClass="android.support.v7.widget.Sea rchView" Action View
  • 11. ActionBar An Action Provider as a ActionView replaces the button, but it has complete control of the behavior of the Action. To insert a Action Provider on the Action Bar You must add the following attribute inside the menu xml file (there are predefined by the ActionProvider platform, as ShareActionProvider): android:actionProviderClass="android.support.v7.widget.ShareActionProvider" To define a custom Action Provider you must extend ActionProvider. In these cases it is not necessary to enter the event management in Activity.onOptionsItemSelected(), but you can use the ActionProvider.onPerformDefaultAction(). Action Provider
  • 12. ActionBar In a Custom Action Provider you must: ● Pass the Context to the costructor: public CustomActionProvider(Context context) { super(context); } ● Define the Action View: @Override public View onCreateActionView() { LayoutInflater inflater = LayoutInflater.from(getContext()); View view = inflater.inflate(R.layout.activity_main, null); return view; } ● Define the action: @Override public boolean onPerformDefaultAction() { Intent intent = new Intent(Intent.ACTION_SEND); getContext().startActivity(intent); return true; } Action Provider
  • 13. ActionBar The Action Bar allows you to define a 3-tier Application Navigation: ● Navigation Tabs: the user navigates between sections with horizontal swype ● Drop-Down Menu: the user selects the title of the Action Bar and a drop down menu appears with a list of available sections ● Navigation Drawer: the user selects the application icon on the Action Bar and an additional menu appears to the left where the developer can put any type of View. Navigation
  • 14. ActionBar The graphical management of the placement of the Tab is given to the system that decides whether to include them as part of the Action Bar or in a separate space. Navigation - Tabs To integrate the Activity with this type of navigation: ● Declare the Tab Navigation in Activity.onCreate(): getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  • 15. ActionBar Navigation - Tabs ● Implement ActionBar.TabListener interface: @Override public void onTabReselected(Tab arg0, FragmentTransaction arg1) { } @Override public void onTabSelected(Tab arg0, FragmentTransaction arg1) { } @Override public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { } ● Add Tabs to the Action Bar in Activity.onCreate(): Tab tab = getSupportActionBar().newTab().setText(R.string.example) .setTabListener(this); getSupportActionBar().addTab(tab);
  • 16. ActionBar Navigation – DropDown Menu The DropDown menu (or Spinner) is used when it is not frequently change the contents of the Activity.
  • 17. ActionBar Navigation – DropDown Menu In order to integrate it: ● Declare List Navigation in Activity.onCreate(): getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); ● Create a SpinnerAdapter: SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item); ● Implements ActionBar.OnNavigationListener: @Override public boolean onNavigationItemSelected(int position, long itemId) { return true; } ● Set the callbacks to handle Action Bar click: getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, navigationCallback);
  • 18. ActionBar Navigation – Drawer The Navigation Drawer is a panel that appears to the left as the user selects the icon for the Action Bar and shows you the navigation options.
  • 19. ActionBar Navigation – Drawer In order to integrate it: ● Insert a DrawerLayout as Activity layout root: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" /> </android.support.v4.widget.DrawerLayout>
  • 20. ActionBar Navigation – Drawer ● Retrieve Activity DrawerLayout and ListView: DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ListView mDrawerList = (ListView) findViewById(R.id.left_drawer); ● Set an Adapter to the list: mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)); ● Handle the on item clicks of the list: mDrawerList.setOnItemClickListener(this); It is possible to handle the Navigation Drawer opening/closing events: ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } }; mDrawerLayout.setDrawerListener(mDrawerToggle);
  • 21. ActionBar Action Bar Style In order to customize the Action Bar you can use styles in /res/values file. <resources> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light"> <item name="android:actionBarStyle">@style/MyActionBar</item> <item name="android:actionBarTabTextStyle">@style/TabTextStyle</item> <item name="android:actionMenuTextColor">@color/actionbar_text</item> <item name="actionBarStyle">@style/MyActionBar</item> <item name="actionBarTabTextStyle">@style/TabTextStyle</item> <item name="actionMenuTextColor">@color/actionbar_text</item> </style> <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar"> <item name="android:titleTextStyle">@style/TitleTextStyle</item> <item name="android:background">@drawable/actionbar_background</item> <item name="android:backgroundStacked">@drawable/actionbar_background</item> <item name="android:backgroundSplit">@drawable/actionbar_background</item> <item name="titleTextStyle">@style/TitleTextStyle</item> <item name="background">@drawable/actionbar_background</item> <item name="backgroundStacked">@drawable/actionbar_background</item> <item name="backgroundSplit">@drawable/actionbar_background</item> </style> <style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textColor">@color/actionbar_text</item> </style> <style name="TabTextStyle" parent="@style/Widget.AppCompat.ActionBar.TabText"> <item name="android:textColor">@color/actionbar_text</item> </style> </resources>