1

I'm trying to display the Search Icon in the top right of my Actionbar. See Code below for the PublicVideos.java that extends an ActionBarActivity

The getoverflow manages to display the overflow menu, but it does not display the search icon. I have added the different sizes search icons in their respective drawables folders.

package faith.faithconnect;

import java.lang.reflect.Field;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;

public class PublicVideosActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_videos);

        centreLogo();
        getOverflowMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
            getMenuInflater().inflate(R.menu.faithmenu, menu);
            MenuItem searchItem = menu.findItem(R.id.action_search);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

            return super.onCreateOptionsMenu(menu);
    }

    private void getOverflowMenu() {
        // TODO Auto-generated method stub
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void centreLogo() {
        // TODO Auto-generated method stub
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#F7CE04")));
        // getOverflowMenu();
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setCustomView(R.layout.public_videos_view);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent backhome = new Intent(this, MainActivity.class);

        startActivity(backhome);
    }

    // public void mingleSwipe(View view)
    // {
    // Intent mingleintent = new Intent(getApplicationContext(),
    // MingleActivity.class );
    // startActivity(mingleintent);
    //
    // }
    public void ratingSelected(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Try Google play
        intent.setData(Uri
                .parse("market://details?id=com.cubeactive.qnotelistfree"));
        startActivity(intent);
    }

    public void shareSelected(View view) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "body here");
        startActivity(Intent.createChooser(shareIntent, "Share Via"));

    }

}

The faithmenu.xml below

<?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/action_search"
              android:title="search"
              android:icon="@drawable/ic_action_search"
              android:showAsAction="ifRoom|collapseActionView"
              android:actionViewClass="android.support.v7.widget.SearchView" ></item>

    </menu>
Quanturium
  • 5,698
  • 2
  • 30
  • 36
George
  • 2,865
  • 6
  • 35
  • 59
  • possible duplicate of [How To show icons in Overflow menu in ActionBar](http://stackoverflow.com/questions/18374183/how-to-show-icons-in-overflow-menu-in-actionbar) – tachyonflux Feb 16 '15 at 20:32
  • @ karaokyo this not a duplicate of How To show in Overflow menu. I have an overflow menu that already works. The problem is that the other menu items do not seem to want to display on the action bar. – George Feb 16 '15 at 20:36
  • You can use `always` replace `ifRoom|collapseActionView` to force item always display. – Xcihnegn Feb 17 '15 at 08:27
  • @GeorgeMujuru you got some advance? Don't forget check one answer as correct. – Adrian Cid Almaguer Feb 20 '15 at 18:49

2 Answers2

2

You should use name space app for showAsAction, so it should be:

<?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/action_search"
          android:title="search"
          android:icon="@drawable/ic_action_search"
          app:showAsAction="ifRoom|collapseActionView"
          android:actionViewClass="android.support.v7.widget.SearchView" >     </item>

</menu>
Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
0

When your item have android:showAsAction="ifRoom" If there's not enough room for the item in the action bar, it will appear in the action overflow.

You can also use "always" (android:showAsAction="always") to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way. Doing so can create layout problems on devices with a narrow screen. It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • @George Mujuru you got some advance? – Adrian Cid Almaguer Feb 19 '15 at 04:03
  • @ In the faithmenu.xml i was supposed to use app:showAsAction="ifRoom|collapseActionView" instead of android:showAsAction="ifRoom|collapseActionView" Thanks for the advice. i only wanted to always display two items. – George Feb 21 '15 at 12:21