Lesson 108. Android 3. ActionBar. Navigation - tabs and drops

Lesson 108. Android 3. ActionBar. Navigation – tabs and drops


In this lesson:

– we use navigation in ActionBar

In the last lesson, we looked at adding ActionBar items. It is convenient for the user to use these elements to perform any operations in the application. For example, in a mailer, this might be deleting emails, forwarding, replying, etc.

Now let’s look at how to add items to navigate your application. There are two types of navigation items: tabs and dropdowns. Let’s create a simple application and implement it first one way, then the second way.

Let’s create a project:

Project name: P1081_ActionBarNavigation
Build Target: Android 4.1
Application name: ActionBarItems
Package name: en.startandroid.develop.p1081actionbarnavigation
Create Activity: MainActivity

We do not add any rows, we do not touch the basic layout.

You

Kodyma MainActivity.java:

package ru.startandroid.develop.p1081actionbarnavigation;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements ActionBar.TabListener {

  final String LOG_TAG = "myLogs";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar bar = getActionBar();

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = bar.newTab();
    tab.setText("tab1");
    tab.setTabListener(this);
    bar.addTab(tab);

    tab = bar.newTab();
    tab.setText("tab2");
    tab.setTabListener(this);
    bar.addTab(tab);

  }

  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) {
    Log.d(LOG_TAG, "reselected tab: " + tab.getText());
  }

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Log.d(LOG_TAG, "selected tab: " + tab.getText());
  }

  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    Log.d(LOG_TAG, "unselected tab: " + tab.getText());
  }
}

We see what we need to turn you on.

IN onCreate we gain access to ActionBar and set the navigation mode to NAVIGATION_MODE_TABS. Next up is the tabs. Everything is easy here – we create, write text, assign a handler, add to ActionBar.

We’ve done MainActivity for the tabs, it implements the ActionBar.TabListener interface methods:

onTabReselected – already selected tab

onTabSelected – Tab selected

onTabUnselected – Tab is not already selected

In these methods we put a log entry and see how it all works there.

We all save and launch the application.

we see you

Now the first tab is selected and the log immediately informs you about it:

selected tab: tab1

Click on the other tab,

look at the logs:

unselected tab: tab1
selected tab: tab2

The first tab is “disassembled” and the second is elected.

Once again, we click on the other tab:

reselected tab: tab2

Second tab reelected. Everything is logical and understandable.

In order not to overload the lesson, I did not undertake any navigation operations in the application. And, in general, we mean that in the handler methods, we code fragment operations. We are even kindly provided with the facility FragmentTransaction for these purposes. However, help warns that in these methods we do not need to call the method ourselves commit, And we cannot add the transaction to BackStack.

The drop-down list

Now let’s see how navigation with the drop-down list works.

rewrite MainActivity.java:

package ru.startandroid.develop.p1081actionbarnavigation;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity implements
    ActionBar.OnNavigationListener {

  String[] data = new String[] { "one", "two", "three" };

  final String LOG_TAG = "myLogs";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    ArrayAdapter adapter = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, data);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bar.setListNavigationCallbacks(adapter, this);
  }

  @Override
  public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    Log.d(LOG_TAG, "selected: position = " + itemPosition + ", id = "
        + itemId + ", " + data[itemPosition]);
    return false;
  }
}

IN onCreate we get ActionBar and turn on NAVIGATION_MODE_LIST navigation mode. For the list, you need to create an adapter that implements SpinnerAdapter. Be it an ArrayAdapter. When creating, we use an array of three rows. Next we call the setListNavigationCallbacks method, in which we pass the adapter and the handler.

We again have Activity handler, implements the method onNavigationItemSelected of the ActionBar.OnNavigationListener interface. This method gives us the position and id of the element selected from the list. Output this infa and the corresponding element of the array.

In the manifesto, I specified the attribute for the MainActivity theme: android: theme = “@ android: style / Theme.Holo.Light”. Otherwise there was a dark text on a dark background.

We save and run everything.

The first element is immediately selected. In logs we see:

selected: position = 0, id = 0, ones

Select any other item from the list

In the logs:

selected: position = 2, id = 2, three

Again, everything is logical and understandable.

For both types of navigation, we can always get the currently selected element using the getSelectedNavigationIndex method. And for tab navigation, there is also a getSelectedTab method that returns the currently selected tab.

Of course, with navigation, we can also add to the ActionBar the usual elements that were discussed in the last lesson.

In this case, if space is not enough, ActionBar will split all its elements into two strips.

Starting with the fourth version of Android, you can use the attribute uiOptions for Activity or Application in the manifest. Assigning a value to it splitActionBarWhenNarrow, Then the result in the absence of space will be as follows:

The items went down. However, this somehow stops working withText in showAsAction.

In the next lesson:

– we work with ListFragment




Discuss in the forum [69 replies]

Leave a Comment