I have SearchView in the top of the layout (not in the action bar), is there anyway to force this View to be always expanded (opened)?
If not, i wish to place fancy image near it, is there anyway to make SearchView hide this image when expanded (clicked/expanded)?
You can use the property android:iconifiedByDefault="false" on XML or set programatically setIconifiedByDefault(false). Acording to the documentation this property set the SearchView expanded like you want.
Take a look at SearchView.setIconifiedByDefault(boolean iconified)
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.expandActionView();
and in menu file, use
showAsAction="ifRoom|collapseActionView"
If any query, feel free to comment.
I am doing this into fragment ,
i done it by using
searchItem.expandActionView();
but when user press back button it collapse so in collapse method i used
getActivity().getSupportFragmentManager().popBackStack();
// when it collapsed i am going back
Below is my solution in detail:
search_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/search_contacts"
android:title="search"
android:icon="#drawable/search2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" >
</item>
</menu>
in fragment i used below code :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
// Define the listener
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
getActivity().getSupportFragmentManager().popBackStack();
// Do something when action item collapses
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Do something when expanded
return true; // Return true to expand action view
}
};
MenuItem searchItem = menu.findItem(R.id.search_contacts);
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
// Any other things you have to do when creating the options menu…
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
//searchView.setIconifiedByDefault(false);
searchItem.expandActionView(); // when fragment opens it expanded auto
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
seachViewFunction(newText); // in searchViewFunction(newText); i am doing my things
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
You can try to use
searchView.onActionViewExpanded();
and if you do not need keyboard opened
searchView.clearFocus();
I have a standard search EditText in my ActionBar, triggered by a touch on a little magnifying glass icon. I can cache the user's search string using the OnQueryTextListener. I want to put that string back into the EditText when the user touches the icon a second time.
I'm using ABS (soon to abandon), targeting 8-19.
How can I do this?
With a normal action bar (you'll have to find the different variation with ABS, not sure if my answer will apply completely with that library).
SearchView is a widget that can be inflated in the options menu, so what I do is inflate a menu using an XML containing an action view:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_user"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/magnifing_glass"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search Users"/>
</menu>
Next, when inflating the action view, set your listeners, and use a global variable to save the previous search.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem search = menu.findItem(R.id.search_user);
//Keep a global variable of this so you can set it within the next listener
SearchView user_search = (SearchView) search.getActionView();
user_search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
global_variable = query;
return true;
}
#Override
public boolean onQueryTextChange(String text) {
return true;
}
});
Finally, in a second listener, set the text of the global variable to your previous query when the action view expands.
//This is set on the menu item
search.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
user_search.setQuery(global_variable, false);
return true; // Return true to expand action view
}
});
I want to call an activity after clicking on share icon in action bar. For this I
create a menu named 'flip' which contains an item named 'menu_share' for Android
2.2+. Flip menu is inflated in 1st activity named 'ShareActivity'. After this I want to call other activity after clicking on shareicon in action bar but icon not responding.
Code for menu xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto=" >
<item android:id="#+id/menu_share"
android:title="share"
android:icon="#drawable/shar"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Code for 1st activity.
public class ShareActivity extends ActionBarActivity {
SocialAuthAdapter adapter;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
ActionBar ab=getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
#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.flip, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.menu_share:
startActivity(new Intent(this,ShActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You have set an ActionProvider with yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider". This is likely intercepting the event, thus preventing your Activity from getting the call to onOptionsItemSelected().
Since you want to start your own Activity instead of using Android's default share behavior, simply remove this line.
How to disable/hide three-dot indicator(Option menu indicator) on ICS handsets which does't have menu button. ?
I am running application as <uses-sdk android:minSdkVersion="5"/> in Manifest, code is compiled with 4.0. Three-dot indicator shows on every screen.
Example for preference activities i don't want show Three-dot indicator, since it does't have any menu options.
Adding android:targetSdkVersion="14" in manifest it works. However don't want hide/remove three dots button on all screens . Only in preference activities don't want to show this three dots button.
Override onPrepareOptionsMenu() in the fragment of the preference with this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.menu_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
if you have more then one item set all the items visibility flag to false
and add the command setHasOptionsMenu(true);
to the onCreate command
after you will set all the visibility of the items to false the menu will disappear
on activity, the only difference is the onPrepareOptionsMenu is boolean and you don't need to add the setHasOptionsMenu(true); command on the creation
I just deleted the method :
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
then that three-dot-menu goes away (:
Hope it helps.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
There is no way to show/hide "three-dot" menu indicator for a single activity. You can hide this menu indicator only for entire app by specifying android:targetSdkVersion="14" (or above) in your manifest file.
However, this menu indicator is not showing on preferences activity if it extends from native android.preference.PreferenceActivity class. I have this scenario implemented in a few of my apps, and it works perfectly.
I assume you are using some custom preferences implementations which does not extends from PreferenceActivity. Android Dev Team suggests to always use PreferenceActivity for any preferences in your applications.
Way too late to the party here, I was trying to remove all my menu items and the 3-dots(option menu indicator), I did differently than the solution given here I am surprised that nobody had told it. There is a visibility tag that can be set to false and no changing of code in activity is required visibility=false does the trick
in res / menu /..
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
visibility=false
android:title="#string/action_settings"/>
override method and return false remember of not call super
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
Remove this item in res / menu / main.xml
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
In addition: do not add an item that has showAsAction="never"- this will avoid the dots from showing. If you have more items than can not be shown at once the dots will be there again (and they are items that are flagged ifRoom).
Following code worked for my app. Tried on Samsung Galaxy S4 (Android 4.3) and Nexus 4 (Android 4.2):
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.action_settings);
item.setVisible(false);
return true;
}
for hiding 3 dots in actionbar/ toolbar
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_dash_board_drawer, menu);
return false; //for visible 3 dots change to true, hiding false
}
You can actually change the Android targetVersion thus forcing the 3-dot menu to either hide or show. You need to override onCreate of your Activity like this :
#Override
public void onCreate(Bundle savedInstanceState) {
getApplicationInfo().targetSdkVersion = 10; // To enable the 3-dot menu call this code before super.OnCreate
super.onCreate(savedInstanceState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
getApplicationInfo().targetSdkVersion = 14; // To disable the 3-dot menu call this code before super.OnCreate
super.onCreate(savedInstanceState);
}
Tested on Android 4.x.x and Android 3.0
I just excluded the "onCreateOptionsMenu"-method:
#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_planos, menu);
return true;
}
If you simply want to hide the button, this solution is a bit of a hack but works across all versions of Android (using AppCompat) and doesn't affect your other menu items:
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
...
<item name="android:actionOverflowButtonStyle">#style/AppTheme.Overflow</item>
<!-- If you're using AppCompat, instead use -->
<item name="actionOverflowButtonStyle">#style/AppTheme.Overflow</item>
</style>
<style name="AppTheme" />
<style name="AppTheme.Overflow">
<item name="android:src">#null</item>
</style>
If you want the Overflow button hidden only on some screens, you could make this an alternate theme (change AppTheme above to AppTheme.NoOverflow) that only certain activities use :
AndroidManifest.xml
<activity android:name=".NoOverflowActivity"
android:theme="#style/AppTheme.NoOverflow" >
This effectively just makes the icon have no width and height. I rarely recommend opposing design guidelines but in my scenario we used dedicated hardware that did not properly report a menu button was present.
Just want to improve #war_hero answer.
If You wanna set the visibility on run time You can use oncreateoptions menu parameter like this
Menu overflow;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
this.overflow = menu;
return super.onCreateOptionsMenu(menu);
}
and then make a function to show or hide any menu item according to the index. for e.g.
public void hideorShowMenuItems(boolean bool){
overflow.getItem(1).setVisible(bool);
overflow.getItem(2).setVisible(bool);
overflow.getItem(3).setVisible(bool);
overflow.getItem(4).setVisible(bool);
overflow.getItem(5).setVisible(bool);
}
Copy paste this code in main.xml in menu folder
you just need to make the item android:visible="false"
<?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_settings"
android:orderInCategory="100"
android:visible="false"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Should be:
<item
android:id="#+id/linearlayout_splash"
android:orderInCategory="100"
android:showAsAction="never"
android:visible="false"
android:title="#string/action_settings"/>
If MainActivity is
public class MainActivity extends AppCompatActivity
In MainActivity Class, Remove the below code.
#Override
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);
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);
}
Do it like this.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(leftDrawer);
return (!drawerOpen);
}
I am checking if my navigation drawer is visible I will hide the menu and vice versa. You can use it according to you requirement. Hope this helps. Happy Coding. :)
This is how I find a solution of mine. It maybe helpful.
#Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
//KEYCODE_MENU
if(keycode == KeyEvent.KEYCODE_MENU){
/* AlertDialog.Builder dialogBuilder
= new AlertDialog.Builder(this)
.setMessage("Test")
.setTitle("Menu dialog");
dialogBuilder.create().show();*/
return true;
// finish();
}
return super.onKeyDown(keycode,event);
}
I just used
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Where R.menu.main is only an empty menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" ></menu>
Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/hello"
android:title="Hello"
android:icon="#drawable/icon"/>
<item android:id="#+id/bye"
android:title="Bye" />
</menu>
Thanks !
This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.