Adding separators between groups of actionbar menu items - java

I'm creating an android action bar menu for an app. What I'm looking for is a way to show the logical groupings of menu items. So what I have currently is:
Whereas what I'd like is:
I'm creating the menu as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu){
//Note, on 2.3.x - 3.0 this is called when user opens the menu for the first timed
//on 3.0+ it is called when the activity starts
MenuInflater inflater = getMenuInflater();
//adds the (currently empty) menu, after login it will be filled with options
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.add("example");
menu.add("example");
return true;
}
Menu XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto"
>
</menu>
I had hoped menu groups would help, but I understand that they are just for modifying all the menu items properties together. Equally, submenu actually creates a hidden menu that is opened free-floating when clicked.
Is there any way to create this logical grouping of menu items that I'm looking for?

Related

Android search view not behaving right on toolbar

there you have the video that shows how my app actually behaves:
https://www.youtube.com/watch?v=81XIdEo0lks&feature=youtu.be
You see that when I click the search icon, it just moves it to the left side, then I need to click it again, then i need to click the x button 2 times before I actually get out of the search view, a complete mess.
Here's how I want my search view to behave:
https://www.youtube.com/watch?v=sJ-Z9G0SDhc&t
MainActivity.java (only the relevant part)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.app_bar_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
options_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/app_bar_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/logout_btn"
android:title="Log Out" />
</menu>
I want my search view to behave like the search view in that video, click on the search, the keyboard appears, you can type, you can exit. I also want to make this 3 vertical dots icon disappear when I enter the search view.
First declare a state variable to determine when menu item(s) needs to be hidden
boolean isHideMenuState = false;
Then reference the item(s) needed to be hidden inside onCreateOptionsMenu block
MenuItem logoutItem = menu.findItem(R.id.logout_btn);
//... initialize other menu items here
//use hidden state to define when to hide item(s)
if(isHideMenuState){
logoutItem.setVisible(false);
//by now the ellipses(...) should go off
//if not try using setShowAsAction passing (SHOW_AS_ACTION_NEVER)
}
else{
logoutItem.setVisible(true);
}
Next: Add focus change listener to your searchView
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Toggle isHideMenuState when search widget has focus/no focus
isHideMenuState = hasFocus
//Tell menu to re-create itself
invalidateOptionsMenu(); // onCreateOptionsMenu(...) is called again
}
});
And lastly for the UI try changing the actionViewClass of your searchView item
app:actionViewClass="android.support.v7.widget.SearchView"
click on the search, the keyboard appears, you can type, you can exit.
In order to do that, you need to do this:
in onCreateOptionsMenu:
add searchView.setIconifiedByDefault(false);
add
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchView.setIconified(false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
searchView.setIconified(true);
searchView.clearFocus();
return true;
}
});
The purpose of searchView.setIconifiedByDefault(false); is to show the expanded view of SearchView by default when user pressed the search icon. The searchView.setIconified(false); is to let the view has focus and show the soft-keyboard. The searchView.setIconified(true); is to clear the text when your user exit from the SearchView. Lastly, the searchView.clearFocus(); is to hide the soft-keyboard.
I also want to make this 3 vertical dots icon disappear when I enter the search view.
For this, you can do this:
add logoutItem.setVisible(false); in onMenuItemActionExpand
add logoutItem.setVisible(true); in onMenuItemActionCollapse

My app has an action bar, but no menu.xml. How do I modify my action bar?

Hello and thanks for reading,
When I first made my project, I was prompted by Android Studio to chose a boiler plate. I chose empty activity (the one without FAB and others). Still, my app has an ActionBar, but it just shows the name. Now, I want to modify that action bar and add a menu. My java extends AppCompatActivity, so there is an action bar. However, unlike my prior experiences in eclipse, there is no menu xml to that I can locate.
How can I add one or modify my action by through other means? Can I add one manually?
Thanks!
1) You need to create (or modify if it exist) your menu resources file, /res/menu/main_menu.xml to create the actions.
eg:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:icon="#drawable/ic_action_setting"
android:title="Settings">
</item>
</menu>
2) Override onCreateOptionsMenu() in your activity to allows to inflate actions defined in your XML:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
3) Override onOptionsItemSelected() to react the actions selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

Whenever I try to add a button to my actionbar, the items are added under the overflow menu

I am trying to add a simple button to my action bar, but when I try to use the following code, the items are added as items under the overflow menu, not on the actionbar itself. Any help would be appreciated,
Jacob
Here is my Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_review, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/seda"
android:icon="#drawable/redpinreal"
android:title="Hello Worldh"
android:showAsAction="withText|always"/>
</menu>
I also have this enabled:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Part of your code (android:showAsAction, instead of yourapp:showAsAction) is attempting to use the native action bar. Part of your code (getSupportActionBar(), instead of getActionBar()) is attempting to use the appcompat-v7 action bar backport. Choose one and stick with it.

Action Bar Buttons only for one activity

So I have a Search Button in my Action Bar, But I only want that button in the first Activity, but when I open the seconde activity, the Action button is still there. How to get rid of that button in the seconde activity without removing the action bar, because I nee it from my Navigation Drawer.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Activity1:
#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);
}
Activity2:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//I thought removing the INFLATER part would help, but it didn't...
return super.onCreateOptionsMenu(menu);
}
You can try hiding the search menu item in your second activity.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_search).setVisible(false);
return true;
}
You can refer to this question with multiple answers.
You can make the Item in your 2nd Activity invisible like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_search);
item.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Another approach would be to load a second menu.xml with no item in it.
if you return false it will not be shown. as android docment saying http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}

Add ActionBar items on my MasterDetailFlow?

My main activity on my Android app is a MasterDetailFlow activity, and I don't know how to add items to the ActionBar. I've used this code from the developer website:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:showAsAction="ifRoom|withText" />
</menu>
My XML files in res/layout are as follows:
activity_item_detail.xml
activity_item_list.xml
activity_item_twopane.xml
fragment_item_detail.xml
When I add the above XML code to any of them I get an error: The markup in the document following the root element must be well-formed.
None of the files start with <?xml version="1.0" encoding="utf-8"?>
You need to create the menu in a separate XML file and then inflate it. The recommended way to do it is to create a folder 'menu' and place the menu file inside it.
Now you need to create an OptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.myMenu, menu); // inflating the menu
return super.onCreateOptionsMenu(menu);
}
Create an onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.menu_settings:
// do whatever you want
break;
}
return super.onOptionsItemSelected(item);
}

Categories