Victor Oliveira
Victor Oliveira

Reputation: 3713

Create android action bar - How to?

I Know the question is pretty lame, but I'm really having difficulties here. I'm new to android and I'm trying to follow google's tutorial to create one action bar:

  1. http://developer.android.com/design/patterns/actionbar.html
  2. http://developer.android.com/training/basics/actionbar/adding-buttons.html

But I've been stuck here for hours.

My app has one login interface which will trigger the home screen and in this home screen I will have my action bar. I've tried a lot of combinations to make this action bar come up but nothing went useful.

One of the things I've tried, simple add this xml source to see my layout:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <!-- Search, should appear as action button -->
        <item android:id="@+id/action_search"
              android:icon="@drawable/ic_action_search"
              android:title="@string/action_search"
              android:showAsAction="ifRoom" />
        <!-- Settings, should always be in the overflow -->
        <item android:id="@+id/action_settings"
              android:title="@string/action_settings"
              android:showAsAction="never" />
    </menu>

When I do this and come to the graphical layout to check it, it gives me this message:

    "<menu>" does not set the required layout_width attribute:
     (1) Set to "wrap_content"
     (2) Set to "match_parent"

        "<menu>" does not set the required layout_height attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_search" does not set the required layout_width attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_search" does not set the required layout_height attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_settings" does not set the required layout_width attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_settings" does not set the required layout_height attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
        You must supply a layout_width attribute.
        You must supply a layout_height attribute.

If I set the layout content, the next error message is:

Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup

It can't render the layout somehow...I've tried also to set one relative or linear layout outside menu, like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
    </menu>

</RelativeLayout>

but this way I receive:

Unexpected namespace prefix “xmlns” found for tag Menu

Also; I've tried to remove my .xml from res/layout folder and left him alone in another res/menu folder...only with the .xml provided by google; This way I don't get syntax errors, but I can't work anymore with the graphical layout and one way or another when the home screen intent is threw the app crashes.

so please, What exactly I'm missing here?

Upvotes: 0

Views: 1766

Answers (2)

Victor Oliveira
Victor Oliveira

Reputation: 3713

This answer is based on samleighton87 answers, I made a suggestion for him to edit his post but his not so active at stackoverflow; So based on his answer I'm giving another answer with some corrections to clarify my point of view when I had the problem:

Check out this tutorial: https://www.youtube.com/watch?v=iA2Efmo2PCA

Right in the beginning you will see what needs to be done and what you are missing.

You need to create res/menu folder and right after the .xml file with the code gave by google at main_activity_actions.xml; After this make sure to have inside your activity .java file this method:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  // Inflate the menu items for use in the action bar
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.main_activity_actions, menu);
  return super.onCreateOptionsMenu(menu);
}

The main class inside this .java file will extend "Activity". You will realize now that what's automatically generated is the Reference inside your R.java, he (R.java) will be able to find your res/menu folder allowing you to point to it with inflater.inflate(R.menu.main_activity_actions, menu);. Keep in mind: this .xml file is not part of your layout xml files. It is a separated .xml file in the menu folder and you also won't be able to use the graphical layout to handle it.

Then to respond to individual items in the menu xml use this code:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
  // Handle presses on the action bar items
  switch (item.getItemId()) 
  {
      case R.id.action_search:
          openSearch();
          return true;
      case R.id.action_settings:
          openSettings();
          return true;
      default:
          return super.onOptionsItemSelected(item);
  }
}

If you want to support Android 2.1 and above use this advice check - http://developer.android.com/training/basics/actionbar/setting-up.html#ApiLevel7

Upvotes: 1

samleighton87
samleighton87

Reputation: 579

The menu xml is not part of your layout xml. It is a separate xml file in the menu folder and is added at runtime using this code:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  // Inflate the menu items for use in the action bar
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.main_activity_actions, menu);
  return super.onCreateOptionsMenu(menu);
}

Then to respond to the individual items in the menu xml use this code:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
  // Handle presses on the action bar items
  switch (item.getItemId()) 
  {
      case R.id.action_search:
          openSearch();
          return true;
      case R.id.action_settings:
          openSettings();
          return true;
      default:
          return super.onOptionsItemSelected(item);
  }
}

If you want to support Android 2.1 and above use this advice - http://developer.android.com/training/basics/actionbar/setting-up.html#ApiLevel7

Upvotes: 1

Related Questions