Wednesday, October 14, 2015

Display OptionsMenu in FragmentActivity


I tried to display options menu in FragmentActivity, and run on Android 5, but the option menu doesn't shown!

My case is:
Min Sdk Version: API 16: Android 4.1 (Jelly Bean)
Target Sdk Version: API 23; Android 6.0 (Marshmallow)
Compile Sdk Version: API 23: Android 6.0 (Marshmallow)
Build Tools Version: 23.0.1

values/styles.xml
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>


menu/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/option1"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:title="Option 1"/>
    <item
        android:id="@+id/option2"
        android:orderInCategory="100"
        android:title="Option 2"/>
    <item
        android:id="@+id/option3"
        android:orderInCategory="100"
        android:title="Option 3"/>
</menu>

MainActivity.java
package com.blogspot.android_er.testfragmentactivity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.option1:
                Toast.makeText(MainActivity.this, "Option 1", Toast.LENGTH_LONG).show();
                return true;
            case R.id.option2:
                Toast.makeText(MainActivity.this, "Option 2", Toast.LENGTH_LONG).show();
                return true;
            case R.id.option3:
                Toast.makeText(MainActivity.this, "Option 3", Toast.LENGTH_LONG).show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


To fix it:

(approach 1)
Edit MainActivity.java, extends ActionBarActivity (deprecated) or AppCompatActivity, instead of FragmentActivity.

Refer to the document of FragmentActivityIf you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.

And, ActionBarActivity is deprecated, so you should use AppCompatActivity instead.

(approach 2)
Alternatively, edit values/styles.xml, set parent of <style> to "android:Theme.Holo.Light" or "android:Theme.Holo.Light.DarkActionBar", instead of "Theme.AppCompat.Light.DarkActionBar".

This video show how to and demo on Android Emulator running Android 4.1 Jelly Bean with API 16 and Android 6.0 Marshmallow with API 23.