Search bar widget in android? [closed] - android

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Is there any search bar widget available just like android default search bar?. i just want search bar like default one and im going to handle searching functionality. I can do this by using edit text widget but i want to know is there any ready made widget is available?

See here Using the Search Widget in android

In your onCreate method you can use:
onCreate method:
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
handleIntent(getIntent());
OnCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feed, 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;
}
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="br.com.srtorcedor.FeedActivity">
<item android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
android:title="#string/search_title"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
</menu>
Searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_label"
android:hint="#string/search_hint" >
</searchable>
In the fragment, you need to put the following methods:
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void doSearch(String queryStr) {
Log.i("Your search: ",queryStr);
}
Doing it, you will see the search widget on the action bar. I hope it can work with y'all.

you can the make the edit text acts like a search bar using the TextWatcher class.

Related

Simple Android SearchView not functioning properly

I am trying to follow the search interface manual from android.com. I have few problems and I am not sure what I did wrong exactly.
If you want to look, here is the project as zip file.
1- The android:hint is not visible when I touch search icon. It comes empty. Why? If I set it programmatically it works eg. searchView.setQueryHint(getString(R.string.search_hint)); Why it doesn't work from XML?
2- Search field does not get focus and keyboard does not appear automatically when I touch search icon. I need to touch to the search textfield to get focus. I believe it should get focus automatically?
3- When I write something and touch the search icon on the keyboard. I do not see any log entries logged at logcat window. What is missing?
4- (SOLVED) I tried android:iconifiedByDefault="false" in XML and also searchView.setIconifiedByDefault(false); and it is always iconified when I start the application. Why is that happening?
I found out that iI need to remove collapseActionView from app:showAsAction otherwise setIconifiedByDefault(false) does not work. Also if you remove collapseActionView and set setIconifiedByDefault(false) then it never collapses.
I am guessing I missed something somewhere but I am not sure exactly what...
So below are the steps I followed:
I started a new project with 'Basic Activity' in Android Studio 3.
It has a ready toolbar and 'Hello World' in the middle.
Then to xml folder I added the searchable.xml file with contents:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search hint text"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>
Then changed AndroidManifest.xml file and added an activity:
<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>
Then created a simple SearchableActivity.class. Just to log some lines.
package com.test.myapplication;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchableActivity extends ListActivity {
private static String TAG = SearchableActivity.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Log.d(TAG, "handleIntent");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
}
Then added to existing menu_main.xml the necessary item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.test.myapplication.MainActivity">
<item android:id="#+id/search"
android:title="#string/app_name"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Update: If I change to app:actionViewClass="android.support.v7.widget.SearchView" and also in MainActivity.class if I import import android.support.v7.widget.SearchView; then when I click on the search icon, it shows default Search... text gets focus automatically. But does NOT show hint from XML and still there are no log entries when I submit any information.
UPDATE:
I realized that if I put the intent-filter and the meta part under MainActivity I am getting the search intent inside main activity. But I don't understand why it does not start the SearchableActivity. Because manual page for SearchView clearly says:
When a user executes a search from the search dialog or widget, the system starts your searchable activity and sends it a ACTION_SEARCH intent.
Use this
app:actionViewClass="android.support.v7.widget.SearchView"
Instead of this
app:actionViewClass="android.widget.SearchView"
Apparently all I needed to do was to forward the intent to correct activity from onCreateOptionsMenu()
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
if (searchManager != null)
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

Icons not showing up in Action bar

I am trying to get an action bar within my android app to show up with icons. No matter what I do, I only see the overflow icon with a dropdown, while I want to see the Home and Logout icons next to each other on the action bar menu. I have read through most posts on SO for a solution but am unable to find a solution. Can anyone please help ?
Below is my code :
AndroidManifest.xml
<application
android:name=".application.MySampleApplication"
android:icon="#drawable/letter"
android:label="My Sample"
android:theme="#android:style/Theme.WithActionBar">
Menu_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_logout"
android:icon="#drawable/logout"
android:title="#string/logout"
android:orderInCategory="0"
app:showAsAction="always" />
<item
android:id="#+id/action_home"
android:title="#string/gohome"
android:icon="#drawable/ulogo"
android:orderInCategory="0"
app:showAsAction="always" />
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
logout();
return true;
}
if (id == R.id.action_home) {
goHome();
return true;
}
return super.onOptionsItemSelected(item);
}
How it shows up now
How I want it to show up
MainActivity extends Activity
This means that you are using the native action bar, not the backport offered by appcompat-v7. In that case, replace the app: prefixes in your menu resource with android:, and you will have better luck.
Was googling for a solution and saw someone use it
That is surprising. Your question is the first time I have ever seen anyone use that theme. I didn't even know it existed until I looked it up as part of checking your question. Most likely, you will want to use more modern themes.

Actionbar compatibility two sets of menu dots

I have been trying to add an action bar to my application.
It is working but not as nice as I would hope. The problem is that I have two sets of three dots for accessing the menu. In the Action bar itself, and along the bottom where my soft keys are. This is the same on a Nexus 4 and a Moto X. Both sets of buttons work. On a Note 8 I have no buttons at all (should I expect the top action bar ones?), but the (physical) menu key does what it is supposed to do.
Extracts from Manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
login_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/login"
android:title="#string/menu_login"
android:icon="#android:drawable/ic_lock_idle_lock"
app:showAsAction="never"/>
</menu>
Extracts from Login.java
public class Login extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.login:
handleLogin();
return true;
}
return super.onOptionsItemSelected(item);
}
This is how it looks, any assistance appreciated.
As per the Say Goodbye to the Menu Button blog post, you need to add a targetSdkVersion of 14 or higher:
<uses-sdk android:minSdkVersion="10"
android:targetSdkVersion="19" />
Note it is a good practice to always set your target SDK version to the highest version you have tested your app on as it is the target SDK version that sets up compatibility modes such as what you are seeing.

Search View uses app icon instead of logo

Android documents explain that app logo is used everywhere when it is defined. But when the search view expands, app icon is used instead of app logo and I can't find a way to show app logo when search view is in expanded state.
Here are the relevant parts.
Manifest file
<application
android:name="MyApp"
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:logo="#drawable/app_logo"
android:theme="#style/Theme.MyTheme" >
searchable.xml (setting icon doesn't change anything)
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always|collapseActionView" />
Activity Code
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.menu_search).setActionView(searchView);
NOTE: I use ABS SearchView but same thing happens when I switch to default SearchView.
EDIT : I added some screenshots for clarity.
Here I used the star image for app logo, and android image for app icon.
The first screenshot is the default view of activity.
The second one is the view when I click search button.
It shows the android image while I expect it to be the star image.
Set the logo drawable as your icon. Via theme attribute
<item name="android:icon">#drawable/ab_logo</item>
or in code
getSupportActionBar().setIcon(R.drawable.ab_logo);
You're using a completely different SearchView you declared in menu.xml.
With this code you create new SearchView, which is not what you want.
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
So try creating menu like this.
this.getSupportMenuInflater().inflate(R.menu.menu, menu);
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
final MenuItem searchItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(info);
And for the ActionBar, instead of setDisplayUseLogoEnabled(true) and others, try set all options at once
final ActionBar bar = getSupportActionBar();
bar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_HOME_AS_UP);
This is the setup I have in my current project and never experienced any problems like these.
I had the same problem and I solved it using the following code:
menu.findItem(R.id.menu_search).setOnActionExpandListener(this);
// Your Activity/Fragment must implement the MenuItem.OnActionExpandListener interface
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
getSherlockActivity().getSupportActionBar().setIcon(R.drawable.uvweb_logo);
return true;
}
Let me know how this works for you.
I achieved the solution with this simple logic:
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_search:
getActionBar().setIcon(R.drawable.your_logo);
return true;
}
return super.onOptionsItemSelected(item);
}
Just remove collapseActionView from your menu Xml.
To replace the icon with a logo, specify your application logo in the manifest file with the android:logo attribute, then call setDisplayUseLogoEnabled(true) in your activity.
refer this document1
document2
And please see this answer for more details
The only thing you need to have set is this (in onCreate of activity):
getSupportActionBar().setIcon(R.drawable.logo);
...not there is also a setLogo.
Regardless to if you have the icon in the manifest set to something different, this will tell the activity to use a different image during runtime.
I tested this with the following menu item and it works:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_hint"
android:orderInCategory="1"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always|collapseActionView"/>
</menu>
Inflated like normal in onCreateOptionsMenu:
getSupportMenuInflater().inflate(R.menu.search_menu, menu);
I had the same problem, (for ABS) I had:
ActionBar actionBar = ((SherlockFragmentActivity) activity).getSupportActionBar();
actionBar.setLogo(R.drawable.logo);
And I just added:
actionBar.setIcon(R.drawable.logo);
And the issue is gone.
I found the best way to solve this problem was to add the SearchView programatically to the menu like this:-
SearchView searchView = new SearchView(getActionBar().getThemedContext());
menu.add("Search")
.setIcon(R.drawable.ic_action_search)
.setActionView(searchView)
.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
getActionBar().setIcon(R.drawable.ic_action_willyweather);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
})
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
And then use the onActionExpandListener to change the icon/logo to the correct value when then SearchView is expanded.
This solution may also work if you define the SearchView in menu.xml, but you'd then still need to setup the onActionExpandListener() in code to do the same as the above example.
Andy.

Beginning android: Working with the main pop-up menu

Apologies for what I am sure is a common question, but after Googling for a while it seems I don't know the proper Android term for what I want and so am stuck.
I would like to know how to work with the main pop-up menu associated with a view...that is, if you are sitting at some activity doing nothing and press the menu key on the phone, how to work with that menu that commonly opens on many apps containing "Settings", "Exit", etc.
I am not sure what this is called, but if someone can point me to the appropriate portion of the SDK, that would be appreciated. Also, if someone knows how to work this menu within the context of the Eclipse ADT plugin, that'd be great, too.
Cheers.
As CaseyB says, it's just called a menu.
For a quick start on using it, you could create a subfolder in the res folder of an eclipse project and call it "menu" add in some xml for the view, and the call a MenuInflator from the onCreateOptionsMenu function.
Ok, that might be a bit confusing, so i've included some sample code that should get you started. This code should make it so when you press the menu button you can select one of two new activities to get loaded.
In Main Activity add:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// Define whatever other activities you can to load in here or whatever.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
startActivity(new Intent(this, Info.class));
break;
case R.id.logs:
startActivity(new Intent(this, Logs.class));
break;
}
return true;
}
menu.xml file: Link to whatever icon images you want.
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<!-- Copyright © 2012 Tutela Technologies Ltd. -->
<!-- All Rights Reserved. -->
<!-- -->
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/info"
android:icon="#drawable/ic_menu_info_details"
android:title="Info"></item>
<item
android:id="#+id/logs"
android:icon="#drawable/ic_menu_report_image"
android:title="Logs"></item>
</menu>
Then in your AndroidManifest.xml make sure you remember to add in the new activities.
<activity
android:name="com.whatever.Gui.Info"
android:label="#string/appTitle">
</activity>
<activity
android:name="com.whatever.Gui.Logs"
android:label="#string/appTitle">
</activity>
Note: in this example the labels are defined in strings.xml
<string name="appTitle">Your App name</string>
Hope this helps you!
Cheers
It's just called the menu. Here is a tutorial to get you started. Things get a little weird in 3.0+ but once you get the basics down it shouldn't be too hard to pick up.
On versions prior to 3.0, these are menus. Menus are being replaced by the ActionBar in the more recent versions of Android.
About menus: http://developer.android.com/guide/topics/ui/menus.html
Tutorial about menus: http://www.androidhive.info/2011/09/how-to-create-android-menus/
Look at the example:
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SettingsMenuItem:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In xml: (in /res/menu/menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/SettingsMenuItem"
android:icon="#drawable/settings"
android:title="#string/settings"/>
</menu>

Categories

Resources