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
Related
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I used startActivity() method but it is not working.I think this method is not working because my MainActivity extends app compat actvity.
So please help me to start activity in an app compat activity.
I try various times but when I start my apk and click on button then app crashes.
Here my code
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E96050"/>
<TableRow><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainButton"
android:text="Open Second Activity"
android:onClick="yas"/></TableRow> </TableLayout>
MainActivity.java
package com.mycompany.myapp;
import android.app.Activity;
import android.os.*;
import android.support.v7.app.*;
import
android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.*;
public class MainActivity extends
AppCompatActivity {
#Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0,0,0,"item1");
{
mnu1.setIcon(R.drawable.ic_launcher);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public void yas(View vv) {
Intent iiii = new
Intent(MainActivity.this,SecondAct.class);
startActivity(iiii);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".SecondActivity"
android:label="secondActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="SecondActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
use this
Intent intent=new Intent(this,Target_Activity.class);
startActivity(intent);
And open your manifest file add this line
<activity android:name=".Target_Activity"
android:parentActivityName=".MainActivity">
</activity>
I hope you try this code..
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Please add second activity into android manifest file.
<activity android:name=".SecondActivity"/> // define second activity name.
add this code in android manifest file..
<activity
android:name=".SecondActivity"
android:label="secondActivity" >
</activity>
I am following along a YouTube Tutorial on how to get a query from a searchview and I am getting this error cannot be referenced from a static context.
MainActivity.java:
package com.zoggfrombetelgeuse.clef;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_toolbar);
getSupportActionBar().setTitle(R.string.my_toolbar_tile);
Intent searchIntent = getIntent();
if(Intent.ACTION_SEARCH.equals(searchIntent.getAction())){
String query = searchIntent.getStringExtra(SearchManager.QUERY);
Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
SearchView searchview = (SearchView) menu.findItem(R.id.action_search);
SearchManager searchmanager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()) );
return super.onCreateOptionsMenu(menu);
}
}
MainActicity is my only Activity.
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zoggfrombetelgeuse.clef">
<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>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity"/>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"
/>
</activity>
</application>
Change this line
SearchView.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()) );
to
searchview.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()) );
I am developing an android application, the problem is that all activities work fine on emulator but one of the activity doesn't load on tablet (Running 4.4 kitkat) and stopes the Application, MainPage.Class is not working.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarttrack"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme" >
<activity
android:name="com.smarttrack.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.smarttrack.LoginActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.MainPage"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.AskActivity"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.AskLocation"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.Options"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.Settings"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.smarttrack.MsgBroadcast">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
MainPage.Class
package com.smarttrack;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainPage extends Activity implements OnClickListener{
ImageView img;
TextView statusText;
String status ;
Button askActivity;
Button askLocation;
Button addActivity;
Button optionsMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
initVars();
img.setOnClickListener(this);
askActivity.setOnClickListener(this);
askLocation.setOnClickListener(this);
addActivity.setOnClickListener(this);
optionsMenu.setOnClickListener(this);
}
private void initVars() {
// TODO Auto-generated method stub
img = (ImageView)findViewById(R.id.switchingImage);
statusText = (TextView)findViewById(R.id.appStatus);
img.setImageResource(R.drawable.switch_off);
status = "active";
askActivity = (Button) findViewById(R.id.AskActivity);
askLocation = (Button) findViewById(R.id.btAskLocation);
addActivity = (Button) findViewById(R.id.addActivity);
optionsMenu = (Button) findViewById(R.id.optionsButton);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch(id){
case R.id.switchingImage:
Runnable swap = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(status == "active"){
img.setImageResource(R.drawable.switch_off);
statusText.setText("Status = Running");
status = "unactive";
}else {
img.setImageResource(R.drawable.switch_on);
statusText.setText("Status = Stopped");
status = "active";
}
}
};
img.postDelayed(swap, 100);
break;
case R.id.AskActivity:
Intent i = new Intent(this,AskActivity.class);
startActivity(i);
break;
case R.id.btAskLocation:
Intent loc = new Intent(this,AskLocation.class);
startActivity(loc);
break;
case R.id.addActivity:
break;
case R.id.optionsButton:
Intent opt = new Intent(this,Options.class);
startActivity(opt);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_item, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.ActionBarHelp:
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("SmartTrack").setMessage("Cool application");
Dialog dlg = bldr.create();
dlg.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
in your manifest xml your target sdk is 18 but 4.4 can run on 19
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.)
I want to create a splash screen that will then move to the login/register screen. My code looks like this:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AssaultTDActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.TimeOut();
}
public void TimeOut(){
long start = System.currentTimeMillis();
boolean continueloop = true;
long timenow;
while (continueloop = true){
timenow = System.currentTimeMillis();
if (timenow - start > 5000){
continueloop = false;
this.GoToRegister();
}
}
}
public void GoToRegister(){
Intent i = new Intent(AssaultTDActivity.this, register_activity.class);
startActivity(i);
finish();
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class register_activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
}
and my manifest file is the following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:screenOrientation="landscape"
android:label="#string/app_name"
android:name=".AssaultTDActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity class =".register_activity"
android:label="Log in"
android:screenOrientation="landscape"
android:name=".register_activity" >
</activity>
</application>
So am I doing something wrong here?
Also is there a command to "do events" while looping so you dot get stuck in a loop?
Hopefully this is the issue: looks like you may have had a find/replace mistake, this line in your manifest is wrong:
<uses-Activityk android:minActivitykVersion="8" />
Change it to:
<uses-sdk android:minSdkVersion="8" />
Because you added so many activities, it will most likely be fixed if you add:
<category android:name="android.intent.category.DEFAULT" />
So your main activity is the default activity and then the Android Launcher wont get tripped up.
<activity android:name=".MainActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Taken from: http://developer.android.com/reference/android/content/Intent.html
Activities will very often need to support the CATEGORY_DEFAULT so
that they can be found by Context.startActivity()