SearchView not searching - android

I am trying to implement search functionality in my android app using SearchView within the layout of my app. I purposefully chose to have it in the layout rather than in the Action Bar.
The problem is that once I type the search text and hit the enter/search key on the keyboard nothing happens. The searchable intent is never called. I feel like I must be missing something that is causing the search not to trigger
I apologize if I haven't included the code in the correct format below. Please let me know if there is any additional information that would help. Thanks in advance!
Manifest
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.lotr.arda.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".WebActivity"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".menuActivity"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
</activity>
<activity android:name=".searchActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
</manifest>
Res/XML/search
<?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/searchhint" >
</searchable>
SearchActivity.java
package com.lotr.arda;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
public class searchActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
System.out.println(query);
//doMySearch(query);
}
}
}
menuActivity.java (layout where searchview is located)
package com.lotr.arda;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView;
public class menuActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_page);
try{
AdView myad = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest();
myad.loadAd(adRequest);
SearchManager searchManager = (SearchManager)getSystemService(SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
SearchView sv = (SearchView)findViewById(R.id.searchView1);
sv.setSearchableInfo(searchableInfo);
Button alpha = (Button)this.findViewById(R.id.button1);
alpha.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(menuActivity.this, MainActivity.class);
startActivity(intent);
}});
}catch(Exception e) {System.out.println(e.toString());}
}
}

You are missing a SearchView#setOnQueryTextListener. You'll need to override the OnQueryTextSubmit() method to actually perform and handle your search.
EDIT: I am assuming you're querying a database and using a Cursor for the results.
mSearchView.setOnQueryTextListener(new OnQueryTextListener()
{
#Override
public void onQueryTextSubmit(String query) {
Cursor c = getContentResolver.query(URI, null, "col_1 = ? OR col_2 = ?", new String[] { query, query }, null);
//Do whatever you want with your Cursor here
}
}
);

For a quick look it seems you missed default_searchable setting in your AndroidManifest.
Under activity setting for MainActivity, you should add this
<meta-data
android:name="android.app.default_searchable"
android:value=".WebActivity" />

Make sure your searchables.xml file is in the correct location (i.e, in your res/xml/ folder) and that the same file does not contain any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, breaking your search functionality.
(Also, ensure you set componentName in the appropriate manner, because the example shown in the Android guide is for only when you are making searches and displaying searches in the same Activity.)

Related

SearchView does not trigger SearchActivity

I am having trouble making my SearchView in the ActionBar trigger an intent that is received by my SearchActivity. I think the issue is in the Manifest/XML somewhere, because the logcat isn't even showing the log in the onCreate method of the SearchActivity. I've used these resources, and others (including SO responses), to try and implement it, but haven't had any luck.
manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activitiesFragments.MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<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=".activtiesFragments.SearchActivity"/>
</activity>
<activity
android:name=".activitiesFragments.LoginActivity"
android:label="#string/login_button_text"
android:screenOrientation="portrait" />
<activity
android:name=".activitiesFragments.SignUpActivity"
android:label="#string/create_account_button"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activitiesFragments.LoginActivity" />
</activity>
<activity android:name=".activitiesFragments.SearchActivity">
<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>
</application>
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_messages, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.friends_menu_item).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
SearchActivity
package dev.workman.shoutout.activitiesFragments;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import dev.workman.shoutout.R;
public class SearchActivity extends AppCompatActivity {
ArrayList<String> query_results = new ArrayList<>();
ArrayAdapter<String> adapter;
ListView results_view;
String TAG = "SearchActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
results_view = (ListView) findViewById(R.id.results_list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, query_results);
results_view.setAdapter(adapter);
Log.d(TAG, "onCreate: created searchview activity");
handle_intent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
query_results.clear();
handle_intent(getIntent());
}
public void handle_intent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d(TAG, "handle_intent: query " + query);
// process the query, add values to the query_results adpater
// then call adapter.notifyDataSetChanged()
}
}
}
I figured out my problem. Rather than an XML/manifest issue, I the problem lied with the getComponentName() call.
I changed it to
new ComponentName(MainActivity.this, SearchActivity.class)
and it works now. I hope this helps others with the same issue

Application crashes upon new Activity

I know and I do understand that this question has been asked, but I can't seem to interpret it for my application. I am creating an application - using Android Studio - that opens a Activity (called 'About'). When a user clicks on the 'about button' on my MainActivity, it should launch the 'About' activity. However, when I test this out on my device, it says the app has stopped. And on my output panel, it says something about an error with my Manifest.xml file?
MainActivity:
package com.msp.supercarsounds;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickedAbout(View view) {
final int result = 1;
Intent AboutButtonClicked = new Intent (this, About.class);
AboutButtonClicked.putExtra("About", "MainActivity");
startActivityForResult(AboutButtonClicked, result);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.msp.supercarsounds">
<uses-sdk android:minSdkVersion="17"
android:targetSdkVersion="22"/>
<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>
</application>
</manifest>
Thank you for your help and time!
You have to declare About activity
<activity android:name=".About">
</activity>
Add this below the </activity> tag of MainActivity
your second activity "about" is not added to manifest.xml add this under the mainactivity
<activity android:name=".about">/activity>

Calling another class from MainActivity in OnCreate()

I have two main classes in my application for activity launch as.
MainActivity.Java
package com.connect;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
private PolicyManager policyManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
policyManager = new PolicyManager(this);
System.out.println("Hello");
if (!policyManager.isAdminActive()) {
Intent activateDeviceAdmin = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdmin.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN,
policyManager.getAdminComponent());
activateDeviceAdmin
.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"After activating admin, you will be able to block application uninstallation.");
startActivityForResult(activateDeviceAdmin,
PolicyManager.DPM_ACTIVATION_REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK
&& requestCode == PolicyManager.DPM_ACTIVATION_REQUEST_CODE) {
// handle code for successfull enable of admin
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Droidian.java
package com.connect;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.io.IOException;
public class Droidian extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
PackageManager i = getApplicationContext().getPackageManager();
i.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
if (isMyServiceRunning() == false) {
startService(new Intent(getApplicationContext(), DroidianService.class));
Log.i("com.connect", "startService");
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DroidianService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
After executing MainActivity.java , Droidian.java should run as this is the class which would be doing all the function. I don't know how to call the other class in MainActivity.java class. Here is my Manifest file snippet.
Android Manifest.xml
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connect"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" tools:ignore="OldTargetApi"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<supports-screens android:resizeable="true"
android:largeScreens="true"
android:xlargeScreens="true"
/>
<application
android:allowBackup="false"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:persistent="true">
<activity
android:name="com.connect.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>
<receiver
android:name="com.connect.SampleDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin" />
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You cant declare two activities as LAUNCHER. change your droidian activity as DEFAULT.
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Add this to your onCreate
Intent intent = new Intent(getApplicationContext(), Droidian.class);
startActivity(intent);
A few issues:
At first, there is the launcher thing Yogesh mentioned. The launcher category in the intent filter means, that this Activity is the one that gets called first, thus there can only exist one launcher.
It isn't exactly clear if you really want Droidian to be a Activity, since you plainly call it a class. Is it meant to have its own view, or should it be a worker class? If the later, don't extend Activity, just make it a regular class. You then can call the functions by instanciating it in your MainActivity like Droidian mDroidian = new Droidian() or making your functions static.
If you really intent to use a Activity and 'call onCreate', you have to start the Activity. Certain methods can't be started manually but are called automatically once you start an Activity, see the Android reference on Activities, especially the 'Activity Lifecycle' section.
The easiest way to start an Activity would be
Intent intent = new Intent(MainActivity.this, Droidian.class);
startActivity(intent);
Also you might want to have a look at the Android Developer trainings, especially the one about starting an activity.

Unfortunately [yourapp], has stopped

I'm using intent method to make a simple app that takes you from main screen [with an enter button] to another screen which has three options. Code sourced online and seems to be error free, though my app crashes saying "Unfortunately [yourapp], has stopped" immediately after i press the button which is meant to take to the the other screen.
This is my first activity code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.enterBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, HomeActivity.class);
startActivity(intent);
}
});
}
}`
And this is my landing screen's activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
I'm really stuck with this issue and any help would be much appreciated. Many thanks in advance.
Every activity needs to be added in your manifest file under application tag. This seems to be the problem in your case.
Try to post you LogCat so that we might get some more information and if you have not yet added your Activity in the manifest, this is the way of adding it (Activities go under application tag)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.southmp3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:label="#string/app_name" >
</activity>
</application>
all the Activities should be going under the application tag
I had forgotten to register my new activity on the androidmanifest.xml file. That's what sorted my app crash issue.

Cannot load new blank activity - android tutorial

I am a new android developer and encountered a problem while following the tutorial on Android's site - http://developer.android.com/training/basics/firstapp/starting-activity.html
My program loads on the emulator fine, but when you type something into the EditText and then hit the send button, a window pops up saying "App has stopped working". It seems as though my new activity isn't being created and I'm not sure as to why. I've scoured the web (and my code) to see if I could get a solution but did so to no avail. Any help would be appreciated.
The code for my first activity:
package com.example.appli;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout);
//////Button myButton = (Button) findViewById(R.id.my_button); //Told in tutorial to put this in onCreate
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
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);
}
}
The code for my second activity (the one being called)
package com.example.appli;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
The Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appli"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.example.myapp.DisplayMessageActivity" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In AndroidManifest File you have declared incorrect package
change
<activity android:name="com.example.myapp.DisplayMessageActivity" />
to
<activity android:name="com.example.appli.DisplayMessageActivity" />
or just
<activity android:name=".DisplayMessageActivity" />
you have declared only one Activity that's also wrong package name
already your manifest has the package name
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appli"
so just declare like this
<activity android:name=".DisplayMessageActivity" />
and you need to add this in your manifest also
<activity android:name=".MainActivity />
remove the target SDK version that you mentioned
android:targetSdkVersion="15"
Try this
<activity android:name=".DisplayMessageActivity"/>
Instead of
<activity android:name="com.example.myapp.DisplayMessageActivity" />
in your Manifest.xml file.
You has defined the package package com.example.appli; in your activities.
Why have you defined you activity in manifest like <activity android:name="com.example.myapp.DisplayMessageActivity" />?
Replace this line with
<activity android:name=".DisplayMessageActivity" />.

Categories

Resources