SlideShare a Scribd company logo
By
Kalluri Vinay Reddy
Android Club
www.facebook.com/vitccandroidclub
Chapter-2
Adding the Action Bar
lesson-1
The action bar is one of the most important design elements you can
implement for your app's activities. It provides several user interface
features that make your app immediately familiar to users by offering
consistency between other Android apps. Key functions include:
A dedicated space for giving your app an identity and indicating the user's
location in the app.
Access to important actions in a predictable way (such as Search).
Adding the Action Bar
• Support for navigation and view switching (with tabs or drop-down
lists).
• This training class offers a quick guide to the action bar's basics.
Lessons
Setting Up the Action Bar
Learn how to add a basic action bar to your activity, whether your app
supports only Android 3.0 and higher or also supports versions as low as
Android 2.1 (by using the Android Support Library).
Adding Action Buttons
Learn how to add and respond to user actions in the action bar.
Styling the Action Bar
Learn how to customize the appearance of your action bar.
Overlaying the Action Bar
Learn how to overlay the action bar in front of your layout, allowing for
seamless transitions when hiding the action bar.
• In its most basic form, the action bar displays the title for the activity
and the app icon on the left.
• Even in this simple form, the action bar is useful for all activities to
inform users about where they are and to maintain a consistent identity
for your app.
Setting Up the Action Bar
Support Android 3.0 and Above Only
Beginning with Android 3.0 (API level 11), the action bar is
included in all activities that use the Theme.Holotheme
(or one of its descendants), which is the default theme when
either the targetSdkVersion orminSdkVersion attribute is set
to "11" or greater.
So to add the action bar to your activities, simply set either
attribute to 11 or higher.
For example:
<manifest ... >
<uses-sdk android:minSdkVersion="11" ... />
...
</manifest>
Support Android 2.1 and Above
Adding the action bar when running on versions older than Android 3.0 (down to
Android 2.1)
requires that you include the Android Support Library in your application.
To get started, read the Support Library Setup document and set up the v7 appcompat
library (once you've downloaded the library package, follow the instructions
for Adding libraries with resources).
Once you have the Support Library integrated with your app project:
1.Update your activity so that it extends ActionBarActivity.
2. For example:public class MainActivity extends ActionBarActivity { ... }
2.In your manifest file, update either the <application> element or
individual <activity>
3.elements to use one of the Theme.AppCompat themes. For example:<activity
android:theme="@style/Theme.AppCompat.Light" ... >
Note:
If you've created a custom theme, be sure it uses one of the Theme.AppCompat
themes as its parent.
Styling the Action Bar.
Now your activity includes the action bar when running on Android 2.1
(API level 7) or higher.
Remember to properly set your app's API level support in the manifest:
<manifest ... >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" />
...
</manifest>
The action bar allows you to add buttons for the most
important action items relating to the app's current context.
Those that appear directly in the action bar with an icon
and/or text are known as action buttons.
Actions that can't fit in the action bar or aren't important
enough are hidden in the action overflow.
Adding Action Buttons
Action bar with an action button for Search
and the action overflow, which reveals
additional actions.
Specify the Actions in XML
All action buttons and other items available in the action
overflow are defined in an XML
menu resource. To add actions to the action bar, create
a new XML file in your project's
res/menu/ directory.
Add an <item> element for each item you want to include
in the action bar.
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
This declares that the Search action should appear as an action
button when room is
available in the action bar, but the Settings action should always
appear in the overflow.
(By default, all actions appear in the overflow, but it's good
practice to explicitly declare
your design intentions for each action.)
The icon attribute requires a resource ID for an image. The name that
follows @drawable/
must be the name of a bitmap image you've saved in your
project's res/drawable/ directory.
For example,
"@drawable/ic_action_search“
refers to ic_action_search.png.
Likewise, the title attribute uses a string resource
that's
defined by an XML file in your project's res/values/
directory, as discussed in Building a Simple User
Interface.
• Note: When creating icons and other bitmap images for your app, it's
important that you provide multiple versions that are each optimized
for a different screen density. This is discussed more in the lesson about
Supporting Different Screens.
If your app is using the Support Library
for compatibility on versions as low as Android 2.1, the showAsActionattribute
is not available from the android: namespace.
Instead this attribute is provided
by the Support Library and you must define your own XML namespace and use that
namespace as the attribute prefix.
(A custom XML namespace should be based on your
app name, but it can be any name you want and is only accessible within the scope
of the file in which you declare it.)
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Add the Actions to the Action Bar
To place the menu items into the action bar, implement
the onCreateOptionsMenu() callback method in your activity to
inflate the menu resource into the given Menu object.
For example:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Respond to Action Buttons
When the user presses one of the action buttons or another
item in the action overflow, the system calls your
activity's onOptionsItemSelected() callback method.
In your implementation of this method, call getItemId() on
the given MenuItem to determine which item was pressed—the
returned ID matches the value you declared in the
corresponding <item> element's android:id attribute.
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
All screens in your app that are not the main entrance to your app
(activities that are not the "home" screen) should offer the user a way to
navigate to the logical parent screen in the app's hierarchy by pressing
the Up button in the action bar.
Add Up Button for Low-level Activities
When running on Android 4.1 (API level 16) or higher, or when using
ActionBarActivity from the Support Library, performing Upnavigation simply
requires that you declare the parent activity in the manifest file and enable
the Up button for the action bar.
For example, here's how you can declare an activity's parent in the
manifest:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Then enable the app icon as the Up button by
calling setDisplayHomeAsUpEnabled():
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
Because the system now knows MainActivity is the parent activity for
DisplayMessageActivity, when the user presses the Up button,
the system navigates to the parent activity as appropriate—you do not
need to handle the Up button's event.
Chapter 2 lesson-1 adding the action bar

More Related Content

PPTX
Chapter 2 lesson-2 styling the action bar
PPTX
Android app development lesson 1
PDF
Android ui layout
PPTX
PPTX
ActionBarCompat Tutorial-Part 1(Prepare and Setup)
PPTX
Create an android app for database creation using.pptx
PPTX
Android Widget
PPTX
Android ui with xml
Chapter 2 lesson-2 styling the action bar
Android app development lesson 1
Android ui layout
ActionBarCompat Tutorial-Part 1(Prepare and Setup)
Create an android app for database creation using.pptx
Android Widget
Android ui with xml

What's hot (20)

PPT
Android UI Patterns
PPTX
Android development session 3 - layout
PPT
android layouts
PPT
View groups containers
PDF
Android Screen Containers & Layouts
ODP
Lab: Developing with the extension library
PPTX
CCI 2019 - PowerApps for Enterprise Developers
PDF
Android appwidget
PPTX
Android Training (Android UI)
PPTX
Android Tutorials : Basic widgets
ODP
Lab: Mobile App Development with XPages and Extension Library
PPS
11 asp.net session16
PPTX
iOS Development (Part 2)
PDF
Android Material Design APIs/Tips
PPTX
Android Tutorials - Powering with Selection Widget
PPTX
Material Design Android
PPT
1) workbench basics
PDF
Android layouts
PPTX
Web application development process
PPSX
11 asp.net session16
Android UI Patterns
Android development session 3 - layout
android layouts
View groups containers
Android Screen Containers & Layouts
Lab: Developing with the extension library
CCI 2019 - PowerApps for Enterprise Developers
Android appwidget
Android Training (Android UI)
Android Tutorials : Basic widgets
Lab: Mobile App Development with XPages and Extension Library
11 asp.net session16
iOS Development (Part 2)
Android Material Design APIs/Tips
Android Tutorials - Powering with Selection Widget
Material Design Android
1) workbench basics
Android layouts
Web application development process
11 asp.net session16
Ad

Viewers also liked (15)

PDF
IoT Business Reality & Patent Development
PDF
IoT Disruptive Innovation & Patent Exploitation
PDF
Startup Idea Pitching
PPTX
Performance Tuning en Azure SQL Database
PDF
Level Up Your Biml: Best Practices and Coding Techniques (TUGA IT 2016)
PPTX
Cinzia Battistella; Modeling a business ecosystem: a network analysis approach
PDF
Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)
PDF
Biml Academy 2 - Lesson 5: Importing source metadata into Biml
PPTX
WiFi direct
PDF
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
PDF
COP21 and the Philippines - Updates from the DENR
PDF
Firebase Auth Tutorial
PDF
17 Karakter yang Harus Dimiliki Seorang Entrepreneur
PPTX
Patrick geddes theory
PDF
DepEd Order no. 36 s. 2016
IoT Business Reality & Patent Development
IoT Disruptive Innovation & Patent Exploitation
Startup Idea Pitching
Performance Tuning en Azure SQL Database
Level Up Your Biml: Best Practices and Coding Techniques (TUGA IT 2016)
Cinzia Battistella; Modeling a business ecosystem: a network analysis approach
Level Up Your Biml: Best Practices and Coding Techniques (NTK 2016)
Biml Academy 2 - Lesson 5: Importing source metadata into Biml
WiFi direct
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
COP21 and the Philippines - Updates from the DENR
Firebase Auth Tutorial
17 Karakter yang Harus Dimiliki Seorang Entrepreneur
Patrick geddes theory
DepEd Order no. 36 s. 2016
Ad

Similar to Chapter 2 lesson-1 adding the action bar (20)

PPTX
Mobile Application Development-Components and Layouts
PPTX
Building a simple user interface lesson2
DOCX
Android action bar and notifications-chapter16
PPTX
Android Development Basics
PDF
Using android's action bar
PPTX
Android apps development
PPTX
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
PDF
Ap quiz app
PPTX
Android Programming.pptx
DOCX
Android building blocks and application life cycle-chapter3
DOCX
Lecture exercise on activities
DOCX
Leture5 exercise onactivities
DOCX
Android menus in android-chapter15
PPT
Android Bootcamp Tanzania:understanding ui in_android
PPTX
Android session 2-behestee
DOCX
Android resources in android-chapter9
PPT
Beginning Native Android Apps
PDF
Introduction to Andriod Studio Lecture note: Android Development Lecture 1.pdf
PPTX
Android application componenets for android app development
Mobile Application Development-Components and Layouts
Building a simple user interface lesson2
Android action bar and notifications-chapter16
Android Development Basics
Using android's action bar
Android apps development
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Ap quiz app
Android Programming.pptx
Android building blocks and application life cycle-chapter3
Lecture exercise on activities
Leture5 exercise onactivities
Android menus in android-chapter15
Android Bootcamp Tanzania:understanding ui in_android
Android session 2-behestee
Android resources in android-chapter9
Beginning Native Android Apps
Introduction to Andriod Studio Lecture note: Android Development Lecture 1.pdf
Android application componenets for android app development

More from Kalluri Vinay Reddy (10)

PPTX
Create an other activity lesson 3
PPTX
Social media marketing
PPTX
Data Centers and Internet
PPTX
Frame relay
PPTX
web development basics tables part2
PPTX
Web development basics 3
PPTX
Web development basics2
PPTX
Android basic
PPTX
Web development basics
PPTX
Inside Google
Create an other activity lesson 3
Social media marketing
Data Centers and Internet
Frame relay
web development basics tables part2
Web development basics 3
Web development basics2
Android basic
Web development basics
Inside Google

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
web development for engineering and engineering
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
composite construction of structures.pdf
PDF
Well-logging-methods_new................
PPTX
additive manufacturing of ss316l using mig welding
PPT
Project quality management in manufacturing
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT 4 Total Quality Management .pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CH1 Production IntroductoryConcepts.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
573137875-Attendance-Management-System-original
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Lesson 3_Tessellation.pptx finite Mathematics
web development for engineering and engineering
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
composite construction of structures.pdf
Well-logging-methods_new................
additive manufacturing of ss316l using mig welding
Project quality management in manufacturing
Digital Logic Computer Design lecture notes
UNIT 4 Total Quality Management .pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx

Chapter 2 lesson-1 adding the action bar

  • 1. By Kalluri Vinay Reddy Android Club www.facebook.com/vitccandroidclub Chapter-2 Adding the Action Bar lesson-1
  • 2. The action bar is one of the most important design elements you can implement for your app's activities. It provides several user interface features that make your app immediately familiar to users by offering consistency between other Android apps. Key functions include: A dedicated space for giving your app an identity and indicating the user's location in the app. Access to important actions in a predictable way (such as Search). Adding the Action Bar
  • 3. • Support for navigation and view switching (with tabs or drop-down lists). • This training class offers a quick guide to the action bar's basics.
  • 4. Lessons Setting Up the Action Bar Learn how to add a basic action bar to your activity, whether your app supports only Android 3.0 and higher or also supports versions as low as Android 2.1 (by using the Android Support Library). Adding Action Buttons Learn how to add and respond to user actions in the action bar. Styling the Action Bar Learn how to customize the appearance of your action bar. Overlaying the Action Bar Learn how to overlay the action bar in front of your layout, allowing for seamless transitions when hiding the action bar.
  • 5. • In its most basic form, the action bar displays the title for the activity and the app icon on the left. • Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app. Setting Up the Action Bar
  • 6. Support Android 3.0 and Above Only Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use the Theme.Holotheme (or one of its descendants), which is the default theme when either the targetSdkVersion orminSdkVersion attribute is set to "11" or greater. So to add the action bar to your activities, simply set either attribute to 11 or higher. For example: <manifest ... > <uses-sdk android:minSdkVersion="11" ... /> ... </manifest>
  • 7. Support Android 2.1 and Above Adding the action bar when running on versions older than Android 3.0 (down to Android 2.1) requires that you include the Android Support Library in your application. To get started, read the Support Library Setup document and set up the v7 appcompat library (once you've downloaded the library package, follow the instructions for Adding libraries with resources). Once you have the Support Library integrated with your app project: 1.Update your activity so that it extends ActionBarActivity. 2. For example:public class MainActivity extends ActionBarActivity { ... } 2.In your manifest file, update either the <application> element or individual <activity> 3.elements to use one of the Theme.AppCompat themes. For example:<activity android:theme="@style/Theme.AppCompat.Light" ... >
  • 8. Note: If you've created a custom theme, be sure it uses one of the Theme.AppCompat themes as its parent. Styling the Action Bar. Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher. Remember to properly set your app's API level support in the manifest: <manifest ... > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" /> ... </manifest>
  • 9. The action bar allows you to add buttons for the most important action items relating to the app's current context. Those that appear directly in the action bar with an icon and/or text are known as action buttons. Actions that can't fit in the action bar or aren't important enough are hidden in the action overflow. Adding Action Buttons
  • 10. Action bar with an action button for Search and the action overflow, which reveals additional actions.
  • 11. Specify the Actions in XML All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. Add an <item> element for each item you want to include in the action bar.
  • 12. res/menu/main_activity_actions.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" android:showAsAction="ifRoom" /> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showAsAction="never" /> </menu>
  • 13. This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings action should always appear in the overflow. (By default, all actions appear in the overflow, but it's good practice to explicitly declare your design intentions for each action.) The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you've saved in your project's res/drawable/ directory.
  • 14. For example, "@drawable/ic_action_search“ refers to ic_action_search.png. Likewise, the title attribute uses a string resource that's defined by an XML file in your project's res/values/ directory, as discussed in Building a Simple User Interface.
  • 15. • Note: When creating icons and other bitmap images for your app, it's important that you provide multiple versions that are each optimized for a different screen density. This is discussed more in the lesson about Supporting Different Screens.
  • 16. If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsActionattribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)
  • 17. res/menu/main_activity_actions.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showAsAction="ifRoom" /> ... </menu>
  • 18. Add the Actions to the Action Bar To place the menu items into the action bar, implement the onCreateOptionsMenu() callback method in your activity to inflate the menu resource into the given Menu object. For example: @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu); }
  • 19. Respond to Action Buttons When the user presses one of the action buttons or another item in the action overflow, the system calls your activity's onOptionsItemSelected() callback method. In your implementation of this method, call getItemId() on the given MenuItem to determine which item was pressed—the returned ID matches the value you declared in the corresponding <item> element's android:id attribute.
  • 20. @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.action_search: openSearch(); return true; case R.id.action_settings: openSettings(); return true; default: return super.onOptionsItemSelected(item); } }
  • 21. All screens in your app that are not the main entrance to your app (activities that are not the "home" screen) should offer the user a way to navigate to the logical parent screen in the app's hierarchy by pressing the Up button in the action bar. Add Up Button for Low-level Activities When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Upnavigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.
  • 22. For example, here's how you can declare an activity's parent in the manifest: <application ... > ... <!-- The main/home activity (it has no parent activity) --> <activity android:name="com.example.myfirstapp.MainActivity" ...> ... </activity> <!-- A child of the main activity --> <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application>
  • 23. Then enable the app icon as the Up button by calling setDisplayHomeAsUpEnabled(): @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_displaymessage); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // If your minSdkVersion is 11 or higher, instead use: // getActionBar().setDisplayHomeAsUpEnabled(true); }
  • 24. Because the system now knows MainActivity is the parent activity for DisplayMessageActivity, when the user presses the Up button, the system navigates to the parent activity as appropriate—you do not need to handle the Up button's event.