i am beginner in android studio and sorry my English is not good.
i have a searchView in secondActivity.
i have done things like below :
1.implemented the v7 AppCompat support library in gradle like this compile 'com.android.support:appcompat-v7:25.3.1'
2.implemented android.support.v7.widget.SearchView.OnQueryTextListenerinterface for current activity like this public class ViewActivity extends AppCompatActivity implements SearchView.OnQueryTextListener
but the searchView is still null.
below are my source code:
Menu.xml
<?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_search1"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android:support.v7.widget.SearchView"
android:title="#string/search_hint" />
</menu>
in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu){
getMenuInflater().inflate(R.menu.menu_search, menu);
final MenuItem item = menu.findItem(R.id.action_search1);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setQueryHint("Search Name");
searchView.setIconified(false);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
MenuItemCompat.getActionView(MenuItem item) is deprecated in API level 26.1.0.
Now the recommended way is calling getActionView() directly on the menu item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
// ...
}
Related
I haven't change any code last week, but this problem suddenly appeared yesterday.
Here is method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
});
return super.onCreateOptionsMenu(menu);
}
When I try to open activity that contains SearchView, app closes. Logcat says: attempt to invoke virtual method searchView.setOnQueryTextListener() on a null object reference.
search_menu:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#mipmap/ic_search"
android:title="#string/search"
android:inputType="text"
android:focusableInTouchMode="false"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
What is wrong with searchview? I've tried all methods offered in similar questions, but they do not work.
Activity:
public class CategoryActivity extends Activity {
ArrayList<> mArrayList;
CustomAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mArrayList = new ArrayList<>();
Objects.requireNonNull(getActionBar()).setBackgroundDrawable(new ColorDrawable(Color.rgb(8,16,38)));
Objects.requireNonNull(getActionBar()).setDisplayHomeAsUpEnabled(true);
You declare in menu xml the wrong type of SearchView:
Change
app:actionViewClass="android.widget.SearchView"
to
app:actionViewClass="android.support.v7.widget.SearchView"
also change:
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
to:
SearchView searchView = (SearchView) item.getActionView();
the signature of getActionView() you use is deprecated.
Edit:
In your activity's class, check the imports on top, there must be a line:
import android.support.v7.widget.SearchView;
I'm trying to fetch my SearchView from the Toolbar within a Fragment.
Im infalting a menu item in the onCreateOptionsMenu.
Problem : searchItem.getActionView() returns a "View" and not a "SearchView". See code below
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.menu_search, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = searchItem.getActionView(); // This one is red - "returns View, should return Searchview"
}
XML for menu item (menu_search.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/search"
android:id="#+id/search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Try this :
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();
I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and java file.
menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_action_sort"
android:title="#string/sort"
app:showAsAction="ifRoom"/>
</menu>
java file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
now i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?
Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.
I used MenuItemCompat to get actionview, that might do the trick.
This is my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.yourapp.youractivity">
<item
android:id="#+id/search"
android:title="#string/app_name"
android:icon="#drawable/nav_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
This is how i create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shop_list, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//Using MenuItemCompat here, that can do the trick
searchView =
(SearchView)MenuItemCompat.
getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
//etc...
//etc...
return true;
}
In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).
Try this inside the Class and extend ActionBarActivity in your Class
(Example: - public class Home extends ActionBarActivity)
#Override
public void onBackPressed() {
super.onBackPressed()
}
For collapsing the SearchView: Try this,
menu.collapseActionView();
(or)
searchView = menuItem.getActionView().
(or)
searchView.onActionViewCollapsed()
In my app I have a SearchView that uses the support.v7 library because of compatibility.
The problem is that, in a device with API 11 or higher, when I open the searchview i get the support api style and only after I search for something the style changes to my application style.
I want the theme in the newest APIs to be always like the second image. Already tried other themes but the result is always the same.
Style when the searchview expands:image link.
Style after a search:image link
options_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:malfriends="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"
malfriends:actionViewClass="android.support.v7.widget.SearchView"
malfriends:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="#+id/logoff"
android:title="#string/logout"
malfriends:showAsAction="never"/>
</menu>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name" android:searchSuggestAuthority=
"com.dandrex.malfriends.controller.provider.SearchFriendsProvider"
android:searchSuggestSelection=" ?" />
My activity class
import android.support.v7.widget.SearchView;
...
public class FeedTabActivity extends ActionBarActivity implements
ActionBar.TabListener
onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(new ComponentName(
FeedTabActivity.this, FeedTabActivity.class)));
searchView.setIconifiedByDefault(false);
} else {
MenuItem itemCompat = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(itemCompat);
}
return true;
}
In my app, I'm using a SearchView in the actionbar that it's always expanded like this:
My main.xml is this:
<item
android:id="#+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always"
android:title="Search" />
MainActivity.java:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchViewItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setIconifiedByDefault(false);
return true;
}
I would like to remove the search icon and expand the editText.
Regards