4

This can be considered as a extension of this Question

I have added following to the code to get a searchView Widget

res/menu/main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="always"
          android:actionViewClass="android.support.v7.widget.SearchView" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

Mainactivity.java

public class MainActivity extends Activity {

    @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.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        System.out.println("TESTING:    "+searchView);



        return super.onCreateOptionsMenu(menu);
    }

---
---

}

The app shows up fine but on touching the search button nothing shows up.

Screen shot

Have set the Sdk versions as follows:(4.0.3 -4.2.2)

android:minSdkVersion="15"
android:targetSdkVersion="17"
Community
  • 1
  • 1
Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58

2 Answers2

16

Change you menu to,

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

    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:actionViewClass="android.support.v7.widget.SearchView"
          yourapp:showAsAction="always" />

</menu>
yajnesh
  • 2,089
  • 1
  • 23
  • 36
  • 1
    The alteration here is that the xmlns for yourapp has been added, and the namespace is changed for the last two properties on the item (in case anyone else has difficulty seeing it). http://stackoverflow.com/questions/18099221/action-bar-not-showing-action-view-icons suggests having both. – hajamie Aug 17 '14 at 00:35
0

You're missing "collapseActionView" in your menu:

app:showAsAction="ifRoom|collapseActionView"
Luis
  • 2,833
  • 3
  • 27
  • 41