Emanuel
Emanuel

Reputation: 1861

Double ActionBar appears?

this is my first app for android, I've been an iPhone dev for 3 years, it's been a change of mindset, and I still find some things odd. First I don't know if this iPhone background might be causing some troubles, but here's what I'm trying to do:

I want to implement an ActionBar with two options working like a TabBar from iOS:

ActionBar

What I want is that when the user selects an action, some Activity will be presented to the user.

Here's what I'm doing so far (which isn't that much):

myapp.java only creates the ActionBar elements and loads the activity_first:

package com.example.myApp;

import com.actionbarsherlock.ActionBarSherlock;
import com.actionbarsherlock.view.MenuItem;

import android.os.Bundle;

import com.actionbarsherlock.ActionBarSherlock.OnCreateOptionsMenuListener;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

public class myApp extends SherlockActivity implements OnCreateOptionsMenuListener {
    ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("myApp");
        mSherlock.setContentView(R.layout.activity_first);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("First")
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        menu.add("Second")
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        return true;
    }
}

The activities have nothing so far, apart from the generated stub:

import android.app.Activity;
import android.os.Bundle;

public class First extends Activity {

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


    }
}

So I have 1 Question and 1 problem:

  1. Question: Is this the correct use of an ActivityBar? I mean, should it be used to switch activities?
  2. Problem: As you can see in the OnCreate method of myApp class, I'm loading the activity_first, It does load the activity, however it loads the ActionBar Twice, like so:

enter image description here

I don't get why is it being loaded twice. If I remove the line: mSherlock.setContentView(R.layout.activity_first); it loads the Bar once, obviously I need to load the activity...

Also I've assigned the NoActionBar theme to the activity_first in the graphical editor of the XML (activity_first.xml), but it doesn't work. What Can I do to load it just once.

Thank you for your valuable time.

Upvotes: 1

Views: 1567

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

Is this the correct use of an ActivityBar? I mean, should it be used to switch activities?

You can think of "First" and "Second" as being the equivalent of toolbar buttons in a desktop app. You are welcome to have those action bar items start up activities if you wish.

As you can see in the OnCreate method of myApp class, I'm loading the activity_first, It does load the activity, however it loads the ActionBar Twice, like so:

Delete this line:

ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);

and change mSherlock.setContentView(R.layout.activity_first); to just setContentView(R.layout.activity_first);. I believe that will clear up your problems.

Upvotes: 3

Related Questions