Top Banner
ActionBarCompat Tutorial Part 1-Prepare and Setup h ttp://www.mobiletuts.me [email protected] 06/26/2022 Mobiletuts.me [email protected]
24

ActionBarCompat Tutorial-Part 1(Prepare and Setup)

May 12, 2015

Download

Technology

Haining Lee

ActionBarCompat is sometimes confusing. I start a serial tutorials on it, hoping to help anyone who may need it.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

ActionBarCompat TutorialPart 1-Prepare and Setup

http://[email protected]

04/12/2023 Mobiletuts.me [email protected]

Page 2: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Action Bar

04/12/2023 Mobiletuts.me [email protected]

The action bar is a window feature that identifies the user location, and provides user actions and navigation modes. Using the action bar offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.

Figure 1. An action bar that includes the [1] app icon, [2] two action items, and [3] action overflow.

The action bar provides several key functions:

Provides a dedicated space for giving your app an identity and indicating the user's location in the app.Makes important actions prominent and accessible in a predictable way (such as Search).Supports consistent navigation and view switching within apps (with tabs or drop-down lists).

Page 3: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

ActionBarActivity or Activity?

If you want to set title , logo, navigation modes for an ActionBar, you need to get a reference to ActionBar.

With sdk level higher than 11 (>=11), you can directly call getActionBar() in an Activity to fetch a reference of ActionBar which you can configure.

For sdk level lower than 11 (<=10), however, getActionBar is not supported in an Activity.

04/12/2023 Mobiletuts.me [email protected]

Page 4: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

ActionBarActivity or Activity?

Google Android team released a new backward-compatible Action Bar implementation called ActionBarCompat that‘s part of the Support Library r18 during I/O 2013 . The ActionBarCompat APIs let you build the essential Action Bar design pattern into your app, with broad compatibility back to Android 2.1 (sdk7).

ActionBarActivity (android.support.v7.app.ActionBarActivity) and ActionBar (android.support.v7.app.ActionBar) was introduced within ActionBarCompat, which can be used essentially as same as Activity with ActionBar features in sdk levels higher than 11.

04/12/2023 Mobiletuts.me [email protected]

Page 5: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

ActionBarActivity for Backward Compatibility

Therefore use ActionBarActivity if you need to use ActionBar while supporting sdk level lower than 11.

04/12/2023 Mobiletuts.me [email protected]

Page 6: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Get Started with ActionBarCompat

ActionBarCompat is a new API in the Android Support Library that allows you to add an Action Bar to applications targeting minSdkVersion 7 or greater. The API and integration have been designed to closely mirror the existing framework APIs, giving you an easy migration path for your existing apps.

04/12/2023 Mobiletuts.me [email protected]

Page 7: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Add ActionBarCompat as a project dependency1. Android Studio or Eclipse+ADT go to Tools->Android->SDK Manager

2. Check if the Android Support Library (revision 18) has been installed.

The version of Android support library ( Revision 18 ) is released with a new library called appcompat under the package android.support.v7.The new library facilitates users to implement action bar back up to Android 2.1 ( API Level 7 ) .

04/12/2023 Mobiletuts.me [email protected]

Page 8: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Adding ActionBarCompat support library

EclipseMake sure you have downloaded the Android Support Library using the SDK Manager.Create a libs/ directory in the root of your application project.Copy the JAR files from your Android SDK installation directory /extras/android/support/v7/appcompat/libs/android-support-v4.jar/extras/android/support/v7/appcompat/libs/android-support-v7-appcompat.jarinto your application's project libs/ directory.Right click the JAR file and select Build Path > Add to Build Path.

Android Studio Make sure you have downloaded the Android Support Repository using the SDK Manager.Open the build.gradle file for your application.Add the support library to the dependencies section. For example, to add the v7 appcompat library, add the following lines:dependencies {

…compile 'com.android.support:appcompat-v7:18.0.0'

}

04/12/2023 Mobiletuts.me [email protected]

Page 9: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Create New Android Project

04/12/2023 Mobiletuts.me [email protected]

Page 10: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Create New Android Project

04/12/2023 Mobiletuts.me [email protected]

Page 11: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Dependency

The Gradle build script dependency identifier for this library is as follows:com.android.support:appcompat-v7:18.0.0+This dependency notation specifies the release version 18.0.0 or higher.

04/12/2023 Mobiletuts.me [email protected]

Page 12: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

When using default Activity

04/12/2023 Mobiletuts.me [email protected]

Page 13: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Default Activity support ActionBar on level higher than API 11

04/12/2023 Mobiletuts.me [email protected] on API17

Page 14: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Use ActionBarActivity for Backward Compatibility

04/12/2023 Mobiletuts.me [email protected]

Using ActionBarActivity instead of Activity

Done? Not yet, if you compile and run now, you will run into fata error caused by theme setting.

Page 15: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Error Caused by Theme Setting

04/12/2023 Mobiletuts.me [email protected]

This is cause by theme setting. When using ActionBarCompat, you can only use a Theme.AppCompat theme or its descendant (for customized theme).

Page 16: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Update Style Resources

04/12/2023 Mobiletuts.me [email protected]

The first things to update are your Activity themes so they use a Theme.AppCompat theme. If you're currently using one of the Theme.Holo themes then just replace this with the related Theme.AppCompat theme. For instance if you have the following in your AndroidManifest.xml:

<activity ... android:theme="@android:style/Theme.Holo" />You would change it to:

<activity ... android:theme="@style/Theme.AppCompat" />

UseTheme.AppCompat theme or its descendant

Page 17: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Update Style Resources

04/12/2023 Mobiletuts.me [email protected]

UseTheme.AppCompat theme or its descendantIf you don’t want to change your activities’ theme setting one by one, you can change the theme setting for application in AndroidManifest.xml: <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">

<style name="AppBaseTheme" parent="android:Theme.Light"></style>

<!-- Application theme. Which is a decendant of Theme.App --> <style name="AppTheme" parent="AppBaseTheme"></style>

Page 18: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Becareful with values-v11/14 When you update the style resource files, do update all the styles.xml in values folder and the possible values-v11 and values-v14 folder. Cause your app may load styles.xml file in values-v1X folder according to target Android API level!

04/12/2023 Mobiletuts.me [email protected]

Page 19: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Try again, it works now!

04/12/2023 Mobiletuts.me [email protected]

Running on API17

Page 20: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Add More Actions on Action Bar

04/12/2023 Mobiletuts.me [email protected]

In the following sections, I will show how to add Actions such as Search to Action Bar.

You can download these icons which you can use for Actions from Download the Action Bar Icon Pack

Then copy the icons you need to drawable folders. Here I copied 2_action_search.png files to drawable-hdpi, drawable-xhdpi and drawable-mdpi folders.

Page 21: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Structure of Action Bar Icon Pack

04/12/2023 Mobiletuts.me [email protected]

Page 22: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Modify Menu Resources

04/12/2023 Mobiletuts.me [email protected]

As with the standard Action Bar in Android 3.0 and later versions, action items are added via the options menu. The difference with ActionBarCompat is that you need to modify your Menu resource files so that any action item attributes come from ActionBarCompat's XML namespace.

Page 23: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

Now, you did it.

04/12/2023 Mobiletuts.me [email protected]

Compile and run again.

Running on API 17 Running on API 7

Page 24: ActionBarCompat Tutorial-Part 1(Prepare and Setup)

To be continued

• In next tutorial, I will show you how to 1. Configure menu items;2. Add menu items callback (how to interact

with user through Action Bar);3. Something else

04/12/2023 Mobiletuts.me [email protected]