Daniel Lodge
Daniel Lodge

Reputation: 51

How to create a title bar / settings menu with FragmentActivity?

I created an ActionBarActivity test app, which shows the title bar and settings menu ok. I changed extends ActionBarActivity to extends FragmentActivity and the program runs, but the title bar / menu isn't displayed.

I am using swipe tabs with ViewPager in my real app, so extending ActionBarActivity isn't an option.

The question was asked similarly in How to Create option menu in FragmentActivity? - but the answer isn't apparent.

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.            
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

menu_main.xml

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    >
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

Upvotes: 3

Views: 1688

Answers (2)

Daniel Lodge
Daniel Lodge

Reputation: 51

The solution is to simply use ActionBarActivity rather than FragmentActivity.

I confirmed a ViewPager (containing fragments) inside a fragment works fine - contrary to most info on the form.

Upvotes: 2

aeskreis
aeskreis

Reputation: 1973

The issue is that FragmentActivity doesn't have an ActionBar. The purpose of ActionBarActivity (which is a subclass of FragmentActivity) is to supply the necessary functionality to display an ActionBar in your content. If you do not wish to use ActionBarActivity (or the new and improved AppCompatActivity), use the new class Toolbar, which allows you to embed an ActionBar-like View directly into your layout.

Upvotes: 4

Related Questions