Learn Android in 20 concepts: Concepts 13 and 14

 android development

Another week, two concepts. And these are the last two complex concepts to see. The rest will be a little easier. Today we will discuss how to manage the information on Android and something very important that was introduced in Honeycomb.’s ActionBar

13. Content Management (Content Provider)

A Content Provider given access to a central data repository. A provider is part of the Android application, which often provides its own graphical interface for working with data. However, the Content Providers are primarily intended for use by other applications accessing the provider by a customer.

To access data from a Content Provider will use a customer order ContentResolver . This object provides methods for basic “CRUD ( Create, retrieve, update, delete) of persistent data storage. It acts as an abstraction layer between the data repository and the external appearance of data as tables. . As always, you can use it, we need to assign permissions in the Manifest

This is an example of query :


A Content URI is a URI that identifies a data provider. Includes full symbolic name of the provider ( authority) and a name that points to a table ( path). When we call a method on the client, this data is one of the arguments.

From here, management is quite similar to what is done in database, referring to the concept of queries, cursors … Therefore, we will not exterdernos much at this point, but only discuss the basics and provide links to official code examples.

To request data from a vendor , we need to build a query . To see an example of a query simple, my recommendation is to visit the link .

Modifying Data will be similar and can see examples in the following link .

But suppose we do not want to create our own Content Provider, but we want to rescue some data from phone to use in our application. This is the case of Calendar or Contacts. Each will have its own suppliers so that we can have this information in your application, provided you have given permissions in the Manifest.

For the Calendar use the Calendar Provider and in the case of Contacts use the Contacts Provider .

With this, we can relate our applications using Content Providers, and use external data to our app events such as Calendar or Contacts.

14. The action bar ActionBar

When Google redesigned Android, including the Holo interface, among other things brought the ActionBar . This is a feature of the window identifies user location in the application and provides both Actions possible to be performed by the user, as navigation modes. could summarize their key roles in the following:

  • Provide a dedicated space for the identity of the application
  • Provide a dedicated space to identify where the user is within the application
  • Facilitate important actions in an accessible and predictive
  • Supporting

  • consistent navigation, especially in changes between different views


This functionality was added to Android HoneyComb 3.0, but what if we wanted to use it in an earlier version of Android? Just have to use the support library, for example Seller appcompat v7 support, on which we will base the rest of the article.

To add ActionBar , enough to extend our activity class ActionBarActivity (of the support library) and use one of the topics Theme.AppCompat as the subject of our activity. In case you do not need that compatibility, we can use the normal class Activity and use one of the topics Theme.Holo.

To remove the ActionBar , simply call the hide method:

 actionbar hide

can also add elements of action , so that the least important most important actions we will see them at all times in our ActionBar, and can hide in the overflow button (the famous three points).

 menu actionbar

Add a menu actions, we follow these steps:

Create menu in the folder res

  • Overwrite onCreateOptionsMenu method
  • To decide whether to always display only if there is space or menu item, use the property showAsAction
  • always have to set the title ( title) for each element as:
    • If there is insufficient space, the item will appear in the drop overflow element only with the title
    • screens optimized for the blind read that title
    • If we only see the icon, click to make a long finger, see text

  • menu1 actionbar  actionbar menu2 actionbar menu3

    Once you have added the items, just manage events when you click on each menu item

     actionbar menu4

    In addition, we can use the Split ActionBar , which is basically that if you lack space and a second bar where we will have more space is added. To this we must add the option uiOptions = “splitActionBarWhenNarrow” to each activity stated in our Manifest or directly to the entire application. But keep in mind that this option was added in the Android API 14, and if we take it in earlier versions, we add a as a child of each activity stating that value .

    split actionbar

    Another thing that we can add in the ActionBar is that icon on the left (the application icon) allows us to precisely navigate top screen. To do this, we call the method setDisplayHomeAsUpEnabled. Another option is declared in the Manifest for each activity what his father activity (available from Android 16).

     actionbar up

    More things we can see and add in some applications are search dialogs . To this effect a ActionView .

     ActionView actionbar

    And we manage to expand and collapse said ActionView :

     actionbar collapse

    We can also add a Provider Action such as is the case we can see typical button to share:

    actionbar provider

    Another typical element is tabs which need

    ActionBar.TabListener Implement Interface

  • ActionBar.Tab Instantiate for each tab and assign the listener with setTabListener the method. With setTitle and setIcon will customize the tab.
  • Add each tab the ActionBar through addTab method
  • actionbar tab

    Another thing we can add is a drop-down navigation . To do this:

    will create a SpinnerAdapter which provide us the list of items in the drop-down list

  • will implement ActionBar.. OnNavigationListener to define the behavior we need for each item
  • onCreate method of our activity, we will enable the dropdown navigation setNavigationMode calling (NAVIGATION_MODE_LIST)
  • will assign the callback list for setListNavigationCallbacks
  •  actionbar navigation

    As a last step, we customize the ActionBar to our style and our implementation, using styles. But I recommend you always follow the Android style guide .

    If you want to see some sample code, follow the lessons that Google provides us about it here .

    And here the concepts of today.

    See section Learn Android in 20 concepts

    Article Aprende 20 Android concepts: Concepts 13 and 14 was published in The Free Android (The reference Android Apps Blog, News. Free Games and Android smartphones)

    The Free Android

    Learn Android in 20 concepts: Concepts 13 and 14
    Source: english  
    April 6, 2014

    Next Random post