SearchView doesn't appear in Toolbar - Android Lollipop - android

I have in the following Activity this Toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_select_currency"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
Also the menu is declared in:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_currency"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
In the activity I call:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.currency_selector_options_menu, menu);
return true;
}
private void initActionBar() {
// Initialize the Toolbar( this is the new ActionBar in Lollipop)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_select_currency);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.select_currency));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Now my issue is that I do not get the SearchView like in normal ActionBar, what I get is a normal menu. How can I get a normal SearchView ?

Use your app namespace:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
myapp:showAsAction="always"
Explanation:
Since you are using a support library for old android versions and it is not included into the sdk, the android namespace doesn't contains the showAsAction attribute, which is rather contained into the support library you linked to your project, so to reach the attribute you must use your app namespace

Related

Menu item always goes in the overflow menu in Android

I have a menu icon for my ActionBar, but it always goes into the overflow menu no matter what I do. I want it to show up as an icon and not go into the overflow menu.
Here is my menu_daily_selfie.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/camera_button"
android:icon="#android:drawable/ic_menu_camera"
android:title="Camera"
app:showAsAction="always"/>
</menu>
I'm using the appcompat library. What gives? Seems like this should work.
Here is my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_daily_selfie, menu);
return true;
}
My main activity extends ListActivity since it uses a ListView
Then you are not using appcompat-v7 properly. Either:
Switch to inheriting from AppCompatActivity, and manage your own ListView, or
Stop using appcompat-v7, and switch your menu resource to use android:showAsAction instead of app:showAsAction

Android Toolbar in webapp

I am developing a web app, and here I have a problem.
I have a tool bar(android widget toolbar) with logo and search button
As you can see I have the login page(webview). I want to user to see this search button after users login to the webpage. How should I do that?
Edited:
In my toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000000"
android:elevation="3dp"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In my 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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
And in MainActivity.java onCreate,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//toolbar.setNavigationIcon(R.mipmap.logo9);
toolbar.setTitle("");
toolbar.setSubtitle("");
......
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
And finally in activity_main.xml, I just include the toolbar,
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
If the search button is an Android View, you can hide it by calling View.setVisibility(View.GONE).
If you have instead defined it as a menu XML resource and loaded it in this Activity, then just don't load the menu resource unless the user is logged in. That is done in public boolean onCreateOptionsMenu(Menu menu) of the Activity.
If you go to another Activity after logging in, then just use a separate Toolbar there, with the icon like here, but without one in the login Activity.
Please provide more information on how you added the search icon to the Toolbar in the first place, then I will be able to help you more.
Edit:
You can use the WebView.getUrl() method to get the URL of the displayed website, if your login page is on login.php, you can look if the URL is that, and then not show the search icon. For example you can check the URL with String.contains(), like: .contains("login.php"). And if that is true don't show the button.
In your styles for this activity use this attribute in your style for this activity
<item name="android:windowNoTitle">true</item>

How to get the following transition/animation effect in App Bar dropdown menu?

I'm developing a material design app & I want this effect when I click on the menu option or the three dots on the side of ActionBar/Toolbar.
The effect I want is at the last of 'usage' section & just above the 'menu items' section. It is labeled as 'App bar dropdown'.
The 'three dots' is commonly called the 'overflow' menu on Android, and knowing the terminology will improve your search results.
CodePath has an example on using the Toolbar as ActionBar. It will be helpful to read the whole page, because it explains the history of both Toolbar and ActionBar. The section Using toolbar as ActionBar is particularly relevant to your question.
In your MainActivity onCreate method:
// Set a ToolBar to replace the ActionBar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Your MainActivity will override the onCreateOptionsMenu and inflate your menu resource file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
And you will define a menu resource file:
/res/menu/menu_main.xml
<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=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
The 'showAsAction' attribute tells Android to only display this menu item in the overflow menu (never as an action on the toolbar).

Item with app:showAsAction not showing

I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
android:showAsAction="always" />
</menu>
works perfect, but proper and compatible version like
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
app:showAsAction="always" />
</menu>
not showing my icon at all.
I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)
Things you should always check when you want to use action bar are
1) Extend ActionBarActivity instead of Activity
public class MainMenu extends ActionBarActivity{
2) Have the right style selected as defined at manifest
Manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Style
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) Select the right title for showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
**yourapp**:showAsAction="ifRoom" />
...
</menu>
This is what most people get wrong
4) Define your Menu in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
If you do all the following your action bar should work.
Then you should add the onClickListener for every position...
I just reread your question and saw your problem is the complete opposite (but some pieces of my old answer still apply to your problem), so here is the updated answer:
Update:
You have imported the appcompat library in your gradle file, but you seem to support only devices newer than API Level 11 or 14? If this is the case, the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity because of your library import. That's why you are getting the error. But as your android:showAsAction attribute is working, you are using the native Activity and the native attribute call is correct, even if the lint check says it is wrong. So if you want to remove the lint error, you have to delete the appcompat lib from your gradle file and maybe change your activity theme to the native Holo Light theme, as your current theme might rely on the appcompat theme.
The answer why it isn't working with the app namespace is in the XML attribute loading for native respectively library code, which is handled in the old answer.
Old Answer
If you are using the ActionBarActivity from the support library to reach devices lower than API level 11, the main issue here is, that the ActionBarActivity recreates some of the native Android XML attributes such as android:showAsActionin its own scope, which you define with:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then access them with the same attribute (here showAsAction) in the app: namespace.
So the ActionBarActivity can't see or reach the native android:showAsAction attribute as it is only looking for it in the app namespace and not the android namespace.
If you want to use the native attribute, you have to use the native Activity with a Holo Theme, which is only supported from API Level 11 and higher.
addthis:
yourapp:showAsAction="ifRoom"
or
android:showAsAction
for example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
</menu>
and in Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And read more here

SearchView in OptionsMenu not full width

I have a working SearchView which expands in my OptionsMenu when the user taps on the search icon. However it only expands within the available space among the other OptionsMenu icons. On a wide screen this is fine, but with a narrow space there is only room to show 5-10 charaters in the search box. I want it to overlay the other icons such as it does for the Android Contacts app. Currently, I'm building with targetSdkVersion = 17. Hopefully I'm missing something simple :)
(Note added later: the only solution I've found workable so far is to hide all the menu icons when I want to expand the search icon. This is conceptually simple. But it is messy because when restoring hidden icons, one has to go through a bunch of logic to figure out which ones to restore, or keep state variables around, etc.)
Here's my item xml in for the OptionsMenu:
<item
android:id="#+id/menu_search_shallow"
android:title="Search Current Folder"
android:icon="#drawable/ic_btn_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
I also have in my main activity code:
#Override
public boolean onCreateOptionsMenu (Menu menu)
{
getMenuInflater().inflate(R.menu.nav_menu, menu);
this.optionsMenu = menu;
MenuItem searchItem = menu.findItem (R.id.menu_search_shallow);
searchItem.setOnActionExpandListener (this);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_shallow_hint));
searchItem = menu.findItem (R.id.menu_search_deep);
searchItem.setOnActionExpandListener (this);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_deep_hint));
}
and
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener (this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setQuery ("", false);
return true;
}
Instead of creating a new layout I set the MaxWidth programmatically in onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
....
For some reason jjung's solution didn't work for me. I had to add android:maxWidth="10000dp", just a really high maxWidth, to get it to expand to the full available width.
Also, just to add, I'm using the support library (v7), and so my layout file looks like this:
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="10000dp" />
And in my item, I have:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
...
myapp:actionLayout="#layout/searchview" />
...
Where myapp is just some arbitrary namespace for the attributes added by the support library (see Adding Action Items for more details).
Maybe I am late but thought it might be helpful for someone who faced the same issue as me.
final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);//set search menu as full width
in onCreateOptionsMenu
of your class may solve this problem but one thing we need to understand.
If you just want to show search menu (on clicking of search button) and not any other menu items in actionbar then you need to keep the search item as a first item in menu file. Here is an example
<?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">
<!--keeping searach as first item-->
<item
android:id="#+id/action_search"
android:icon="#drawable/search_ab"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
<item
android:id="#+id/itemFilter"
android:icon="#drawable/filter"
android:title="#string/filter"
app:showAsAction="ifRoom" />
</menu>
And it will be displayed as
on clicking search it will be shown as full width
And if you keep search as second or last item in menu then it will be show like this.
and on clicking search
As crazy as it might seem, in my case problem was this line:
app:actionViewClass="android.widget.SearchView" in menu.xml:
<item
android:id="#+id/action_search"
app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:title="#string/title"
app:actionLayout="#layout/layout_search"
app:showAsAction="always" />
When I removed app:actionViewClass, it fully expanded as defined in my layout_search file.
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="10000dp"
android:layout_centerInParent="true"
android:background="#color/colorAccent"
android:queryHint="Search me" />
I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons).
I solved it by adding in item:
android:actionLayout="#layout/my_search_view"
and in layout/my_search_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
There are so many ways to solve this issue - I'll just add the one I've used in several apps.
I have my custom SearchView (with some other customization) which extends android.support.v7.widget.SearchView and in order to stretch to full width:
#Override
public void setLayoutParams(final ViewGroup.LayoutParams params) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
super.setLayoutParams(params);
}

Categories

Resources