action bar

honeycomb actionbar

Search for: honeycomb actionbar

honeycomb actionbar

Search for: honeycomb actionbar

a bit of coverage on action bar

2010 io presentation on the subject

navigation_mode_list

Search for: navigation_mode_list

ActionBar

Search Google for: ActionBar

Search Android Developers Group for: ActionBar

Search Android Beginers Group for: ActionBar

Search Google Code for: ActionBar

Search Android Issues Database for: ActionBar

affordance

Search Google for: affordance

Search Android Developers Group for: affordance

Search Android Beginers Group for: affordance

Search Google Code for: affordance

Search Android Issues Database for: affordance

wiki meaning for affordance

Meaning: An easily discoverable action that a user can perform

cognitive affordance

Afforance related articles on the topic of software engineering


public void workwithTabbedActionBar()
{
    ActionBar bar = this.getActionBar();
    bar.setTitle("Hellow there. how are you");
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    TabListener tl = new TabListener(this,this);
    
    Tab tab1 = bar.newTab();
    tab1.setText("Tab1");
    tab1.setTabListener(tl);
    bar.addTab(tab1);
    
    Tab tab2 = bar.newTab();
    tab2.setText("Tab2");
    tab2.setTabListener(tl);
    bar.addTab(tab2);
}

public class TabListener extends BaseTester
implements ActionBar.TabListener
{
    private static String tag = "tc>";
    public TabListener(Context ctx, IReportBack target)
    {
        super(ctx, target);
    }
    public void onTabReselected(Tab tab, FragmentTransaction ft) 
    {
        this.mReportTo.reportBack(tag, "ontab re selected:" + tab.getText());
    }
    public void onTabSelected(Tab tab, FragmentTransaction ft) 
    {
        this.mReportTo.reportBack(tag, "ontab selected:" + tab.getText());
    }
    public void onTabUnselected(Tab tab, FragmentTransaction ft) 
    {
        this.mReportTo.reportBack(tag, "ontab un selected:" + tab.getText());
    }
}

Don't pay attention to the base classes and extraneous variables. Read the code for concepts.


public void workwithListActionBar()
{
    ActionBar bar = this.getActionBar();
    bar.setTitle("Hellow there. how are you");
    //bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(
       new MyAdapter(this),new ListListener(this,this));
}

public class MyAdapter extends ArrayAdapter<String>
implements SpinnerAdapter
{
    public MyAdapter(Context ctx)
    {
        super(ctx,
          android.R.layout.simple_spinner_item, 
          new String[]{"one","two"});
        
        this.setDropDownViewResource(
          android.R.layout.simple_spinner_dropdown_item);
    }
    public View getDropDownView(
      int position, View convertView, ViewGroup parent)
    {
        return super.getDropDownView(
          position, convertView, parent);
    }
}

Read on using spinners


public class ListListener 
extends BaseTester
implements ActionBar.OnNavigationListener
{
    public ListListener(
    Context ctx, IReportBack target)
    {
        super(ctx, target);
    }
    public boolean onNavigationItemSelected(
    int itemPosition, long itemId) 
    {
        this.mReportTo.reportBack(
          "list listener","ItemPostion:" + itemPosition);
        return true;
    }
}

<item android:id="@+id/menu_list_nav"
    android:title="List Nav Activity" 
    android:icon="@drawable/creep001" 
    android:showAsAction="ifRoom"/>

MenuItem actionItem = menu.add("Action Button");        
actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);        
actionItem.setIcon(android.R.drawable.ic_menu_share);

Standard: NAVIGATION_MODE_STANDARD
Tab: NAVIGATION_MODE_TABS
List: NAVIGATION_MODE_LIST

These modes seem to be exclusive for a given action bar. Even some of the methods of the action bar class are applicable only if the action bar is in that mode.

In a way they are perhaps more like sub classes from a base class.


protected boolean onMenuItemSelected(MenuItem item)
{
  if (item.getItemId() == android.R.id.home)
  {
    this.reportBack(tag,"Home Pressed");
  }
  return true;
}

private int getNavMode()
{
  ActionBar bar = this.getActionBar();
  return bar.getNavigationMode();
}

NAVIGATION_MODE_DROPDOWN_LIST

Search Google for: NAVIGATION_MODE_DROPDOWN_LIST

Search Android Developers Group for: NAVIGATION_MODE_DROPDOWN_LIST

Search Android Beginers Group for: NAVIGATION_MODE_DROPDOWN_LIST

Search Google Code for: NAVIGATION_MODE_DROPDOWN_LIST

Search Android Issues Database for: NAVIGATION_MODE_DROPDOWN_LIST

Looks like the value for both

NAVIGATION_MODE_LIST
NAVIGATION_MODE_DROPDOWN_LIST

appears to be the same.

The only affect of setting one mode or vs the other, (betweent these two modes), is wether to show the titles or not. I suppose those can be done through display options setting.


public void workwithStandardActionBar()
{
    ActionBar bar = this.getActionBar();
    bar.setTitle("Standard Navigation Mode Title");
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    //test to see what happens if you were to attach tabs
    attachTabs(bar);
}
public void attachTabs(ActionBar bar)
{
    TabListener tl = new TabListener(this,this);
    
    Tab tab1 = bar.newTab();
    tab1.setText("Tab1");
    tab1.setTabListener(tl);
    bar.addTab(tab1);
    
    Tab tab2 = bar.newTab();
    tab2.setText("Tab2");
    tab2.setTabListener(tl);
    bar.addTab(tab2);
}

Attaching tabs in the mode ignores them.

I will upload the look and feel of various action bar modes in a short while


public void workwithDropdownListActionBar()
{
    ActionBar bar = this.getActionBar();
    bar.setTitle("Hellow there. how are you");
    //bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_DROPDOWN_LIST);
    
    bar.setDropdownNavigationMode(
      new MyAdapter(this),new ListListener(this,this));
}

Click on the image for a bigger image


Home icon invokes R.id.home menu item
Some menu items can be added as action icons
(look top right funny icons)
(icons from: androidicons.com: thank you)
An icontest icon shows only as text in the menu
clicking on title nothing happens
Setting tabs won't give an error but won't show tabs

Click on the image for a bigger image


Tabs show up
tab listener needs to be there
(if not a run time exception)
Home icon works as in standard

Click on the image for a bigger image


You need a list listener
behaves much like a tab
standard nav behavior is still there

Click on the image for a bigger image


very much like list nav
undocumented
(may go away)
title is not displayed
(this can be done through list nav)
(with title display option turned off)
(i suppose.)

Download Eclipse ADT Importable Zip File

Please note the link above is a zip file download.


Create 4 separate activities
(Each activity with a separate nav mode action bar)
provide a menu to navigate between 4 activities
show how the home icon behaves
show what parts of the action bar is shown in each mode
capture and log mouse actions as tabs are pressed
(these are logged right on the screen)
(no need to see the log.d)

Actionbar and Backport

Actionbar and Backport

There is some news that some or most of the fragment related code that is in 3.0 may be made available in older versions of Android as a set of libraries.

However from the link above Romain Guy indicates that the ActionBar API is more closely tied to other APIs of 3.0 such as the Menus and hence most likely will not be made available as part of this backport exercise.

well we are in ICS now. Actionbar is now part of phones too


android:showAsAction="ifRoom|withText"

ActionBar actionBar = getActionBar();
if (actionBar!=null)
   actionBar.hide();

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

Read this to undestand themes

If you respond to the application icon by returning to the home activity, you should include the FLAG_ACTIVITY_CLEAR_TOP flag in the Intent. With this flag, if the activity you're starting already exists in the current task, then all activities on top of it are destroyed and it is brought to the front. You should favor this approach, because going "home" is an action that's equivalent to "going back" and you should usually not create a new instance of the home activity. Otherwise, you might end up with a long stack of activities in the current task


boolean onOptionsItemSelected(MenuItem item) 
{    
    switch (item.getItemId()) 
    {        
        case android.R.id.home:            
            // app icon in Action Bar clicked; go home            
            Intent intent = new Intent(this, HomeActivity.class);            
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);            
            startActivity(intent);            
            return true;        
        default:            
        return super.onOptionsItemSelected(item);    
    }
}

you can use the home to go up as well


actionBar.setDisplayHomeAsUpEnabled(true)

think hard and use the clear top flag

You can insert something like a searchview in the action bar

searchview

Read this to understand search better


add a menu item with search view
create a search results activity that can respond
create an xml file: xml/searchable.xml
define the search results activity in the manifest file
in onCreateOptions hook up the searchview 
  to the search results activity.

<item android:id="@+id/menu_search"
    android:title="Search"
    android:showAsAction="ifRoom"  
    android:actionViewClass="android.widget.SearchView"            
    />

public class SearchResultsActivity 
{
    private static String tag=
      "Search Results Activity";
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        final Intent queryIntent = getIntent();
        final String queryAction = queryIntent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)) 
        {
           Log.d(tag,"onCreate: intent for search");
           this.doSearchQuery(queryIntent);
        }
        else {
           Log.d(tag,"onCreate: intent NOT for search");
        }
    }
    @Override
    public void onNewIntent(final Intent newIntent) 
    {
        super.onNewIntent(newIntent);
        Log.d(tag,"new intent calling me");
        
        // get and process search query here
        final Intent queryIntent = getIntent();
        final String queryAction = queryIntent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)) 
        {
           this.doSearchQuery(queryIntent);
           Log.d(tag,"new intent for search");
        }
        else {
           Log.d(tag,"new intent NOT for search");
        }
    }
    private void doSearchQuery(final Intent queryIntent) 
    {
        final String queryString = 
           queryIntent.getStringExtra(SearchManager.QUERY);
        Log.d(tag, queryString);
    }
}//eof-class

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/slabel"
    android:hint="@string/shint" 
/>
<!-- /res/xml/searchable.xml -->

<activity android:name=".SearchResultsActivity"
   android:label="Search Results">
     <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
      </intent-filter>
     <meta-data android:name="android.app.searchable"
                        android:resource="@xml/searchable"/>
</activity>

private void setupSearchView(Menu menu)
{
    SearchView searchView = 
        (SearchView) menu.findItem(R.id.menu_search).getActionView();
    if (searchView == null)
    {
        this.reportBack(tag, "Failed to get search view");
        return;
    }
    //setup searchview
    SearchManager searchManager = 
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName cn = 
        new ComponentName(this,SearchResultsActivity.class);
    SearchableInfo info = 
        searchManager.getSearchableInfo(cn);
    if (info == null)
    {
        this.reportBack(tag, "Failed to get search info");
        return;
    }

    searchView.setSearchableInfo(info);

    // Do not iconify the widget; expand it by default
    searchView.setIconifiedByDefault(false); 
}

How to enable actionbar in ICS

Search for: How to enable actionbar in ICS

See this document on actionbar from google


<manifest ... >
    <uses-sdk android:minSdkVersion="4"
              android:targetSdkVersion="11" />
    ...
</manifest>

If it is 11 or higher you don't need to do that. actionbar is automatically enabled. A target version that is recent than the minsdkversion is a hint to android to gracefully fallback where possible but optimize it if later.