My main activity needs to invoke a search and display search results. The search dialog does not appear when onSearchRequested() is called.
I have found similar questions and followed every detail (I think) but apparently I have something else wrong. Here are snips of my implementation.
Parts of AndroidManifest.xml
<application
android:label="#string/AppName"
android:launchMode="singleTop"
... >
<meta-data
android:name="android.app.default_searchable"
android:value=".main.MainActivity" />
<activity
android:name=".main.MainActivity"
android:launchMode="singleTop"
... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/deep_search" />
</activity>
...
</application>
deep_search.xml
<?xml version="1.0" encoding="utf-8"?>
<Searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/AppName"
android:hint="#string/search_deep_hint" >
</Searchable>
Parts of MainActivity:
public class MainActivity extends Activity implements ...
{
#Override
protected void onNewIntent (Intent intent)
{
setIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra (SearchManager.QUERY);
// Do work using string
}
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
int selectedId = item.getItemId();
switch (selectedId)
{
case ...:
case R.id.menu_search_deep:
boolean launched = onSearchRequested();
logI ("home page deep searched launched: " + launched); // log shows we got here
}
return true;
}
...
}
Ok I'm incredibly frustrated I've been trying to get a SearchView to work for hours now with no luck. I checked out how to handle this and I found a sample project and the sample project works great. I then implement the same exact things in my project and bam I'm screwed. The intent won't fire. I don't understand why. I followed this
I have all my activities in an activity folder but removed the Search and Main one to see if that was the case... it's not. In my Search Results activity I'm handling the intent. It looks like this currently.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
txtQuery = (TextView) findViewById(R.id.txtQuery);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_result, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
txtQuery.setText("Search Query: " + query);
}
}
My Searchable XML looks like this.
<?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" />
For some reason the search hint is not appearing, Yet the string resource shows up and it's declared.
my options menu looks like this
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="Search"
android:icon="#android:drawable/ic_search_category_default"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item android:id="#+id/Menu"
android:title="MENU"
android:showAsAction="collapseActionView|ifRoom"
/>
And my Manifest looks like this.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MenuActivity"
android:label="#string/title_activity_menu"
android:theme="#style/IDME.Theme.Transparent" >
</activity>
<activity
android:name=".activities.CategoriesActivity"
android:label="#string/title_activity_categories" >
</activity>
<activity
android:name=".SearchResultActivity"
android:label="#string/title_activity_search_result"
>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
</application>
</menu>
And my Search View inflator looks like this.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
I cannot for the life of me figure out what's going on, I followed both guides to a tee, I cannot see what's wrong. I've messed around with metadata and I just really can't see the problem anymore. I'm stuck
I, too, followed the exact same (official Android) guide Setting Up the Search Interface and was in the same boat as the OP #rubsnick.
Turns out, all that that needs to be tweaked is--
Change
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Notice the <meta-data code! (specifically, android:name)
Also, no need to specify the searchable xml resource here... instead, do that in the SearchResultActivity activity, as shown below...
Lastly, Modify <activity for SearchResultActivity, like so:
<activity
android:name=".SearchResultActivity"
android:label="#string/title_activity_search_result" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
</activity>
The following SO thread does full justice as to why default_searchable and not searchable:
What's the difference between android.app.default_searchable and android.app.searchable meta-data?
your meta data is incorrect you must use:
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"
android:value="your package name of searchable activity.SearchResultActivity" />
In my application i made my content provider searchable, defining my main activity as the default search activity on my manifest.
<activity
android:name="com.pt.application.MainActivity"
android:label="#string/title_activity_map"
android:launchMode="singleTop"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Then i implemented the onSuggestionClick option to update a map, based on the suggestion clicked by the user. The searchview is on the action bar.
The problem is that when the user clicks on a suggestion, somehow the activity is recreated (i think OnPause and OnResume are called?). Is there any way to prevent this from happening? I don't want that my activity is "recreated" every time the user clicks on a suggestion.
If more code is needed, please ask. Thanks.
EDIT: Code added
<?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"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="web_search"
android:searchSuggestAuthority="com.pt.provider"
android:searchSuggestIntentData="content://com.pt.provider/busstop">
</searchable>
On my main activity:
// Bind the Activity’s SearchableInfo to the Search View
searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
this.setupSearchView();
private void setupSearchView()
{
// Use the Search Manager to find the SearchableInfo related
// to this Activity.
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
searchView.setSubmitButtonEnabled(false);
searchView.setOnSuggestionListener(new OnSuggestionListener() {
#Override
public boolean onSuggestionSelect(int position) {
return false;
}
#Override
public boolean onSuggestionClick(int position) {
// Do things
return false;
}
});
}
Solved my question. Changed on the manifest:
From:
android:launchMode="singleTop"
To:
android:launchMode="singleTask"
I've implemented the action bar found here https://github.com/johannilsson/android-actionbar and it works beautifully. However, I've added a search button and can't get it to show the search dialog. I followed the Google's search dialog tutorial to set my project up, but the dialog just won't show up. Any help?
Manifest:
<activity android:name=".HomeActivity" android:label="Intelicase Mobile" android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity" 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>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
<!-- this activity enables the search dialog to initiate searches in the SearchableActivity -->
<activity android:name=".OtherActivity">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
Action Bar setup:
ActionBar actBar = (ActionBar) findViewById(R.id.actionbar);
actBar.setTitle("Dashboard");
actBar.setHomeAction(new ActionBar.IntentAction(HomeActivity.this, intent, R.drawable.ic_title_home_default));
intent = new Intent().setClass(this, CaseEvidenceActivity.class);
actBar.addAction(new ActionBar.IntentAction(this, intent, R.drawable.ic_title_export_default));
intent = new Intent().setClass(this, OtherActivity.class);
actBar.addAction(new ActionBar.IntentAction(this, intent, R.drawable.ic_title_search_default));
OtherActivity:
public class OtherActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
onSearchRequested();
}
}
searchables.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="Intelicase Mobile"
android:hint="Search Intelicase" android:searchSuggestAuthority="dictionary"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
Action Bar Setup
actionBar.addAction(new SearchMP());
private class SearchMP extends AbstractAction {
public SearchMP() {
super(R.drawable.title_search);
}
#Override
public void performAction(View view) {
onSearchRequested();
}
}
I am using
Window w = getWindow();
w.setTitle("My title");
to change title of my current Activity but it does not seem to work.
Can anyone guide me on how to change this?
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
Just an FYI, you can optionally do it from the XML.
In the AndroidManifest.xml, you can set it with
android:label="My Activity Title"
Or
android:label="#string/my_activity_label"
Example:
<activity
android:name=".Splash"
android:label="#string/splash_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you want it one time & let system handle the rest (not dynamic) then do like this in your manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
<intent-filter android:label="#string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
setTitle(getResources().getText(R.string.MyTitle));
There's a faster way, just use
YourActivity.setTitle("New Title");
You can also find it inside the onCreate() with this, for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("My Title");
}
By the way, what you simply cannot do is call setTitle() in a static way without passing any Activity object.
This worked for me.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
getActivity().setTitle("My Title");
//...
}
If you have multiple activities, you can set it like this in AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="#string/category_numbers"
android:theme="#style/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family"
android:theme="#style/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors"
android:theme="#style/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases"
android:theme="#style/category_phrases" />
<activity
android:name=".ExperimentActivity"
android:label="#string/category_experiment"
android:theme="#style/category_experiment" />
</application>
I'm using Android Studio 3.0.1.
WIth an Activity:
setTitle("Title Text");
Inside a fragment:
getActivity().setTitle("Title Text");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
If you want to set title in Java file, then write in your activity onCreate
setTitle("Your Title");
if you want to in Manifest then write
<activity
android:name=".MainActivity"
android:label="Your Title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In Kotlin, this way:
this.title = resources.getText(R.string.fast_learning)
I have a Toolbar in my Activity and a Base Activity that overrides all Titles. So I had to use setTitle in onResume() in the Activity like so:
#Override
protected void onResume() {
super.onResume();
toolbar.setTitle(R.string.title);
}
The code helped me change the title.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
ActivityName.this.setTitle("Your Activity Title");}
Inside a MainActivity:
public class act1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
setTitle("First Activity");
}
}
setTitle("Whatever apps");
in MainActivity.java is simplier
I would say.
If you want to change Title of activity when you change activity by clicking on the Button. Declare the necessary variables in MainActivity:
private static final String TITLE_SIGN = "title_sign";
ImageButton mAriesButton;
Add onClickListener in onCreate() and make new intent for another activity:
mTitleButton = (ImageButton) findViewById(R.id.title_button);
mTitleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SignActivity.class);
String title_act = getText(R.string.simple_text).toString();
intent.putExtra("title_act", title_act);
startActivity(intent);
finish();
}
});
SecondActivity code in onCreate():
String txtTitle = getIntent().getStringExtra("title_act");
this.setTitle(txtTitle);
If you're using onCreateOptionsMenu, you can also add setTitle code in onCreateOptionsMenu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
setTitle("Neue Aktivität");
return true;
}
setTitle("Welcome Activity");