SearchView doesn't start the SearchableActivity - java

I want to add a SearchView to my ActionBar, so I've done as in Google Tutorial reported here.
AndroidManifest.xml
<activity
android:name="it.polimi.dima.sound4u.activity.SoundSearchActivity"
android:label="#string/title_activity_sound_search" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name="it.polimi.dima.sound4u.activity.MyGiftsActivity"
android:label="#string/title_activity_my_gifts" >
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
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="#string/search_hint" />
MyGiftsActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_gifts, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
case R.id.action_logout:
doLogout();
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
my_gifts.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:title="#string/search_title"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item android:id="#+id/action_logout"
android:title="#string/logout_label"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
SoundSearchActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_search);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.w(TAG_LOG, query);
}
}
The SearchView is correctly created and can switch from the Icon mode to the TextView mode but when I push the Search Button on the keyboard the new SoundSearchActivity doesn't start. Anyone can help me?

Try this in your manifest:
Add a meta-data pointing to the SearchActivity in the tag of the Activity where search is started
Move the searchable tag to the SearchAvtivity
Add launchMode="singleTop" to the SearchActivity to make sure there's only one instance of it at top of the stack. New search intents will come in trough onNewIntent which you already implemented.
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name="it.polimi.dima.sound4u.activity.MyGiftsActivity"
android:label="#string/title_activity_my_gifts" >
<meta-data android:name="android.app.default_searchable"
android:value="it.polimi.dima.sound4u.activity.SoundSearchActivity"/>
The initial guide by Google (found here) didn't give me a working SearchView either, but making theses changes did.

Related

Action Bar - Missing Menu

As I am new to Android Studio I have one question to ask here and I hope You guys don't mind.
So I created action menu that should have 3 doots at right upper end and that should call menu.
Anyway here is my menu:
In my main activity I am able to see menu but I am not able to see title and 3 doots.
Code of my main.java class (LocationInit):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.GoogleMaps:
Intent googlemaps = new Intent(this, googlemaps.class);
startActivity(googlemaps);
break;
case R.id.Settings:
Intent settings = new Intent(this, SettingsActivity.class);
startActivity(settings);
break;
case R.id.LastLocation:
Toast.makeText(getApplicationContext(),"Help", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
Finally my app_bar.xml (XML Of action bar):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/CustomToolbarStyle"
app:popupTheme="#style/CustomPopupTheme">
</android.support.v7.widget.Toolbar>
main_menu.xml
code :
<?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/menu_main_setting"
android:icon="#drawable/ic_settings"
android:orderInCategory="100"
app:showAsAction="always"
android:actionLayout="#layout/toolbar"
android:title="Setting" />
<item
android:id="#+id/menu_main_setting2"
android:icon="#drawable/ic_settings"
android:orderInCategory="200"
app:showAsAction="always"
android:actionLayout="#layout/toolbar"
android:title="Setting" />
</menu>
and just override onCreateOptionsMenu like this in your MainPage.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
and in onCreate() add this
toolbar.inflateMenu(R.menu.main_manu);

Toolbar items shown only in activity_main

I've got menu_main.xml that works ONLY for activity_main.xml. How to make menu_main.xml works elswhere than only in one activity? When I change content view to another layout the toolbar items from menu_main disappears and leaves me with empty app_bar.
menu_main.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=".Przepisy">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
main.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_przepisy, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ambu.przepisy" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".Przepisy"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You need to add
getMenuInflater().inflate(R.menu.menu_przepisy, menu);
return true;
to all activities where you want this menu. Inside onCreateOptionsMenu(Menu menu) method.

Get query string from toolbar searchview in same activity?

I have to get search view query text in same activity without the help of listener.so what i had done so far.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
Manifest
<activity android:name=".MainActivty"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="Hint"
android:label="Label"></searchable>
And in MainActivty overrided as below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// Do work using string
}
}
Still am unable to get the search query after the user press search key. What am doing wrong.help me if any one knows.Thank you.
Hi I was having the same problem this es my menu.xml
<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_search"
android:title="#string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
</menu>
I changed:
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"
with:
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"
The problem was that I was using AppcompatActivity and needs support v7 SearchView.
Hope this helps.

icon not displayed on actionbar

I was going through the tutorial by google and couldn't figure out how to place search icon on action bar.
this is the main_activity_actions.xml code :-
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
this is the MainActivity.java :-
public class MainActivity extends ActionBarActivity
{
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void openSearch()
{
}
public void openSettings()
{
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:theme="#style/Theme.AppCompat.Light"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
I have already seen few answers and posted an the stack overflow and tried them and it still doesn't work
the search icon never shows up while I try to run it
can someone suggest what to do?
in activity_action.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:(YOUR_PROJECT_NAME)="http://schemas.android.com/apk/res-auto"
>
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
YOUR_PROJECT_NAME:showAsAction="always" />

android actionbar google tutorial

i have done the android action bar google tutorial,
but this is what i got:
the action setting button does not appear, and when the search button pressed nothing happens.
can any one tell me what is wrong with the code?
here is the files code..
MainActivity.java
package com.example.actionbar;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
public void openSearch(){
System.out.println("TEST SEARCH");
}
public void openSettings(){
System.out.println("TEST SETTINGS");
}
#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);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
MainActivityActions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity
android:name="com.example.actionbar.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your MainActivityActions.xml, change the android:showAsAction="never" under the settings to "always"
I see you are using AppCompat.
When using appCompat you need to specify more things in the menu xml
Exemple:
<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|collapseActionView" />
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
yourapp:showAsAction="never" />
</menu>
Note:
For the search item it's missing the bold part:
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always|collapseActionView"
and the settings item it's missing the following bold part:
yourapp:showAsAction="never"
the actionViewClass means it's going to use the SearchView with the dropdown "window"
you can also use collapseActionView to make the search item expand and collapse if you are not using the searchView
See this video from Chris Banes about appcompat

Categories