I have an Activity (MainActivity) with a search button in the action bar. It searches by some input string and shows the results in a ListView in another Activity (SearchResultsActivity). The user might click in any result to select it.
I want to return the value selected by the user to the main activity, but it's not working. I looked in the documentation but I didn't find anything related.
I tried to use setResult(Intent) in the results activity but the onActivityResult() from the main activity never gets called.
What am I doing wrong? How can I do it?
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foo.bar" >
<!-- ... -->
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name=".SearchResultsActivity"
android:parentActivityName=".MainActivity" >
<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>
Menu.xml:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<!-- Search -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<!-- ... -->
</menu>
MainActivity:
public class MainActivity extends FragmentActivity {
// ...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.activity_main_menu, 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 true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// never gets called!
super.onActivityResult(requestCode, resultCode, data);
}
// ...
}
SearchResultsActivity:
public class SearchResultsActivity extends Activity {
private ListView listResults;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
listResults = (ListView) findViewById(R.id.listResults);
handleIntent(getIntent());
}
#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);
final Serializable[] results = find(query);
listResults.setAdapter(new ArrayAdapter<BusLine>(this, R.layout.list_view, busLines));
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
setResult(RESULT_OK, new Intent().putExtra("result", selectedResult));
finish();
}
});
}
}
}
I can understand why you might think that you would use setResult and onActivityResult to handle clicks in your search results activity, but that is not how it works. Only when you launch an activity with startActivityForResult do these functions apply -- not when displaying search results.
The solution is to call startActivity on your MainActivity in the normal fashion, that is, not using activity results. Something like this:
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("result", selectedResult);
intent.setClass(SearchResultsActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
Now you will also need to capture this search result in your MainActivity, although you need to make sure you handle the normal case as well as the search results case:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent.hasExtra("result")) {
// Launched from search results
Serializable selectedResult = (Serializable) intent.getSerializableExtra("result");
...
}
...
}
Related
I am creating an app where a user can generate a random number from 1 to 90 to play bingo or tambola. Here I set the homeAsUpEnabled as true, and in the Manifest, I have added the support parent activity:
this is the main activity:
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setTitle("Full House: Housie Number Generator");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
ivBoard.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent = new Intent(NumberActivity.this, com.app.fullhouse.Board.class);
intent.putExtra("doneNumbers", doneNumbers);
startActivity(intent);
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.reset:
new AlertDialog.Builder(this)
.setTitle("Reset")
.setMessage("Press OK to reset Board")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1) {
recreate();
}
}).create().show();
break;
case R.id.showNoOrder:
Intent intent = new Intent(this, com.app.fullhouse.NumberOrder.class);
intent.putExtra("doneNumbers2", doneNumbers2);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
and this is my other activity:
ActionBar actionBar = getSupportActionBar();
tvnumbers = findViewById(R.id.tvNumbers123);
assert actionBar != null;
actionBar.setTitle("Sequence of Number generated");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
and part of another:
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Board");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
please take into notice that I have cut short much of the unrequired items because the code would get too long
What have I tried:
I tried to startActivityForResult and their end activity by guessing that the back button could be
android.R.id.home
when I try to press the back button on the phone, noting goes wrong and the previous data is still there
but when I press the back button in any of the two-child activities, it goes back, but it recreates the whole activity, hence the game is lost
here is the manifest in case you need it :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.fullhouse">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/fullhouse"
android:label="#string/app_name"
android:roundIcon="#mipmap/fullhouse"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".GenerateTicket"></activity>
<activity android:name=".NumberOrder">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NumberActivity" />
</activity>
<activity android:name=".NumberActivity"></activity>
<activity android:name=".Board">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NumberActivity" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
as well as the menu
<?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/reset"
android:menuCategory="container"
android:orderInCategory="104"
android:title="#string/reset_board"
app:showAsAction="never" />
<item
android:id="#+id/showNoOrder"
android:title="Show sequence of numbers"
app:showAsAction="never"
android:orderInCategory="105"/>
</menu>
thank you in advance
Possibly you can find your answer here
How to come back to First activity without its onCreate() called
And can try below from here
Two cases:
If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.
If you don't want to clear all activities and want an existing instance of A at the top, then use intent flags
FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
Note: If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be
called.
I have found an answer (to all the people who visit this later on :))
the answer is this
Intent intent = new Intent(this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_FROM_BACKGROUND);
startActivity(intent);
and in the manifest file, you don't need to add anything
cause here it's working for me
I just started with developing an app and encounter the following. I'm stuck.
When I press on the SearchView in the appbar on my phone the SearchView simply closes. It does not take any query and I can't find out what I did wrong.
The code from the .java file where the SearchView is in the appbar:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ik_bied_aan);
}
#SuppressLint("ResourceType")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.xml.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;
}
}
The manifest:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity"
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>
The SearchableActivity.java file:
public class SearchableActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
#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);
doMySearch(query);
//use the query to search your data somehow
}
}
private void doMySearch(String query) {
}
}
Thank you in advance for your time.
EDIT:
The logs after pressing multiple times on the SearchView appbar:
2019-02-20 16:46:05.958 8141-8163/com.example.android.zwapperr
D/OpenGLRenderer: endAllActiveAnimators on 0x7a9e579200
(MenuPopupWindow$MenuDropDownListView) with handle 0x7a9e6995c0
2019-02-20 16:46:24.317 8141-8148/com.example.android.zwapperr
I/ndroid.zwapper: Compiler allocated 4MB to compile void
android.widget.TextView.<init>(android.content.Context,
android.util.AttributeSet, int, int)
2019-02-20 16:46:25.492 8141-8163/com.example.android.zwapperr
D/OpenGLRenderer: endAllActiveAnimators on 0x7a9e5d0900
(MenuPopupWindow$MenuDropDownListView) with handle 0x7a9e699600
2019-02-20 16:46:39.643 8141-8163/com.example.android.zwapperr
D/OpenGLRenderer: endAllActiveAnimators on 0x7a9e5f5000
(MenuPopupWindow$MenuDropDownListView) with handle 0x7a9e5095a0
cannot find symbol class intent, cannot find...Activity2.class, cannot find...Activity-main
public class MainActivity extends AppCompatActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.MyButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
NewActivity2.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
all the imports precede the above code like import widget button, etc.
Open your Manifest and check if there are entries like these:
<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=".NewActivity2" />
Overriding method should call super, so it should be:
super.onCreate(savedInstanceState);
and make sure that your NewActivity2 is registered in manifest.
anyone can give me a solution on how to use searchView widget(not in action bar) to show a list of result base on what the user type in searchView.
i am very new in developing android application and it happen that i want to try to create a simple application that can retrieve a data from the database.
i try to search from the internet and i found 'searchView' which i am able to search a data. i copy the code from the internet and modify some part. i read the code, i actually understand some code but not all, now from the MainActivity, i see that the 'onSearchRequested();' is called and then an intent is start with a searchView in action bar.
i try to modify the code with putting a searchView widget at the layout, i want to use this instead of 'onSearchRequested();' but im stock, dont know how show the 'ListView' as the result.
this what the run looks like:
a searchview in actionbar
and i want to change it like:
a searchView with result of list item but not in action bar
the code::
'MainActivity.java' // The original
public class MainActivity extends FragmentActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn_search);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onSearchRequested(); // This will start eveything on button click
}
});
}
}
'MainActivity.java' // I already Modify
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn_search);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onSearchRequested();
}
});
SearchView searchView = (SearchView)findViewById(R.id.searchView1);
//This is just some code trying to customize the SearchView
searchView.setQueryHint("Type Word...");
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate!=null) {
searchPlate.setBackgroundColor(Color.WHITE);
int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText!=null) {
searchText.setTextColor(Color.DKGRAY);
searchText.setHintTextColor(Color.LTGRAY);
}
}
// and im stock here, dont now what next to do
}
}
SearchableActivity.java //Nothings modify here
public class SearchableActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
ListView mLVCountries;
SimpleCursorAdapter mCursorAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
// Getting reference to Country List
mLVCountries = (ListView)findViewById(R.id.lv_countries);
// Setting item click listener
mLVCountries.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent countryIntent = new Intent(getApplicationContext(), CountryActivity.class);
// Creating a uri to fetch country details corresponding to selected listview item
Uri data = Uri.withAppendedPath(CountryContentProvider.CONTENT_URI, String.valueOf(id));
// Setting uri to the data on the intent
countryIntent.setData(data);
// Open the activity
startActivity(countryIntent);
}
});
// Defining CursorAdapter for the ListView
mCursorAdapter = new SimpleCursorAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,
null,
new String[] { SearchManager.SUGGEST_COLUMN_TEXT_1},
new int[] { android.R.id.text1}, 0);
// Setting the cursor adapter for the country listview
mLVCountries.setAdapter(mCursorAdapter);
// Getting the intent that invoked this activity
Intent intent = getIntent();
// If this activity is invoked by selecting an item from Suggestion of Search dialog or
// from listview of SearchActivity
if(intent.getAction().equals(Intent.ACTION_VIEW)){
Intent countryIntent = new Intent(this, CountryActivity.class);
countryIntent.setData(intent.getData());
startActivity(countryIntent);
finish();
}else if(intent.getAction().equals(Intent.ACTION_SEARCH)){ // If this activity is invoked, when user presses "Go" in the Keyboard of Search Dialog
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String query){
Bundle data = new Bundle();
data.putString("query", query);
// Invoking onCreateLoader() in non-ui thread
getSupportLoaderManager().initLoader(1, data, this);
}
/** This method is invoked by initLoader() */
#Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle data) {
Uri uri = CountryContentProvider.CONTENT_URI;
return new CursorLoader(getBaseContext(), uri, null, null , new String[]{data.getString("query")}, null);
}
/** This method is executed in ui thread, after onCreateLoader() */
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
mCursorAdapter.swapCursor(c);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
activity_searchable.xml
//This is the list item result i want to show this under the searchView as the result
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lv_countries"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
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"
android:searchSettingsDescription="#string/search_settings"
android:searchSuggestAuthority="in.wptrafficanalyzer.searchdialogdemo.CountryContentProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://in.wptrafficanalyzer.searchdialogdemo.CountryContentProvider/countries"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="1"
android:includeInGlobalSearch="true" >
</searchable>
activity_main.xml
<RelativeLayout xmlns: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"
tools:context=".MainActivity" >
<SearchView
android:id="#+id/searchView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:imeOptions="actionSearch" >
</SearchView>
<Button
android:id="#+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/searchView1"
android:text="#string/search" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.searchdialogdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Activity with SearchDialog enabled -->
<activity
android:name="in.wptrafficanalyzer.searchdialogdemo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Enabling Search Dialog -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
<!-- A Searchable activity, that handles the searches -->
<activity
android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<!-- Activity that shows the country details -->
<activity android:name=".CountryActivity" />
<!-- Content Provider to query sqlite database -->
<provider
android:name=".CountryContentProvider"
android:authorities="in.wptrafficanalyzer.searchdialogdemo.CountryContentProvider"
android:exported="true" />
</application>
</manifest>
Again, Please im just a newbie, if there's something wrong on my question im really sorry, i love programming and im just trying to learn. anyway, thank you very much!!! any help will be appreciated!
Just refer this link ..
http://wptrafficanalyzer.in/blog/customizing-autocompletetextview-to-display-images-and-text-in-the-suggestion-list-using-simpleadapter-in-android/
It's not a big deal. If You know the CustomListView then it's easy for you.
Did not understand the whole question, but maybe you want to forget the onSearchRequested(); and change it with your customize searchView. try this,
you dont need to change anything on your code except in MainActivity. if you don't want onSearchRequested(); for some reason... here..
If you have a custom searchView in your actionbar and use it instead of searchView in your layout you may use this code:
public class MainActivity extends FragmentActivity{
static SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
//Your design here
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("Type something...");
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate!=null) {
searchPlate.setBackgroundColor(Color.DKGRAY);
int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText!=null) {
searchText.setTextColor(Color.WHITE);
searchText.setHintTextColor(Color.WHITE);
}
}
//Maybe this is what you want
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
and in you res/menu/main.xml add this
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
and try to execute your project
or if you really want to use your custom searchView in your layout then this:
public class MainActivity extends FragmentActivity{
static SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView = (SearchView)findViewById(R.id.searchView1);
searchView.setQueryHint("Type Word...");
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate!=null) {
searchPlate.setBackgroundColor(Color.WHITE);
int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText!=null) {
searchText.setTextColor(Color.DKGRAY);
searchText.setHintTextColor(Color.LTGRAY);
//This is what you want?
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.getRootView();//Notice that i change this
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
}
}
}
reference:: Setting Up the Search...
I am just trying to make my webview app run upon the bootup of an android device. I already followed every single step mentioned in this post however it does not run on device boot up. What am I missing here?
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
Toast.makeText(context, "Start Up", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context,
MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} catch (Exception e) {
Toast.makeText(context, "Start Up not possible!", Toast.LENGTH_LONG).show();
}
}
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.google.com/");
//Note: To detect when a URL has started and finished loading, use WebViewClient.onPageStarted and WebViewClient.onPageFinished.
}
#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);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
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;
}
}
}
and here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa.webview1" >
<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>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootUpReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</manifest>
your receiver is out of your application element, it should rather be inside
<receiver android:name=".BootUpReceiver"> // i am suppose to be a child of the application
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application> // clossing application element
for more info check here , and here