I have just starting using an ActionBar, everything is well apart from the search box. I am now wanting to use a SearchView but have found it to be hard to customize, is there a way to use my existing EditText based layout in place of the expandable SearchView?
The top one is the new SearchView and the bottom is my old one which I would like to use, I am not worried about any extras like autocomplete.
I have looked on google for some time but cannot find any solution, my XML for the menu looks like so :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/btnSearch"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:queryHint="blah blah"/>
...
</menu>
I have found that I can get the bottom line to look similar to my old search box by using some ugly looking reflection code, however it still does not look right, the padding etc is all wrong. I don't want any reflection, I just want to use my old layout.
I found something that could solve your problem:
http://www.techrepublic.com/blog/software-engineer/pro-tip-customize-the-android-search-view-widget/#.
I wrote this class to help with SearchView customization:
https://gist.github.com/jaredrummler/c408c9d897fd92d5d116
You could do something like this after inflating the menu in onCreateOptionsMenu:
SearchViewStyle.on(menu, R.id.your_search_id)
.setSearchPlateDrawableId(R.drawable.your_edit_text);
Related
I want my menu items on the BottomNavigationBar to have text-only labels with no icon. Unfortunately, it looks like design_bottom_navigation_item.xml always has a 24x24dp space reserved for the icon, with no publicly-exposed way to set it to gone. And even after getting past that, the label layout is set to a layout_gravity of bottom|center_horizontal, and it doesn't look like there's a way to programatically set layout_gravity to centered.
What is the fastest, easiest way to achieve the goal of a text-only menu item on the bottom nav bar? I'm thinking I can do this by creating a custom version of the item layout, then subclassing BottomNavigationItemView, BottomNavigationMenuView, and BottomNavigationView to specify that custom layout... but that seems like an awful lot of work for one little change. Am I missing something simpler?
dont put iandroid:icon property to your item
Just use the code below. I tried hard to do this easy way but failed.Then I made an alternative. Just use transparent color instead of icon drawble
<item
android:icon="#android:color/transparent"
android:id="#+id/navigation_id"
android:title="title" />
Add this in your dimens file. Then you can add padding according to your navigation view size.
<dimen name="design_bottom_navigation_height"tools:override="true">30dp</dimen>
In my app I want to create a searchview within an actionbar. I've used the following code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/autocomplete_search"
android:icon="#drawable/search_light"
android:title="#string/what_form_edit_text"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" /></menu>
With this code a search icons shows up in my activity. After clicking this icon the search view expands and aligns on the left side of my actionbar (the title is gone).
Now I want to achieve this behaviour when automatically expanding the searchview. I tried to change the values of showAsAction into "always" instead of "ifRoom|collapseActionView", but it's not working. The searchview gets expanded, but is aligned on the right side of the actionbar. Is there any way I am missing to align the searchview on the left side (without implementing my own toolbar and place the searchview within?). Thanks for any help :)
Try to use
app:showAsAction="ifRoom"
It should fix your issue.
I'm trying to implement search interface in actionbar like in Google Play Video App (https://play.google.com/store/apps/details?id=com.google.android.videos).
I think it is SearchView widget but don't understand how do same customization: add up button inside it, set background, and expand the full width of actionbar
#Quiny898 create a nice lib, have a look:
https://github.com/Quinny898/PersistentSearch
Use a standalone Toolbar and set it with an elevation, then inflate your menu in it.
You can add this item to your menu:
<item android:id="#+id/menu_search"
android:title="#string/action_search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
pawesome:showAsAction="ifRoom|collapseActionView"
pawesome:actionViewClass="android.support.v7.widget.SearchView"/>
I'm using not bad library for this: https://github.com/MiguelCatalan/MaterialSearchView
I have the following menu xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_select_appshare"
android:showAsAction="always"
android:title="#string/action_select_appshare"
android:actionViewClass="android.widget.Spinner" />
</menu>
Because a Spinner seem to require the definition of the android:spinnerMode attribute from the get-go, and there doesn't seem to be a proper way to set it later on after the Spinner object is created. I was wondering if there's a way to specify additional attributes (such as android:spinnerMode in my e.g.) for an android:actionViewClass within the menu XML?
Thanks!
Finally found it:
Related links: 1, 2 and 3
Create a new res/layout file. Put the spinner in it, with all the properties you need/want. Note that the Spinner should be the root view.
In your menu file containing the actionClassProvider=spinner, remove actionClassProvider, and replace it with actionLayout="#layout/my_spinner"
All Done
I hope this helps somebody!
well you could solve it in 2 ways: create a custom spinner and put what you want there, or, get the actionView when inflating this menuItem, and then do what you want with it.
I'm attempting to implement a refresh button in my app to allow a user to manually re-sync to a web server. The code works, but I'm having trouble figuring out the action views (at least, I think that's what I'm supposed to be using).
My menu item is here:
<item
android:id="#+id/main_menu_refresh"
android:enabled="true"
android:icon="#drawable/refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/refresh"
android:actionViewClass="android.widget.ProgressBar">
</item>
The problem is, it always shows the ProgressBar. I wondered if it worked like the search widget (the only example I really see online) and added the collapseActionView tag to the showAsAction and that prevented it from showing up immediately. However, when I click the refresh button, the icon disappears (good), but so does the Title in the action bar, and the ProgressBar appears on the left side of the window where the title used to be. Also not what I wanted.
As something of a last ditch effort, I attempted to add this to my code, and remove the actionViewClass from the XML:
MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
Log.w("MyApp", "Have Menu");
ProgressBar pb = new ProgressBar(ReadingList.this);
refresh.setActionView(pb);
That didn't work either, giving me a null pointer error on setActionView.
I need a solution that I can call from any function (there is an auto-sync period at the beginning I would like the ProgressBar to display during as well), and be able to return it to it's static icon after.
I tried reading through this question, but I am having trouble understanding what the answer means. I feel like I was trying to do just what it says, but I guess not. Any assistance is much appreciated.
Edit: For sastraxi's suggestion.
public class IconSwitcher extends LinearLayout{
public IconSwitcher(Context context) {
super(context);
ProgressBar pb = new ProgressBar(context);
ImageView iv = new ImageView(context);
addView(iv);
addView(pb);
}
}
This is my class thus far. However, when I try and reference it with:
MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
IconSwitcher ic = (IconSwitcher) refresh.getActionView();
I get a null pointer error. on creating the IconSwitcher. The button XML is as follows:
<item
android:id="#+id/main_menu_refresh"
android:enabled="true"
android:icon="#drawable/refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/refresh"
android:actionViewClass="IconSwitcher">
</item>
I'm just having a difficult time on referencing that IconSwitcher View.
Edit 2: I'm appearantly having trouble referencing the Menu Item at all.
MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
refresh.setVisible(false);
Also gives me a null pointer when I try and set the visibility. What is wrong with my references?
Instead of setting your action view class to a ProgressBar, set it to a custom LinearLayout class, as such:
class MyViewItem extends LinearLayout
Add a ProgressBar and an ImageView as children in its constructor with addView, and set these childrens' visibilities as either GONE or VISIBLE when your code requires it.
Try to use:
<item
android:id="#+id/main_menu_refresh"
android:enabled="true"
android:icon="#drawable/refresh"
android:orderInCategory="1"
android:title="#string/refresh"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.ProgressBar">
</item>
Note this two lines:
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.ProgressBar"
Did you try to fetch your menu item like this?
MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);