Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView Widget not showing up in Action Bar

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"
like image 633
Sangeet Menon Avatar asked Dec 05 '22 09:12

Sangeet Menon


1 Answers

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>
like image 105
yajnesh Avatar answered Jan 15 '23 07:01

yajnesh