Lesson 112. Android 3. ActionBar. Dynamic placement of elements

Lesson 112. Android 3. ActionBar. Dynamic placement of elements


In this lesson:

– programmatically place elements in ActionBar
– we use elements from fragments

Recently, there was a question on the forum about how to modify ActionBar content while running the program. The topic is really interesting and I missed it. In this lesson we will deal with it.

Let’s try three ways to work with the elements:

1) Add / Remove MenuItem to the Menu object

2) Show / hide group in Menu

3) Elements that relate to fragments

With the first two points, it is clear that we discussed these mechanisms in Lesson 14.

More interesting is the third point. The snippet can implement the onCreateOptionsMenu method and thus create its own ActionBar elements. Once the snippet is added to the screen, these items will be added to ActionBar. And when the snippet is removed, the items will disappear.

Let’s create an application that implements these three options.

Let’s create a project:

Project name: P1121_DynamicActionBar
Build Target: Android 4.1
Application name: DynamicActionBar
Package name: ru.startandroid.develop.p1121dynamicactionbar
Create Activity: MainActivity

Add rows to strings.xml:

Добавить/удалить
Показать/скрыть
Фрагмент
Fragment 1
Fragment 2
Item 1
Item 2
Item 31
Item 32

Let’s create two fragments. Let’s start with the layout files.

fragment1.xml:



	
	

fragment2.xml:



	
	

Files with menu items (they are also ActionBar items):

res / menu / fragment1.xml:



	
	

res / menu / fragment2.xml:



	
	

One item for each snippet. These items will appear in ActionBar when the snippet is displayed.

Classes.

Fragment1.java:

package ru.startandroid.develop.p1121dynamicactionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

  public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
  }

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment1, null);
  }

  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.fragment1, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

}

Fragment2.java:

package ru.startandroid.develop.p1121dynamicactionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
  
  public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
  }
  
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment2, null);
  }
  
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.fragment2, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

}

IN onCreate with setHasOptionsMenu, we enable the snippet output mode in ActionBar.

IN onCreateView create View in onCreateOptionsMenu – menu. As usual.

Fragments are ready.

Now let’s do Activity. rewrite res / layout / main.xml:



	
	
	
	
	
	
	

Two checkboxes and a button. Checkboxes are responsible for handling elements through the first and second methods. The button will show two fragments in the container cont.

res / menu / main.xml:



	
		
		
	

We create a group and an element in it. We will hide and show this group.

MainActivity.java:

package ru.startandroid.develop.p1121dynamicactionbar;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends Activity {

  final int MENU_ID = 1;

  CheckBox chbAddDel;
  CheckBox chbVisible;

  Fragment frag1;
  Fragment frag2;
  Fragment frag;

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

    chbAddDel = (CheckBox) findViewById(R.id.chbAddDel);
    chbVisible = (CheckBox) findViewById(R.id.chbVisible);

    frag = frag1 = new Fragment1();
    frag2 = new Fragment2();

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    menu.setGroupVisible(R.id.groupVsbl, chbVisible.isChecked());
    if (chbAddDel.isChecked()) {
      menu.add(0, MENU_ID, 0, R.string.menu_item1)
          .setIcon(android.R.drawable.ic_delete)
          .setShowAsAction(
              MenuItem.SHOW_AS_ACTION_ALWAYS
                  | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    } else {
      menu.removeItem(MENU_ID);
    }
    return true;
  }

  public void onClick(View view) {
    switch (view.getId()) {
    case R.id.chbAddDel:
    case R.id.chbVisible:
      invalidateOptionsMenu();
      break;
    case R.id.btnFrag:
      frag = (frag == frag1) ? frag2 : frag1;
      getFragmentManager().beginTransaction().replace(R.id.cont, frag)
          .commit();
      break;
    default:
      break;
    }

  }
}

IN onCreateOptionsMenu we set groupVsbl visibility depending on the chbVisible checkbox value.

Depending on the value of the chbAddDel checkbox, we create or delete an item.

IN onClick for checkboxes, call the invalidateOptionsMenu method – menu bar / ActionBar. And after clicking the queue, we display Fragment1 or Fragment2.

We all save and launch the application.

we press the pebble Add / Remove. An item appears. We have added MenuItem to the Menu.

we press the pebble Show / hide. An item appears. We showed a menu group with one item.

Accordingly, removing the checkboxes takes away the items.

press the button fragment. An element of one or the other fragment appears.

Alternatively, you can play with visibility not the entire group, but a specific element – the setVisible method.

If the ActionBar clicks on the overflow button, the onPrepareOptionsMenu method for the Activity and the snippet currently displayed will be called.

In the next lesson:

– we work with ActionMode




Discuss in the forum [2 replies]

Leave a Comment