anon
anon

Reputation:

ActionBar icons won't show up for FragmentActivity despite setHasOptionsMenu(true)

In my android app, I am trying to add icons to the ActionBar in my FragmentActivity; however, it remains blank. Essentially I have a FragmentActivity with separate Fragments which are scrollable tabs like this:

enter image description here

I want to actionbar to have two icons from block.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=".SetupActivity" >



    <item android:id="@+id/action_overflow"
          android:title="@string/action_settings"
          android:orderInCategory="100"
          android:icon="@drawable/ic_action_overflow"
          app:showAsAction="always" />

    <item android:id="@+id/action_search"
          android:title="@string/search"
          android:orderInCategory="200"
          android:icon="@drawable/ic_action_search"
          app:showAsAction="always" />
</menu>

I want it to look something like this:

enter image description here

I am trying to follow this: onCreateOptionsMenu not being called on FragmentActivity when run on phone version

and this https://github.com/thecodepath/android_guides/wiki/ActionBar-Tabs-with-Fragments

Here is my BlockActivity class which extends FragmentActivity. I am unsure why the ActionBar is unable to be edited in spite of having the onCreateOptionsMenu method.

package com.spicycurryman.getdisciplined10.app;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;


// Find out about the default file template warning



/**
 * Created by Spicycurryman on 6/17/14.
 */
public  class BlockActivity extends FragmentActivity implements
        ActionBar.TabListener {



    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = {"Installed", "System", "Custom"};

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.block_apps);


        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.block, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_overflow) {
            return true;
        } else if(id == R.id.action_search) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }





    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }





    }

I have tried everything from inflating from the Fragments to doing it from the FragmentActivity and everything in between not sure what to do now...

Upvotes: 2

Views: 2613

Answers (2)

joao2fast4u
joao2fast4u

Reputation: 6892

Instead of extending FragmentActivity, extend ActionBarActivity.

ActionBarActivity has everything what FragmentActivity has and allows you to access easily to your ActionBar by calling getActionBar() or getSupportActionBar() for support version.

You can check more about it here.

Don't forget to set the activity theme to Theme.AppCompat or a similar theme, and it will work.

Upvotes: 8

raman rayat
raman rayat

Reputation: 414

use these theams for action bar might be it solve your problem :)

Theme.Holo for a "dark" theme Theme.Holo.Light for a "light" theme

Upvotes: -1

Related Questions