I'm implementing searchable for my application and I come to this problem. How can i change the default magnifying glass for my Searchable? The default looks big and fugly and I prefer to use the same drawable i used for my menu icon.
Searching "icon" in Searchable Configuration - Android Developer website doesn't lead to any useful information. I also tried by adding android:icon(It was an option in the Structure tab) to my searchable definition but it's not working.
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:icon="#drawable/ic_custom_test"
android:includeInGlobalSearch="true"
android:label="#string/search_label"
android:searchSettingsDescription="#string/settings_description"
android:searchSuggestAuthority="com.test.provider.SuggestionProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://com.test.provider.SuggestionProvider/info"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="1" >
</searchable>
Thanks in advance for any helps!
Implement your own searchview, delete the search item from main.xml/activity_main(your layout filename) in menu folder, then create a new item and your own search icon,.,
use iconfinder.com for different icons.
in your activity, in options method, create a switch case and add the functionality when you click on this item.,.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager localSearchManager1 = (SearchManager)getSystemService("search");
SearchView localSearchView1 = (SearchView)menu.findItem(R.id.search).getActionView();
localSearchView1.setSearchableInfo(localSearchManager1.getSearchableInfo(getComponentName()));
localSearchView1.setIconifiedByDefault(true);
if (Build.VERSION.SDK_INT >= 11)
{
SearchManager localSearchManager2 = (SearchManager)getSystemService("search");
SearchView localSearchView2 = (SearchView)menu.findItem(R.id.search).getActionView();
localSearchView2.setSearchableInfo(localSearchManager2.getSearchableInfo(getComponentName()));
localSearchView2.setIconifiedByDefault(true) ;
}
Related
First off, I'm running the latest Lollipop - Minimum and targeted SDK are both 22
minSdkVersion 22
targetSdkVersion 22
in the gradle build. I've changed/re changed these to 22, as well as rebuilt/invalidated caches/cleaned. I'm extending Activity in my main:
public class login_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
And here are my menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="ifRoom"/>
</menu>
When I run my code, it keeps showing the items in the ActionBar overflow menu. How do I fix this? Thanks!
Try replacing "ifroom" with "always" for showAsAction like below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="always"/>
</menu>
I actually spent nearly five hours trying to figure this out, but I actually figured it out just now going through my gradle files. Here's how you fix it:
Go to your Gradle Scripts folder
Open the build.gradle file with the SDK options in it
Remove the line under dependencies (Mine's this one, not sure how they vary):
compile 'com.android.support:appcompat-v7:22.2.0' (REMOVE THIS)
Go back to your menu file and REMOVE the namespace definition for the namespace you put before showAsAction
xmlns:app="http://schemas.android.com/apk/res-auto" (REMOVE THIS)
Go change the showAsAction line from something:showAsAction to android:showAsAction
Rebuild & compile. It should work! I wish changing the SDK cleaned up after itself, but hey, what can you do.
Following the instructions at http://developer.android.com/guide/topics/ui/menus.html I try to add an option menu to my existing Activity by first creating an xml as
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
and then inflating the menu in the activity as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Apparently that is not sufficient. So what am I missing in the code? I am testing with Samsung S5.
My activity extends FragmentActivity, in case that matters.
The problem might be with the theme your activity is using. Try to use a theme which has actionbar in it. I suppose, Theme.Holo.ActionBar.
I'm sure the menu is there but you are not able to just see it. The above code, as far as I know, has no problem. The problem might be with the phones with a menu key and the ones without it. There have been infinite discussions over that here, here and here, and also here. One of the hacks that people use to force their menu options in the action bar overflow is
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
Put the above code in the onCreate() of your application class. You are using S5, so try long pressing the multitasking key. That enables the menu options in S5.
I am trying to implement the SearchView ActionBar item as android developers says but I am having some trouble.
(http://developer.android.com/guide/topics/ui/actionbar.html).
There are two mistakes that although I have looked for a lot, I have not been able to find the solution.
1) I have a problem with the class MenuItemCompat. It says:
The method getActionView(MenuItem) is undefined for the type MenuItemCompat
I can only use for this class the following methods:
setShowAsAction(item, actionEnum)
setActionView(item, view)
Here it is the code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.restloader, menu);
MenuItem searchItem = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
return super.onCreateOptionsMenu(menu);
}
2) There is a problem with this:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
I don't understand why it is used but if google says it, it must be appropriate.
Error message:
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.pruebahttp3'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.pruebahttp3'
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/search_menu"
android:orderInCategory="100"
android:title="#string/search"
android:icon="#drawable/ic_search_category_default"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView">
</item>
Thank you very much!
i have got the same problem, i solved it by using the follow code. Be care of your namespace.`
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
share:showAsAction="ifRoom"
share:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/abc_searchview_description_search" />
`
For the 1st:Fixing the second one will fix this :)
For the 2nd:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
Change myapp to you application namespace com.xxx.xxx
Try to copy the lib files directly from yourFolder\sdk\extras\android\support\v7\appcompat\libs
I have a similar problem,but It occurs to me when i directly copy the JAR library file rather than following the android support library procedure. Try the opposite it might work for you.
Kinda weird if you ask me.
I am using ActionBarSherlock and Support Library v4. This is in a `SherlockFragmentActivity'.
I am getting a Nullpointer on line 269, referenced below. I have verified SearchView is null, not SearchManager. Though, menu is also null as well, I am not sure if that is normal? Important: It is ONLY null in my Android 4.0 and 4.1 tests. In 2.3, it works great!
Here is my SearchView Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView(); // line 269
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
return true;
}
I have this as my menu item now.
<item
android:id="#+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="search"/>
LogCat
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.---.---.master.MainFragmentActivity
.setupSearchView(MainFragmentActivity.java:269)
at com.---.---.master.MainFragmentActivity.onCreateOptionsMenu(
MainFragmentActivity.java:258)
at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:45)
at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(
ActionBarSherlock.java:559)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(
ActionBarSherlockCompat.java:479)
at com.actionbarsherlock.internal.ActionBarSherlockCompat
.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:272)
at com.actionbarsherlock.internal.ActionBarSherlockCompat$1.run(
ActionBarSherlockCompat.java:984)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(
ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
imports:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import android.app.SearchManager;
Again, I get Null Pointer in ONLY the 4.0 emulator and 4.1 device. App runs great in 2.3. At one time, pre-ActionBarSherlock, using the SAME code, except importing SearchView through the normal way (not Sherlock) it worked great in 4.0. I am trying to make my app backwards compatible and is broke the newer Android versions, but works great in the older.
EDIT: It was actually Null in 2.3 and Cast Issue in 4.0+ when the menu item had android:actionViewClass="android.app.SearchView"
Then I changed it to android:actionViewClass="com.actionbarsherlock.widget.SearchView"
and now FIXED in 2.3 and Null in 4.0+
UPDATE: I appeared to fix it with a hack:
I added a menu-v11 folder added a duplicate file of menu.xml with one item change only: android:actionViewClass="android.app.SearchView", otherwise it is identical to the other menu.xml file.
In Java, I did this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setupNewSearchView(searchItem, searchManager);
}
return true;
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(MenuItem searchItem,
SearchManager searchManager) {
android.widget.SearchView searchView =
(android.widget.SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
}
Well, it works, but I'm not putting it as an answer yet because it seems like hack way of doing it. Open to better ideas?
There is a problem with missing attrs for v14 (and v11?) in ActionBarSherlock (or ActionBarSherlock + HoloEverywhere + Theme.Sherlock.Light.DarkActionBar ?).
Simple solution and details in the issue thread…
You are trying to use two completely different classes, both named SearchView but in different packages. You cannot simply cast an object from one class into another class. com.actionbarsherlock.widget.Searchview is different than android.widget.SearchView. Therefore, searchView is null on line 269.
It would be relevant for us to see the imports from the top of your Java file.
Probably, the view referenced by R.id.menu_search is an android.widget.SearchView and your code expects it to be a com.actionbarsherlock.widget.Searchview.
Add a theme attribute to your manifest file under the tags of each of the activities that you plan to use an ActionBar. You can create your own custom theme or use one of the default offered by ActionBarSherlock.
<activity
android:name="com.example.MyActivityWithActionBar"
...
android:theme="#style/Theme.Sherlock.Light">
</activity>
More information on:
http://actionbarsherlock.com/theming.html
http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme
I am creating an option menu. it has three option HOME, EMAIL, VISIT. All three options coming at the same row. i have to show HOME option above and rest two below to Home Option(according to attached Screen Shot).
Any Ideas??
Thanks.
Here is my XML file.
<menu android:id="#+id/new_game"
android:icon="#drawable/red_no_bg"
android:title="Home" />
<item android:id="#+id/email"
android:icon="#drawable/red_no_bg"
android:title="Email" />
<item android:id="#+id/help"
android:icon="#drawable/red_no_bg"
android:title="Visit Microsite" />
</menu>
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.overview_menu, menu);
return true;
}
Using the above code i am getting the all option in Same line.
You can Create in two ways
1.Statically.
2.Dynamically.
Am explaining the easiest method of adding menus.
Step 1: Create a new Android Application.
Step 2: After onCreate() method write onCreateOptionMenu();
Step 3: In onCreateOptionMenu() Write these lines
menu.add(11, 21, 1, "Gowtham").setIcon(R.drawable.ic_launcher);
Here "Gowtham" is title of your menu item You can Home&Email use instead of "Gowtham".
Ans use Any images Instead of ic.launcher