How to add search icon in action bar? - android

I am using navigation drawer,and i have action bar in all fragments,I am trying to add search icon in action bar but its not appearing,app is not crashing but even icon is not visible in my action bar,following is my code for that can any one help
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:icon="#drawable/searchs"
android:title="#string/action_settings"/>
</menu>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

i think it is because of:
android:showAsAction="never"
instead of that use:
android:showAsAction="ifRoom"

ShowAsAction must be always or ifRoom.
always makes sure it is always present.
ifRoom pushes the menuitem into overflow menu if space is not available.
if your intention is to attach search functionality to your activity, you should consider using SearchView widget instead of using and icon and handling click on it.

#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_contact, menu);
MainActivityAfterLogin.menu = menu;
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
return false;
}
});
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${relativePackage}.${activityClass}" >
<item
android:id="#+id/menu_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
add this in manifest
<activity
android:name="**mainactivity**"
android:label="#string/title_activity_invited_person_list" >
<meta-data
android:name="android.app.default_searchable"
android:value="**result activity**" />
</activity>
<activity
android:name="com.w3nuts.rsvp.main.SearchResultActivity"
android:label="#string/title_activity_search_result" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>

Related

android want to add searchview in actionbar but app crash

i am working on action bar . i want to add searchview option on the action bar but app crash here.
this is crash
this is my code of option menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search"
android:title="Search"
android:icon="#drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>
this is the code of searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/enter"
android:includeInGlobalSearch="false"
android:label="#string/search"
android:searchSettingsDescription="#string/search_global_description" />
and this is my activity code
public class Thrd extends ActionBarActivity {
Menu m;
final Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thrd);
getSupportActionBar().setTitle("3rd page");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#20a780"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.options_menu, menu );
// Add SearchWidget.
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem( R.id.search ).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName() ) );
return super.onCreateOptionsMenu( menu );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
}
please help me to solve my problem.thanks advance
This was how I implemented my search handling.
In the XML folder under layout, add a searchable.xml file and put this code like so:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search Trends" />
Then in your ANDROID manifest file, add
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.tobisoft.trendify.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
//This is the meta to add for your activity
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
Also you would need to make an activity for the search results like so:
<activity
android:name=".SearchResultsActivity"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme">
<!-- to identify this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
now in the menu folder, with a file name main.xml (or anything you are using)
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="100"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
You can get the icon yourself by a simple google search. You would also need to add the AppCompat Library to your project, something you can also do from a simple google search. In the SearchResultsActivity, add this:
public class SearchResultsActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}
}
The activity_result.xml layout file is this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff141414">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="#dimen/_25sdp"
android:textStyle="bold"
android:text="Search Result goes here!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In your MainActivity.java add this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
All this shld help your app on the right track. Hope it helps
Changing to import android.support.v7.widget.SearchView helped me and also change these lines
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView= (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);`

Search on ActionBar

Level:Beginner
I am trying to implement a search bar on ActionBar. I want that when someone clicks the search icon, he should get a text field, where he simply fills in the text and can search by submitting through the keyboard search Button. Following several tutorials, I am quite confused.
When I do not add
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
I get an icon for search, but it does not expands into a text field. So I am not able to search anything from there.
when I add
`searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); `
I get an error 'unfortunately, Application has stopped' and the logcat shows:-
Process: com.example.bhavya.myapplication, PID: 23261
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setIconifiedByDefault(boolean)' on a null object reference
at com.example.bhavya.myapplication.MainActivity.onCreateOptionsMenu(MainActivity.java:27)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
I also tried to set 'intent-filter' and 'meta-data' in the following way after I was unsuccesful
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bhavya.myapplication" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity"/>
</activity>
<activity
android:name=".SearchableActivity"
android:label="#string/title_activity_searchable" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
but nothing changed, I also made a folder "xml" and set searchable configuration
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/app_name" >
</searchable>
This is my menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is my 2nd activity
public class SearchableActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;I get an icon for search, but it does not expands into a text field. So I am not able to search anything from there.
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}
}
I tried to look up a lot, but only got confused more. Please Help.

Android - SearchView in toolbar not expanding

I'm trying to add a SearchView in my toolbar, I've added the icon, but when I press it, it's not expanding.
Here's the code:
main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.try.MainActivity">
<item
android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager= (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView=null;
if (searchItem!=null) {
Log.d("createOptionMenu","search item not null");
searchView = (SearchView) searchItem.getActionView();
}
if (searchView!=null) {
Log.d("createOptionMenu","search view not null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}else{
Log.e("createOptionMenu","search view null");
}
return true;
}
(this always shows the log message "search view null")
AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".SearchResultActivity"
android:label="#string/title_activity_search_result" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="HINTTT" />
I've tried multiple options as using the expandActionView() or the setIconified(false), but non of those works for me.
You should use MenuItemCompat
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
and for collapsing SearchView use
MenuItemCompat.collapseActionView(searchItem);
and make sure you have changed android:actionViewClass to app:actionViewClass
I had same problem. I just solved mine by making a minor tweak. change
android:actionViewClass="android.support.v7.widget.SearchView"
to app:actionViewClass="android.support.v7.widget.SearchView"
as the docs say, you have to set the collapseActionView (bitwise-OR it with |ifRoom) flag for the showAsAction attribute in the menu.xml file.
Now both exapandActionView() and collapseActionView() will work.
In MainActivity you have to extends ActionbarActivity
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}

Android SearchView shows different layouts

I am defining a search action in my action bar. The search works perfectly. However, when touching the search icon a white background layout occurs (containing the app icon) and when touching the back arrow a blue layout occur (without the app icon).
My problem is that I only want to show the blue layout (appearing after touching the back arrow on the white layout).
Here are code relative to the search action bar implementation.
AndroidManifest:
<meta-data
android:name="android.app.default_searchable"
android:value=".RecipeListActivity" />
</activity>
<activity
android:name=".RecipeListActivity"
android:label="#string/title_activity_recipe_list"
android:parentActivityName=".RecipeFolderActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.cookbook.android.mycookbook.RecipeFolderActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
This is the definition of the menu in xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.cookbook.android.mycookbook.MainFolderActivity" >
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/icon_loupe"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/add_main_folder"
android:title="#string/add_main_folder"
android:orderInCategory="101"
app:showAsAction="never" />
</menu>
Here is the java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_folder, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.search){
Log.d(LOG_TAG,"In onOptionItemSelected, for search");
onSearchRequested();
}
return super.onOptionsItemSelected(item);
}
Any help will be greatly appreciated. :-)

Change the icons of Search in Actionbar

I added a search to the Action bar by using the following code:
activity_main_actions.xml
<!-- 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"/>
<!-- Add -->
<item android:id="#+id/action_add_tip"
android:icon="#drawable/header_add"
android:title="#string/action_add_tip"
android:showAsAction="ifRoom" />
<!-- Count -->
<item android:id="#+id/action_count_tip"
android:icon="#drawable/header_count"
android:title="#string/action_count_tip"
android:showAsAction="ifRoom" />
in MainActivity.java
#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);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
searchable.xml
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Manifest.xml
<activity
android:name="com.emy.healthytips.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
The search is working well with me, but the icon of the search in Action bar is still as it "Gray icon" like the following ScreenShot, although I added my own icon in
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
How can I add my own icon?
This question ma be related with your problem. Please check it.
How to change the default icon on the SearchView, to be use in the action bar on Android?

Categories

Resources