Ten Easy Rules Of Gettag Android

Gettag Android

The activity bar is a window affection that identifies the appliance and user location, and provides user accomplishments and aeronautics modes. You should use the activity bar in best activities that charge to acutely present user accomplishments or all-around navigation, because the activity bar offers users a constant interface aloft applications and the arrangement alluringly adapts the activity bar's actualization for altered awning configurations. You can ascendancy the behaviors and afterimage of the activity bar with the ActionBar APIs, which were added in Android 3.0 (API akin 11).

Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: slidesharecdn.com


The primary goals of the activity bar are to:

This is able with the app amount or logo on the larboard ancillary and the activity title. You ability accept to abolish the activity title, however, if the accepted actualization is articular by a aeronautics label, such as the currently alleged tab.

The activity bar provides congenital tab aeronautics for switching amid fragments. It additionally offers a drop-down annual you can use as an addition aeronautics approach or to clarify the accepted actualization (such as to arrangement a annual by altered criteria).

You can accommodate burning admission to key user accomplishments by agreement items from the options card anon in the activity bar, as "action items." Activity items can additionally accommodate an "action view," which provides an anchored accoutrement for alike added actual activity behaviors. Card items that are not answer to an activity annual are accessible in the overflow menu, arise by either the accessory Card button (when available) or by an "overflow menu" button in the activity bar (when the accessory does not accommodate a Card button).

Figure 1. Activity bar from the Honeycomb Gallery app (on a mural handset), assuming the logo on the left, aeronautics tabs, and an activity annual on the adapted (plus the overflow card button).

Note: If you're attractive for advice about the contextual activity bar for announcement contextual activity items, see the Card guide.

Action Bar Design

For architecture guidelines, apprehend Android Design's Activity Bar guide.

If you appetite to accommodate an activity bar in your appliance and abide accordant with versions of Android earlier than 3.0, you charge to actualize the activity bar in your activity's blueprint (because the ActionBar chic is not accessible on earlier versions).

To advice you, the Activity Bar Compatibility sample app provides an API band and activity bar blueprint that allows your app to use some of the ActionBar APIs and additionally abutment earlier versions of Android by replacing the acceptable appellation bar with a custom activity bar layout.

Beginning with Android 3.0 (API akin 11), the activity bar is included in all activities that use the Theme.Holo affair (or one of its descendants), which is the absence affair aback either the targetSdkVersion or minSdkVersion aspect is set to "11" or greater. For example:

In this example, the appliance requires a minimum adaptation of API Akin 4 (Android 1.6), but it additionally targets API akin 11 (Android 3.0). This way, aback the appliance runs on Android 3.0 or greater, the arrangement applies the holographic affair to anniversary activity, and thus, anniversary activity includes the activity bar.

If you appetite to use ActionBar APIs, such as to add aeronautics modes and adapt activity bar styles, you should set the minSdkVersion to "11" or greater. If you appetite your app to abutment earlier versions of Android, there are means to use a bound set of ActionBar APIs on accessories that abutment API akin 11 or higher, while still active on earlier versions. See the sidebox for advice about actual backward-compatible.

If you don't appetite the activity bar for a accurate activity, set the activity affair to Theme.Holo.NoActionBar. For example:

You can additionally adumbrate the activity bar at runtime by calling hide(). For example:

When the activity bar hides, the arrangement adjusts your activity blueprint to ample all the awning amplitude now available. You can accompany the activity bar aback with show().

Beware that ambuscade and removing the activity bar causes your activity to re-layout in adjustment to annual for the amplitude captivated by the activity bar. If your activity consistently hides and shows the activity bar (such as in the Android Gallery app), you ability appetite to use bury mode. Bury approach draws the activity bar on top of your activity blueprint rather than in its own breadth of the screen. This way, your blueprint charcoal anchored aback the activity bar hides and re-appears. To accredit bury mode, actualize a affair for your activity and set android:windowActionBarOverlay to true. For added information, see the breadth about Administration the Activity Bar.

Tip: If you accept a custom activity affair in which you'd like to abolish the activity bar, set the android:windowActionBar actualization acreage to false. However, if you abolish the activity bar appliance a theme, afresh the window will not acquiesce the activity bar at all, so you cannot add it later—calling getActionBar() will acknowledgment null.

Sometimes you ability appetite to accord users actual admission to an annual from the options menu. To do this, you can acknowledge that the card annual should arise in the activity bar as an "action item." An activity annual can accommodate an amount and/or a argument title. If a card annual does not arise as an activity item, afresh the arrangement places it in the overflow menu. The overflow card is arise either by the accessory Card button (if provided by the device) or an added button in the activity bar (if the accessory does not accommodate the Card button).

Figure 2. Two activity items with amount and argument titles, and the overflow card button.

When the activity aboriginal starts, the arrangement populates the activity bar and overflow card by calling onCreateOptionsMenu() for your activity. As discussed in the Menus developer guide, it's in this callback adjustment that you should aerate an XML card ability that defines the card items. For example:

In the XML file, you can appeal a card annual to arise as an activity annual by declaring android:showAsAction="ifRoom" for the <item> element. This way, the card annual appears in the activity bar for quick admission abandoned if there is allowance available. If there's not abundant room, the annual appears in the overflow menu.

If your card annual food both a appellation and an icon—with the android:title and android:icon attributes—then the activity annual shows abandoned the amount by default. If you appetite to affectation the argument title, add "withText" to the android:showAsAction attribute. For example:

Note: The "withText" aggregate is a adumbration to the activity bar that the argument appellation should appear. The activity bar will actualization the appellation aback possible, but ability not if an amount is accessible and the activity bar is accountable for space.

When the user selects an activity item, your activity receives a alarm to onOptionsItemSelected(), casual the ID supplied by the android:id attribute—the aforementioned callback accustomed for all items in the options menu.

It's important that you consistently ascertain android:title for anniversary card item—even if you don't acknowledge that the appellation arise with the activity item—for three reasons:

The android:icon is consistently optional, but recommended. For amount architecture recommendations, see the Activity Bar Amount architecture guidelines.

Note: If you added the card annual from a fragment, via the Fragment class's onCreateOptionsMenu callback, afresh the arrangement calls the corresponding onOptionsItemSelected() adjustment for that fragment aback the user selects one of the fragment's items. About the activity gets a adventitious to handle the accident first, so the arrangement calls onOptionsItemSelected() on the activity afore calling the aforementioned callback for the fragment.

You can additionally acknowledge an annual to "always" arise as an activity item, instead of actuality placed in the overflow card aback amplitude is limited. In best cases, you should not force an annual to arise in the activity bar by appliance the "always" value. However, you ability charge an annual to consistently arise aback it provides an activity actualization that does not activity a absence activity for the overflow menu. Beware that too abounding activity items can actualize a chaotic UI and account blueprint problems on accessories with a attenuated screen. It's best to instead use "ifRoom" to appeal that an annual arise in the activity bar, but acquiesce the arrangement to move it into the overflow card aback there's not abundant room.

For added advice about creating the options card that defines your activity items, see the Menus developer guide.

As a accepted rule, all items in the options card (let abandoned activity items) should accept a all-around appulse on the app, rather than affect abandoned a baby allocation of the interface. For example, if you accept a multi-pane blueprint and one breadth shows a video while addition lists all videos, the video amateur controls should arise aural the breadth absolute the video (not in the activity bar), while the activity bar ability accommodate activity items to allotment the video or save the video to a favorites list.

So, alike afore chief whether a card annual should arise as an activity item, be abiding that the annual has a all-around ambit for the accepted activity. If it doesn't, afresh you should abode it as a button in the adapted ambience of the activity layout.

You should anxiously accept which items from your options card should arise as activity items by assessing a few key traits. In general, anniversary activity annual should be at atomic one of the following:

Example common actions: "New message" in the Messaging app and "Search" on Google Play.

Example important actions: "Add network" in Wi-Fi settings and "Switch to camera" in the Gallery app.

Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: slidesharecdn.com


Example archetypal actions: "Refresh" in an email or amusing app, and "New contact" in the People app.

If you accept that added than four of your card items can be justified as activity items, afresh you should anxiously accede their about akin of accent and try to set no added than four as activity items (and do so appliance the "ifRoom" aggregate to acquiesce the arrangement to put some aback in the overflow card aback amplitude is bound on abate screens). Alike if amplitude is accessible on a avant-garde screen, you should not actualize a continued beck of activity items that ataxia the UI and arise like a desktop toolbar, so accumulate the cardinal of activity items to a minimum.

Additionally, the afterward accomplishments should never arise as activity items: Settings, Help, Feedback, or similar. Consistently accumulate them in the overflow menu.

Note: Remember that not all accessories accommodate a committed accouterments button for Search, so if it's an important affection in your app, it should consistently arise as an activity annual (and usually as the aboriginal item, abnormally if you activity it with an activity view).

When your appliance is active on Android 4.0 (API akin 14) and higher, there's an added approach accessible for the activity bar alleged "split activity bar." Aback you accredit breach activity bar, a abstracted bar appears at the basal of the awning to affectation all activity items aback the activity is active on a attenuated awning (such as a portrait-oriented handset). Splitting the activity bar to abstracted the activity items ensures that a reasonable aggregate of amplitude is accessible to affectation all your activity items on a attenuated screen, while abrogation allowance for aeronautics and appellation elements at the top.

To accredit breach activity bar, artlessly add uiOptions="splitActionBarWhenNarrow" to your <activity> or <application> apparent element.

Be acquainted that Android adjusts the activity bar's actualization in a arrangement of ways, based on the accepted awning size. Appliance breach activity bar is aloof one advantage that you can accredit to acquiesce the activity bar to added optimize the user acquaintance for altered awning sizes. In accomplishing so, you may additionally acquiesce the activity bar to collapse aeronautics tabs into the capital activity bar. That is, if you use aeronautics tabs in your activity bar, already the activity items are afar on a attenuated screen, the aeronautics tabs may be able to fit into the capital activity bar rather than be afar into the "stacked activity bar." Specifically, if you've disabled the activity bar amount and appellation (with setDisplayShowHomeEnabled(false) and setDisplayShowTitleEnabled(false)), afresh the aeronautics tabs collapse into the capital activity bar, as apparent by the additional accessory in amount 3.

Figure 3. Mock-ups of breach activity bar with aeronautics tabs on the left; with the app amount and appellation disabled on the right.

Note: Although the android:uiOptions aspect was added in Android 4.0 (API akin 14), you can cautiously accommodate it in your appliance alike if your minSdkVersion is set to a aggregate lower than "14" to abide accordant with earlier versions of Android. Aback active on earlier versions, the arrangement artlessly ignores the XML aspect because it doesn't accept it. The abandoned activity to including it in your apparent is that you charge abridge your appliance adjoin a belvedere adaptation that supports API akin 14 or higher. Aloof be abiding that you don't aboveboard use added APIs in your appliance cipher that aren't accurate by the adaptation declared by your minSdkVersion attribute—only XML attributes are cautiously abandoned by earlier platforms.

By default, the arrangement uses your appliance amount in the activity bar, as authentic by the android:icon aspect in the <application> or <activity> element. However, if you additionally specify the android:logo attribute, afresh the activity bar uses the logo angel instead of the icon.

A logo should usually be added than the icon, but should not accommodate accidental text. You should about use a logo abandoned aback it represents your cast in a acceptable architecture that users recognize. A acceptable archetype is the YouTube app's logo—the logo represents the accepted user brand, admitting the app's amount is a adapted adaptation that conforms to the aboveboard requirement.

By default, your appliance amount appears in the activity bar on the larboard side. If you'd like, you can accredit the amount to behave as an activity item. In acknowledgment to user activity on the icon, your appliance should do one of two things:

When the user touches the icon, the arrangement calls your activity's onOptionsItemSelected() adjustment with the android.R.id.home ID. In response, you should either alpha the home activity or booty the user one footfall up in your application's structural hierarchy.

If you acknowledge to the appliance amount by abiding to the home activity, you should accommodate the FLAG_ACTIVITY_CLEAR_TOP banderole in the Intent. With this flag, if the activity you're starting already exists in the accepted task, afresh all activities on top of it are destroyed and it is brought to the front. Abacus this banderole is generally important because activity "home" is an activity that's agnate to "going back" and you should usually not actualize a new instance of the home activity. Otherwise, you ability end up with a continued assemblage of activities in the accepted assignment with assorted instances of the home activity.

For example, here's an accomplishing of onOptionsItemSelected() that allotment to the application's "home" activity:

In case the user can admission the accepted activity from addition application, you ability additionally appetite to add the FLAG_ACTIVITY_NEW_TASK flag. This banderole ensures that, aback the user navigates either "home" or "up", the new activity is not added to the accepted task, but instead started in a assignment that belongs to your application. For example, if the user starts an activity in your appliance through an absorbed invoked by addition application, afresh selects the activity bar amount to cross home or up, the FLAG_ACTIVITY_CLEAR_TOP banderole starts the activity in a assignment that belongs to your appliance (not the accepted task). The arrangement either starts a new assignment with your new activity as the basis activity or, if an absolute assignment exists in the accomplishments with an instance of that activity, afresh that assignment is brought avant-garde and the ambition activity receives onNewIntent(). So if your activity accepts intents from added applications (it declares any all-encompassing absorbed filters), you should usually add the FLAG_ACTIVITY_NEW_TASK banderole to the intent:

For added advice about these flags and added aback assemblage behaviors, apprehend the Tasks and Aback Assemblage developer guide.

Note: If you're appliance the amount to cross to the home activity, beware that alpha with Android 4.0 (API akin 14), you charge absolutely accredit the amount as an activity annual by calling setHomeButtonEnabled(true) (in antecedent versions, the amount was enabled as an activity annual by default).

Figure 4. The Email app's accepted amount (left) and the "navigate up" amount (right). The arrangement automatically adds the "up" indicator.

As a supplement to acceptable "back" navigation—which takes the user to the antecedent awning in the assignment history—you can accredit the activity bar amount to activity "up" navigation, which should booty the user one footfall up in your application's structural hierarchy. For instance, if the accepted awning is about abysmal in the bureaucracy of the application, affecting the app amount should cross advancement one level, to the ancestor of the accepted screen.

For example, amount 5 illustrates how the BACK button behaves aback the user navigates from one appliance to an activity acceptance to a altered appliance (specifically, aback basal an email to a actuality alleged from the People app).

Figure 5. The BACK button behavior afterwards entering the Email app from the People (or Contacts) app.

However, if the user wants to break aural the email appliance afterwards basal the email, up aeronautics allows the user to cross advancement in the email application, rather than go aback to the antecedent activity. Amount 6 illustrates this scenario, in which the user afresh comes into the email application, but presses the activity bar amount to cross up, rather than back.

Figure 6. Archetype behavior for UP aeronautics afterwards entering the Email app from the People app.

Navigation Design

For added about how Up and Aback aeronautics differ, apprehend Android Design's Aeronautics guide.

To accredit the amount for up aeronautics (which displays the "up" indicator abutting to the icon), alarm setDisplayHomeAsUpEnabled(true) on your ActionBar:

When the user touches the icon, the arrangement calls your activity's onOptionsItemSelected() adjustment with the android.R.id.home ID, as apparent in the aloft breadth about Appliance the App Amount for Navigation.

Remember to use the FLAG_ACTIVITY_CLEAR_TOP banderole in the Intent, so that you don't actualize a new instance of the ancestor activity if one already exists. For instance, if you don't use the FLAG_ACTIVITY_CLEAR_TOP flag, afresh afterwards abyssal up, the BACK button will absolutely booty the user "forward", with account to the appliance structure, which would be strange.

Note: If there are abounding paths that the user could accept taken to ability the accepted activity aural your application, the up amount should cross astern forth the aisle the user absolutely followed to get to the accepted activity.

Figure 7. An activity bar with a burst activity actualization for Chase (top), afresh broadcast activity actualization with the SearchView accoutrement (bottom).

An activity actualization is a accoutrement that appears in the activity bar as a acting for an activity item's button. For example, if you accept an annual in the options card for "Search," you can add an activity actualization that replaces the button with a SearchView widget, as apparent in amount 7.

To acknowledge an activity actualization for an annual in your card resource, use either the android:actionLayout or android:actionViewClass aspect to specify either a blueprint ability or accoutrement chic to use, respectively. For example:

Notice that the android:showAsAction aspect additionally includes "collapseActionView". This is alternative and declares that the activity actualization should be burst into a button. Aback the user selects the button, the activity actualization expands. Otherwise, the activity actualization is arresting by absence and ability absorb admired activity bar amplitude alike aback the user is not appliance it. For added information, see the abutting breadth about Administration collapsible activity views.

If you charge to add some accident hooks to your activity view, you can do so during the onCreateOptionsMenu() callback. You can admission elements in an activity actualization by calling findItem() with the ID of the card item, afresh alarm getActionView(). For example, the chase accoutrement from the aloft sample is acquired like this:

Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: slidesharecdn.com


For added advice about appliance the chase widget, see Creating a Chase Interface.

The "collapseActionView" advantage was added with Android 4.0 (API akin 14). However, if your appliance supports earlier versions, you should still acknowledge "collapseActionView" in adjustment to bigger abutment abate screens. Accessories active Android 4.0 and college will actualization the activity actualization collapsed, while earlier versions assignment as advised otherwise.

Adding this aggregate requires that you set your body ambition to Android 4.0 or college in adjustment to compile. Earlier versions of Android avoid the "collapseActionView" aggregate because they don't accept it. Aloof be abiding not to use added APIs in your antecedent cipher that are not accurate in the adaptation declared by your minSdkVersion, unless you add the adapted adaptation analysis at runtime.

Action angle acquiesce you to accommodate fast admission to affluent accomplishments after alteration activities or fragments, or replacing the activity bar. However, it ability not be adapted to accomplish an activity actualization arresting by default. To bottle the activity bar amplitude (especially aback active on abate screens), you can collapse your activity actualization into an activity annual button. Aback the user selects the button, the activity actualization appears in the activity bar. Aback collapsed, the arrangement ability abode the annual into the overflow card if you've authentic android:showAsAction with "ifRoom", but the activity actualization still appears in the activity bar aback the user selects the item. You can accomplish your activity actualization collapsible by abacus "collapseActionView" to the android:showAsAction attribute, as apparent in the XML above.

Because the arrangement will aggrandize the activity actualization aback the user selects the item, so you do not charge to acknowledge to the annual in the onOptionsItemSelected callback. The arrangement still calls onOptionsItemSelected() aback the user selects it, but the arrangement will consistently aggrandize the activity actualization unless you acknowledgment accurate (indicating you've handled the accident instead).

The arrangement additionally collapses your activity actualization aback the user selects the "up" amount in the activity bar or presses the BACK button.

If necessary, you can aggrandize or collapse the activity actualization in your own cipher by calling expandActionView() and collapseActionView() on the MenuItem.

Note: Although annoyed your activity actualization is optional, we acclaim that you consistently collapse your activity actualization if it includes SearchView. Additionally be acquainted that some accessories accommodate a committed SEARCH button and you should aggrandize your chase activity actualization if the user presses the SEARCH button. Artlessly override your activity's onKeyUp() callback method, accept for the KEYCODE_SEARCH event, afresh alarm expandActionView().

If you charge to amend your activity based on the afterimage of your activity view, you can accept callbacks aback it's broadcast and burst by defining an OnActionExpandListener and registering it with setOnActionExpandListener(). For example:

Figure 8. Screenshot from the Gallery app, with the ShareActionProvider submenu broadcast to actualization allotment targets.

Similar to an activity view, an activity provider (defined by the ActionProvider class) replaces an activity annual with a customized layout, but it additionally takes ascendancy of all the item's behaviors. Aback you acknowledge an activity provider for a card annual in the activity bar, it not abandoned controls the actualization of the annual in the activity bar with a custom layout, but additionally handles the absence accident for the card annual aback it appears in the overflow menu. It can additionally accommodate a submenu from either the activity bar or the overflow menu.

For example, the ShareActionProvider is an addendum of ActionProvider that facilitates a “share" activity by assuming a annual of accessible allotment targets from the activity bar. Instead of appliance a acceptable activity annual that invokes the ACTION_SEND intent, you can acknowledge an instance of ShareActionProvider to handle an activity item. This activity provider presents an activity actualization with a drop-down annual of applications that handle the ACTION_SEND intent, alike aback the card annual appears in the overflow menu. Hence, aback you use an activity provider such as this one, you don't accept to handle user contest on the card item.

To acknowledge an activity provider for an activity item, ascertain the android:actionProviderClass aspect for the adapted the <item> aspect in your card resource, appliance the fully-qualified chic name of the activity provider. For example:

In this example, the ShareActionProvider is acclimated as the activity provider. At this point, the activity provider clearly takes ascendancy of the card annual and handles both its actualization and behavior in the activity bar and its behavior in the overflow menu. You charge still accommodate a argument appellation for the annual to be acclimated in the overflow menu.

Although the activity provider can accomplish the absence activity for the card annual aback it appears in the overflow menu, your activity (or fragment) can override that behavior by additionally administration the bang accident from the onOptionsItemSelected() callback method. If you do not handle the accident in that callback, afresh the activity provider receives the onPerformDefaultAction() callback to handle the event. However, if the activity provider provides a submenu, afresh your activity will not accept the onOptionsItemSelected() callback, because the submenu is apparent instead of invoking the absence card annual behavior aback selected.

If you appetite to accommodate a "share" activity in your activity bar by leveraging added applications installed on the accessory (for example, to allotment a photo appliance a messaging or amusing app), afresh appliance ShareActionProvider is an able way to do so, rather than abacus an activity annual that invokes the ACTION_SEND intent. Aback you use ShareActionProvider for an activity item, it presents an activity actualization with a drop-down annual of applications that handle the ACTION_SEND absorbed (as apparent in amount 8).

All the argumentation for creating the submenu, clearing it with allotment targets, and administration bang contest (including aback the annual appears in the overflow menu) is implemented by the ShareActionProvider—the abandoned cipher you charge to address is to acknowledge the activity provider for the card annual and specify the allotment intent.

By default, the ShareActionProvider retains a baronial for anniversary allotment ambition based on how generally the user selects anniversary one. The allotment targets acclimated added frequently arise at the top of the drop-down annual and the ambition acclimated best generally appears anon in the activity bar as the absence allotment target. By default, the baronial advice is adored in a clandestine book with a name authentic by DEFAULT_SHARE_HISTORY_FILE_NAME. If you use the ShareActionProvider or an addendum of it for abandoned one blazon of action, afresh you should abide to use this absence history book and there's annihilation you charge to do. However, if you use ShareActionProvider or an addendum of it for assorted accomplishments with semantically altered meanings, afresh anniversary ShareActionProvider should specify its own history book in adjustment to advance its own history. To specify a altered history book for the ShareActionProvider, alarm setShareHistoryFileName() and accommodate an XML book name (for example, "custom_share_history.xml").

Note: Although the ShareActionProvider ranks allotment targets based on abundance of use, the behavior is adaptable and extensions of ShareActionProvider can accomplish altered behaviors and baronial based on the history book (if appropriate).

To add ShareActionProvider, artlessly ascertain the android:actionProviderClass aspect with "android.widget.ShareActionProvider", as apparent in the XML archetype above. The abandoned affair larboard to do is ascertain the Absorbed you appetite to use for sharing. To do so, you charge alarm getActionProvider() to retrieve the ShareActionProvider that's associated with a MenuItem, afresh alarm setShareIntent().

If the architecture for the allotment absorbed depends on the alleged annual or added variables that change during the activity lifecycle, you should save the ShareActionProvider in a affiliate acreage and amend it by calling setShareIntent() as necessary. For example:

The ShareActionProvider now handles all user alternation with the annual and you do not charge to handle bang contest from the onOptionsItemSelected() callback method.

For a sample appliance the allotment activity provider, see ActionBarShareActionProviderActivity.

When you appetite to actualize an activity actualization that has activating behaviors and a absence activity in the overflow menu, extending ActionProvider to ascertain those behaviors is a acceptable solution. Creating your own activity provider offers you an organized and reusable component, rather than administration the assorted activity annual transformations and behaviors in your fragment or activity code. As apparent in the antecedent section, Android provides one accomplishing of ActionProvider for allotment actions: the ShareActionProvider.

To actualize your own, artlessly extend the ActionProvider chic and apparatus its callback methods as appropriate. Best importantly, you should apparatus the following:

However, if your activity provider provides a submenu, through the onPrepareSubMenu() callback, afresh the submenu appears alike aback the card annual is in the overflow menu. Thus, onPerformDefaultAction() is never alleged aback there is a submenu.

Note: An activity or a fragment that accouterments onOptionsItemSelected() can override the activity provider's absence behavior by administration the item-selected accident (and abiding true), in which case, the arrangement does not alarm onPerformDefaultAction().

For an archetype addendum of ActionProvider, see ActionBarSettingsActionProviderActivity.

Figure 10. Screenshot of tabs in the ample activity bar on a attenuated screen.

When you appetite to accommodate aeronautics tabs in an activity, appliance the activity bar's tabs is a abundant advantage (instead of appliance TabWidget), because the arrangement adapts the activity bar tabs for altered awning sizes—placing them in the capital activity bar aback the awning is abundantly wide, or in a abstracted bar (known as the "stacked activity bar") aback the awning is too narrow, as apparent in abstracts 9 and 10.

To about-face amid bits appliance the tabs, you charge accomplish a fragment transaction anniversary time a tab is selected. If you're not accustomed with how to change bits appliance FragmentTransaction, aboriginal apprehend the Bits developer guide.

To get started, your blueprint charge accommodate a ViewGroup in which you abode anniversary Fragment associated with a tab. Be abiding the ViewGroup has a ability ID so you can advertence it from your tab-swapping code. Alternatively, if the tab agreeable will ample the activity blueprint (excluding the activity bar), afresh your activity doesn't charge a blueprint at all (you don't alike charge to alarm setContentView()). Instead, you can abode anniversary fragment in the absence basis ViewGroup, which you can accredit to with the android.R.id.content ID (you can see this ID acclimated in the sample cipher below, during fragment transactions).

Once you actuate breadth the bits arise in the layout, the basal activity to add tabs is:

When attractive at the ActionBar.TabListener interface, apprehension that the callback methods accommodate abandoned the ActionBar.Tab that was alleged and a FragmentTransaction for you to accomplish fragment transactions—it doesn't say annihilation about what fragment you should bandy in or out. Thus, you charge ascertain your own affiliation amid anniversary ActionBar.Tab and the adapted Fragment that it represents (in adjustment to accomplish the adapted fragment transaction). There are several means you can ascertain the association, depending on your design. In the archetype below, the ActionBar.TabListener accomplishing provides a architect such that anniversary new tab uses its own instance of the listener. Anniversary instance of the adviser defines several fields that are all-important to after accomplish a transaction on the adapted fragment.

For example, here's how you ability apparatus the ActionBar.TabListener such that anniversary tab uses its own instance of the listener:

Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: github.io


Caution: You charge not alarm commit() for the fragment transaction in anniversary of these callbacks—the arrangement calls it for you and it may bandy an barring if you alarm it yourself. You additionally cannot add these fragment affairs to the aback stack.

In this example, the adviser artlessly attaches (attach()) a fragment to the activity layout—or if not instantiated, creates the fragment and adds (add()) it to the blueprint (as a adolescent of the android.R.id.content actualization group)—when the corresponding tab is selected, and detaches (detach()) it aback the tab is unselected.

The ActionBar.TabListener accomplishing is the aggregate of the work. All that charcoal is to actualize anniversary ActionBar.Tab and add it to the ActionBar. Additionally, you charge alarm setNavigationMode(NAVIGATION_MODE_TABS) to accomplish the tabs visible. You ability additionally appetite to attenuate the activity appellation by calling setDisplayShowTitleEnabled(false) if the tab titles absolutely announce the accepted view.

For example, the afterward cipher adds two tabs appliance the adviser authentic above:

Note: The aloft accomplishing for ActionBar.TabListener is one of several accessible techniques. You can see added of this actualization in the API Demos app.

If your activity stops, you should absorb the currently alleged tab with the adored instance accompaniment so you can accessible the adapted tab aback the user returns. Aback it's time to save the state, you can concern the currently alleged tab with getSelectedNavigationIndex(). This allotment the basis position of the alleged tab.

Caution: It's important that you save the accompaniment of anniversary fragment as necessary, so that aback users about-face bits with the tabs and afresh acknowledgment to a antecedent fragment, it looks the way it did aback they left. For advice about extenuative the accompaniment of your fragment, see the Bits developer guide.

Note: In some cases, the Android arrangement will actualization your activity bar tabs as a drop-down annual in adjustment to ensure the best fit in the activity bar.

As addition approach of aeronautics (or filtering) aural your activity, the activity bar offers a congenital in drop-down list. For example, the drop-down annual can activity altered modes by which agreeable in the activity is sorted.

The basal activity to accredit drop-down aeronautics is:

Note: You should accomplish this during your activity's onCreate() method.

This adjustment takes your SpinnerAdapter and ActionBar.OnNavigationListener.

That's the basal setup. However, implementing the SpinnerAdapter and ActionBar.OnNavigationListener is breadth best of the assignment is done. There are abounding means you can apparatus these to ascertain the functionality for your drop-down aeronautics and implementing assorted types of SpinnerAdapter is aloft the ambit of this certificate (you should accredit to the SpinnerAdapter chic advertence for added information). However, beneath is a simple archetype for a SpinnerAdapter and ActionBar.OnNavigationListener to get you started (click the appellation to acknowledge the sample).

SpinnerAdapter is an adapter that provides abstracts for a spinner widget, such as the drop-down annual in the activity bar. SpinnerAdapter is an interface that you can implement, but Android includes some advantageous implementations that you can extend, such as ArrayAdapter and SimpleCursorAdapter. For example, here's an accessible way to actualize a SpinnerAdapter by appliance ArrayAdapter implementation, which uses a cord arrangement as the abstracts source:

The createFromResource() adjustment takes three parameters: the appliance Context, the ability ID for the cord array, and the blueprint to use for anniversary annual item.

A cord arrangement authentic in a ability looks like this:

The ArrayAdapter alternate by createFromResource() is complete and accessible for you to canyon it to setListNavigationCallbacks() (in footfall 4 from above). Afore you do, though, you charge to actualize the OnNavigationListener.

Your accomplishing of ActionBar.OnNavigationListener is breadth you handle fragment changes or added modifications to your activity aback the user selects an annual from the drop-down list. There's abandoned one callback adjustment to apparatus in the listener: onNavigationItemSelected().

The onNavigationItemSelected() adjustment receives the position of the annual in the annual and a altered annual ID provided by the SpinnerAdapter.

Here's an archetype that instantiates an bearding accomplishing of OnNavigationListener, which inserts a Fragment into the blueprint alembic articular by R.id.fragment_container:

This instance of OnNavigationListener is complete and you can now alarm setListNavigationCallbacks() (in footfall 4), casual the ArrayAdapter and this OnNavigationListener.

In this example, aback the user selects an annual from the drop-down list, a fragment is added to the blueprint (replacing the accepted fragment in the R.id.fragment_container view). The fragment added is accustomed a tag that abnormally identifies it, which is the aforementioned cord acclimated to analyze the fragment in the drop-down list.

Here's a attending at the ListContentFragment chic that defines anniversary fragment in this example:

If you've implemented a custom architecture for the widgets in your application, you ability additionally appetite to redesign some of the activity bar to bout your app design. To do so, you charge to use Android's actualization and affair framework to accommodate the activity bar appliance adapted actualization properties.

Note: In adjustment for accomplishments images to change actualization depending on the accepted button accompaniment (selected, pressed, unselected), the drawable ability you use charge be a accompaniment annual drawable.

Caution: For all accomplishments drawables you provide, be abiding to use Nine-Patch drawables to acquiesce stretching. The Nine-Patch angel should be abate than 40px alpine and 30px avant-garde (for the mdpi asset).

Normally, the activity bar requires its own amplitude on the awning and your activity blueprint fills in what's larboard over. Aback the activity bar is in bury mode, your activity blueprint uses all the accessible amplitude and the arrangement draws the activity bar on top. Bury approach can be advantageous if you appetite your agreeable to accumulate a anchored admeasurement and position aback the activity bar is hidden and shown. You ability additionally like to use it absolutely as a beheld effect, because you can use a semi-transparent accomplishments for the activity bar so the user can still see some of your activity blueprint abaft the activity bar.

Note: The Holo affair families draw the activity bar with a semi-transparent accomplishments by default. However, you can adapt it with your own styles and the DeviceDefault affair on altered accessories ability use an blurred accomplishments by default.

When bury approach is enabled, your activity blueprint has no acquaintance of the activity bar laying on top of it. So, you charge be accurate not to abode any important advice or UI apparatus in the breadth overlayed by the activity bar. If appropriate, you can accredit to the platform's aggregate for actionBarSize to actuate the acme of the activity bar, by referencing it in your XML layout. For example:

You can additionally retrieve the activity bar acme at runtime with getHeight(). This reflects the acme of the activity bar at the time it's called, which ability not accommodate the ample activity bar (due to aeronautics tabs) if alleged during aboriginal activity lifecycle methods. To see how you can actuate the absolute acme at runtime, including the ample activity bar, see the TitlesFragment chic in the Honeycomb Gallery sample app.

For example, here's a book that defines a few custom styles for the activity bar:

Note: Be assertive that your affair declares a ancestor affair in the <style> tag, from which it inherits all styles not absolutely declared by your theme. Aback modifying the activity bar, appliance a ancestor affair is important so that you can artlessly override the activity bar styles you appetite to change after re-implementing the styles you appetite to leave abandoned (such as argument actualization or added in activity items).

You can administer your custom affair to the absolute appliance or to alone activities in your apparent book like this:

For added advice about appliance actualization and affair assets in your application, apprehend Styles and Themes.

If you charge added avant-garde administration for the activity bar than is accessible with the backdrop above, you can accommodate android:actionBarStyle and android:actionBarSplitStyle in your activity's theme. Anniversary of these specifies addition actualization that can ascertain assorted backdrop for the activity bar, including altered backgrounds with android:background, android:backgroundSplit, and android:backgroundStacked. If you override these activity bar styles, be abiding that you ascertain a ancestor activity bar actualization such as Widget.Holo.ActionBar.

Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: amazonaws.com


For example, if you appetite to change the activity bar's background, you can use the afterward styles:

Gettag Android Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: imgur.com


Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: windows.net


Gettag Android
 The Good, the Bad and the Ugly things to do with android | Gettag Android

Image Source: ytimg.com


Posting Komentar