4

I have in the following Activity this Toolbar:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_select_currency"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

Also the menu is declared in:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/search"
      android:title="@string/search_currency"
      android:icon="@drawable/ic_action_search"
      android:showAsAction="collapseActionView|ifRoom"
      android:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

In the activity I call:

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

    return true;
}

private void initActionBar() {
    // Initialize the Toolbar( this is the new ActionBar in Lollipop)
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_select_currency);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(getString(R.string.select_currency));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Now my issue is that I do not get the SearchView like in normal ActionBar, what I get is a normal menu. How can I get a normal SearchView ?

Mythul
  • 1,807
  • 7
  • 34
  • 53
  • possible duplicate of [Cannot get searchview in actionbar to work](http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work) – Maveňツ Dec 01 '14 at 11:27

1 Answers1

6

Use your app namespace:

xmlns:myapp="http://schemas.android.com/apk/res-auto"

myapp:showAsAction="always"

Explanation: Since you are using a support library for old android versions and it is not included into the sdk, the android namespace doesn't contains the showAsAction attribute, which is rather contained into the support library you linked to your project, so to reach the attribute you must use your app namespace

MineConsulting SRL
  • 2,340
  • 2
  • 17
  • 32
  • 1
    Thank sir. This worked for me. It would be really helpful if you could explain to me what that custom namespace does and why didnt it work with 'android' ? – Mythul Dec 01 '14 at 16:18