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;
}
Related
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();
// ...
}
I implemented search view for filtering items in listview. It's working fine. My problem is search view showing > ( next icon ) in action bar. What is this? how can i remove this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return true;
}
search_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/search"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
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()
I'm targetting API level 8+ and I'd like to have a full width
search view on top of the action bar, expanded by default and not collapsable.
Here's what I did:
I've an ActionBar activity where I set inside onCreate:
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
then onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
MenuItemCompat.expandActionView(searchItem);
return true;
}
Here my search.xml inside menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:title="#string/search_product"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="always">
</item>
</menu>
and my searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_product"
android:label="#string/searchable"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable>
This way, some extra space remains between back button and search widget.
Can you help me whit this?
P.S. Why search icon is not INSIDE the widget? If I set setIconifiedByDefault to
true and then click on the icon, the image remains inside the text space...
In my android app .. I am trying t put a search edittext with seARCH icon .. but I cant see the search icon in action bar rather the title is seeing in overflow. I am giving my code below
activity
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:title="Settings"/>
</menu>
MainActvity
public class MainActivity extends ActionBarActivity
{
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv1 = (WebView) findViewById(R.id.wView1);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl("http://www.example.com/songs/");
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
Use Search Manager n search view in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
and in your main.xml add this item for search
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
I think you must return `true` instead of
super.onCreateOptionsMenu(menu)
in `onCreateOptionsMenu(Menu menu)` method
Excuses. That was wrong.
I think this problem has something to do with the base class you are using. Your are subclassing ActionBarActivity instead of Activity. This means that you are using compatibility for previous versions. For this to work, you need to set your application's Theme to Theme.AppCompat (or a similar one), and also include the Support Library. Look here
I just realized how to fix the problem!
your menu xml file should have an starting tag like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto" >
you can replace
myApp
with any names you want, NOW the important part is when you define an action item:
<item
android:id="#+id/actionitem_switch_view"
android:title="#string/view_as"
android:showAsAction="always"
myApp:showAsAction="always"
android:icon="#drawable/ic_action_view_as_list"/>
You'd have two showAsAction tags,one for API<11 devices and one for the others,
Look closely that the two showAsAction tags are different,one is from Android namespace and the other's from the tag you defined in menu Starting point