This question already has answers here:
Actionbar not shown with AppCompat
(3 answers)
Closed 9 years ago.
I am using appcompat in my app. I want the menu items to show on actionbar or at least the overflow(3 dots) to show them when there is no room. There is lot of space on the actionbar, but still they don't show up. The menu flow raises from the bottom and that too only when menu button is pressed.
menu_activity.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_lang"
android:showAsAction="always"
android:title="#string/menu_lang"
android:icon="#android:drawable/ic_input_lang"/>
</menu>
activity:
#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_activity, menu);
return true;
}
This post says that it not works when hardware menu button is present. But other apps are able to show items on the same device. So, that answer seems to be incorrect. Can someone please help on this?
You seem to be using the wrong menu:
Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu
The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity
Try it with this again - I ran into this too once and it drove me nuts...
Update
After clarification that the above part was for obfuscation, please make sure that:
You extend ActionBarActivity.
You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the showAsAction attribute, so that the compatibility library can pick them up.
You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction tag to app:showAsAction according to the Android guide.
Your corrected menu_activity.xml would then look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_lang"
app:showAsAction="always"
android:title="#string/menu_lang"
android:icon="#android:drawable/ic_input_lang"/>
</menu>
The extended Android guide for actionbar covers these new and nasty traps...
Okay I think I found a solution for you. It is called Overflow Menu and you need to call the below method in your onCreate method.
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this and let me know if it works for you.
EDIT:
I think I understood your problem finally. I can give you one idea. If you do not want to use overflow menu and just display menu items as displayed in the screen-shot you have posted, you can create a layout on top of your activity.
You can take a linear layout, give a background that looks the same as action bar and place whatever icons you want to put there, and give functionality to them with onClickListener. Hope this will give you some idea. This way you can create your own custom menu. Try the layout below and replace ic_launcher drawable with menu icons. Then just set onClickListeners to them and perform whatever functions you want to perform inside onClick method.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#66000000" >
<ImageView
android:id="#+id/xMenuBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#drawable/ic_launcher" />
<TextView
android:id="#+id/xMenuTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chats"
android:textSize="18sp"
android:layout_toRightOf="#+id/xMenuBtn1"
android:layout_centerVertical="true" />
<ImageView
android:id="#+id/xMenuBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/xMenuBtn3"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/xMenuBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
Try in your activity onCreate() :
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);
and then :
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getSupportMenuInflater().inflate(R.menu.menu_activity, menu);
return super.onPrepareOptionsMenu(menu);
}
Related
I want to provide a numbered month's list dropdown with calendar icon click on the app toolbar. I am having two issues/questions to implement this.
Unable to place calendar, icon with out disturbing the title (NavHost traversed fragment names).
Don't know how to show a clickable drop down list with some options on that icon
With the help of internet, I have implemented single activity and multi fragment (Jetpack navigation) architecture app as shown in below image.
Below is the code I have tried to implement this:
And here is App screenshots with different behaviors
When I uncomment above code (21 to 35), I see 1 and 3 of below images and If I comment that ou, I see 2 and 4.
For the first issue, is it just a UI trick to update the RelativeLayout width to warp_content of child ? or any other way to achieve this properly.
For the second issue, I didn't find a one step or atleast two step solution. Every where it shows Navigation menu option, when I try for "Android toolbar clickable dropdown list".
My Final goal: Show a calendar icon with months list click able dropdown (1 month, 3 months, 6 months, 9months ... e.t.c) on the right side of the toolbar when user lands on Invoices page for filtering
Make this in xml layout file:
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/app_bar"
style="#style/MyTheme.ToolbarStyle"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:paddingStart="12dp"
android:paddingLeft="12dp"
android:paddingEnd="12dp"
android:paddingRight="12dp"
app:title="#string/app_name"/>
<ImageView
android:id="#+id/yourImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_your_icon" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
You need use popup menu. Make xml file for menu. For example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/month1"
android:title="1month" />
<item
android:id="#+id/months3"
android:title="3months" />
<item
android:id="#+id/months6"
android:title="6months" />
<item
android:id="#+id/months9"
android:title="9months" />
</menu>
And than use menu in activity. Create instance of popup menu, set listener for it and set listener for youe image. Write this in onCreate function:
ImageView image = findViewById(R.id.yourImageView);
PopupMenu popupMenu = new PopupMenu(this, image);
popupMenu.getMenuInflater().inflate(R.menu.menu_your_name, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
//Do what you want
return true;
}
});
image.setOnClickListener(view->{
popupMenu.show();
});
May be this is not the most optimal way, but it must be work as you want.
I found the answer, for my multiple fragment and single activity architecture ("Jetpack navigation") related menu/custom action icons in toolbar
Note: Below 1,2,3 helps to solve above problem statement 1 and the remaining helps 2
Create another menu file with required content.
Populate it with `onCreateMenuOptions in the desired fragment
(Note: Include setHasOptionsMenu(true) in the onCreate)
If you inflate a menu in this onCreateMenuOptions, you new menu will be appended to the already existing one of parent/main activity inflated.
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.invoice_list_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
Create a sub menu. in the menu item with radio checkable group. See the last screenshot for example
override onOptionsItemSelected(item: MenuItem) to capture the menu clicks. Make sure you are updating the checkable status of the radio item.
Note: Make sure you are using the style="#style/Widget.MaterialComponents.Toolbar.Primary" from above 2nd link, to get the proper white overlayed nice action bar. Otherwise you will end up having some dark text colored action bar with out known why
Below links are helpful to understand more:
Android developers docs Menu (To understand things inside menu)
Android material menu action bar implementation (To create a proper action bar with material design compatible)
Extended ActionBar guide (To add search to toolbar and create collapsable toolbar )
Android Jetpack Navigation example contained menu usage
Below screenshot is the final output and code reference
I am new on android.
What I want to do is, when I press the icon on Actionbar the LinearLayout is showing.
Here's the picture
As you can see, there's a magnifier icon.
when I press that icon, the search bar below it will gone.
And when i press the icon again, it'll show again.
I already trying use setVisibility, but its show error.
Here's my Java code
MenuItem searchBar;
searchBar = (MenuItem) findViewById(R.id.search);
searchBar.setVisibility(View.GONE);
}
public void toggle_contents(View v){
if(searchBar.isShown()){
Fx.slide_up(this, searchBar);
searchBar.setVisibility(View.GONE);
}
else{
searchBar.setVisibility(View.VISIBLE);
Fx.slide_down(this, searchBar);
}
}
And Here's my XML code
<LinearLayout
android:id="#+id/search"
android:background="#color/gray74"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="7dip"
android:paddingBottom="7dip" >
<EditText
android:id="#+id/search_global"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/searchbackground"
android:text="Search"
android:textColor="#color/gray74" />
</LinearLayout>
Thanks before
First: A MenuItem is not a View and consequently it's not part of your layout. Therefore you shouldn't be using:
MenuItem searchBar = (MenuItem) findViewById(R.id.search);
But instead:
MenuItem searchBar = menu.findItem(R.id.search);
Second: When you make changes to your menu, you should call invalidateOptionsMenu().
By doing this you basically tell the menu that some changes have been made to it. This will make the menu recreate itself and request that the system call onPrepareOptionsMenu().
I have an action sherlock bar and I need customize this control as the following picture. I need a pseudocode or similar simple example. I downloaded the library, examples, and I have been searching in this site and google, but I could not find something like this.
Sorry if the information is incomplete, in that case I'll edit the post with the information you need. Thanks.
Note:
I do not want to use android.support.v4.widget.DrawerLayout, ActionBarDrawerToggle. I need the sherlock slide effect.
I need a image and label above the list of menu options (LISTVIEW) (see the right image).
In the future I'm going to need a bar like below (the style must be similar to see this link)
SidingMenu layout:
You should set a ListView as menu layout. Next, create sliding_menu_header.xml layout with ImageView and TextView inside:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:src="#drawable/image_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Now, you can inflate this layout and set it as ListView's header view:
View menuHeader = getLayoutInflater().inflate(R.layout.sliding_menu_header, null);
mListView.addHeaderView(menuHeader);
mListView.setAdapter(mYourMenuAdapter);
ActionBar layout:
You can add a custom view to the ActionBar. In your case the layout can be like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:text="button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button2"
android:text="button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then add below lines to your onCreate method:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_view);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
Button button1 = actionBar.getCustomView.findViewById(R.id.button1);
Button button2 = actionBar.getCustomView.findViewById(R.id.button2);
Hope it will be helpful to you. If my answer is incomplete please comment and I'll update it.
Implement "Sliding Menu" it's perfect with Sherlock ActionBar
Link library
https://github.com/jfeinstein10/SlidingMenu
VideoTutorial for implement two libraries
https://www.youtube.com/watch?v=IW6idyV0CZQ
EDIT
//pre-ICS
if (actionBarSherlock instanceof ActionBarImpl) {
enableEmbeddedTabs(actionBarSherlock);
//ICS and forward
} else if (actionBarSherlock instanceof ActionBarWrapper) {
try {
Field actionBarField = actionBarSherlock.getClass().getDeclaredField("mActionBar");
actionBarField.setAccessible(true);
enableEmbeddedTabs(actionBarField.get(actionBarSherlock));
} catch (Exception e) {
Log.e(TAG, "Error enabling embedded tabs", e);
}
}
//helper method
private void enableEmbeddedTabs(Object actionBar) {
try {
Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
setHasEmbeddedTabsMethod.setAccessible(true);
setHasEmbeddedTabsMethod.invoke(actionBar, true);
} catch (Exception e) {
Log.e(TAG, "Error marking actionbar embedded", e);
}
}
look that link:
https://groups.google.com/forum/#!topic/actionbarsherlock/hmmB1JqDeCk
When you had tabs on your action bar you can asing one style for your tabs, border, backgroundImage, gravity and you give button appearance.
Look:
http://www.silverbaytech.com/2013/05/13/themes-for-the-android-actionbar-tabs/
Try it and tell us.
From your picture it seams you want to scroll the actionbar too. That's not possible (to my knowledge). For something like that to work you have to make your custom actionbar (as a ordinary layout) and handle the visibility change and scroll by yourself. You can also check actionbar_compat (appcompat_v7). if basically offers the same functionality as ActionbarSherlock and SlidingMenu combined. http://android-developers.blogspot.ro/2013/08/actionbarcompat-and-io-2013-app-source.html. And the menu can contain heterogenous items, if that's your concern.
I use ActionBarSherlock, in which I set the navigation mode to 'list'
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, this);
Is it possible to dynamically show a second spinner, depending on which item is selected?
Use a custom action bar layout.
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar_custom);
action_bar_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/action_bar_spinner_collection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
<Spinner
android:id="#+id/action_bar_spinner_collection_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</LinearLayout>
Use this one..This may works...
Code:
getMenuInflater().inflate( R.menu.main, menu );
mSpinnerItem = menu.findItem( R.id.menu_spinner );
setupSpinner( mSpinnerItem );
Menu XML:
<item
android:id="#+id/menu_spinner"
android:actionViewClass="android.widget.Spinner"
android:visible="false"
android:showAsAction="always"/>
NEVER use NAVIGATION_MODE_LIST and onNavigationItemSelected it is not worth it !
#Override
public boolean onNavigationItemSelected(int position, long itemId)
You also cannot use menu's to do this:
#Override public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
due to inflation ordering.
Reasons:
(1) it generates a "hidden" spinner, for which you can not get the id.
(2) you cannot customize this spinner
(3) you save 30 lines of code, but are permanently limited if you want to add a second bi-directional spinner
(4) not even in the special case of "simple code" (one spinner), you lose to much.
(5) you cannot use tabs.
the key is actionBar.setCustomView(R.layout.action_bar_custom);
and spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()...
for each spinner.
Trust me I lost hours trying each solution.
Let me give you a brief scenario what is happening.
I want to have a refresh button on my action bar which switches to the indeterminateProgressStyle (a rotating circle) while somethings are loading.
In my project i have the following layouts. (e.g. res/layout/actionbar_indeterminate_progress.xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ProgressBar android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_gravity="center"
style="?android:attr/indeterminateProgressStyle" />
</FrameLayout>
and when the user clicks on the refresh button I do the following.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
refreshMenuItem = menu.findItem(R.id.menu_refresh);
if (refreshMenuItem != null) {
// Show refresh button if we are in action bar mode, but only if
// there's something to be refreshed
boolean selectedItemExists = (getCurrentBrowsingContainer() != null);
refreshMenuItem.setVisible(selectedItemExists);
if (is_spinner_running) {
refreshMenuItem.setActionView(R.layout.actionbar_indeterminate_progress);
} else {
refreshMenuItem.setActionView(null);
}
}
return super.onCreateOptionsMenu(menu);
}
also my res/menu/main.xml looks like below.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
...
<item android:id="#+id/menu_refresh" android:icon="#drawable/ic_menu_refresh" android:showAsAction="always" android:title="#string/refresh" />
<item android:id="#+id/menu_search" android:title="#string/menu_search" android:icon="#android:drawable/ic_menu_search" android:showAsAction="never" />
<item android:id="#+id/settings" android:title="#string/settings" android:icon="#android:drawable/ic_menu_preferences"></item>
<item android:id="#+id/exit" android:title="#string/exit" android:icon="#android:drawable/ic_menu_close_clear_cancel"></item>
...
</menu>
The problem is that, I start my activity in portrait for example and hit on the refresh button, it stops nicely. Now i rotate it and hit the refresh button again but now it keeps on showing the indeterminate progress for some reason. I checked, the refreshMenuItem.setActionView(null) is getting called but it still keeps on showing the indeterminate thingy. Also now the refreshMenuItem seems to be invisible for some reason, cause the onOptionsItemSelected is not getting called when i click on the indetermintate progress.
so I tried with a different sample app and it works perfectly without any issues. So i am wondering where could i go wrong. any pointers will help.
Actually I figured out the problem. my project was supposed to handle configuration changes before (so we had configchanges in the manifest). with the actionbarsherlock, it becomes unresponsive with the configchanges.
Now since we were not recreating the activity before on config changes, we were passing the activity in some of our code, which was fine since it was never recreated and we took care of cleaning it. but now to get actionbarsherlock working we have to recreate the activity on configchanges. The earlier instance of the activity was still present in some parts of the code and was not getting overridden with the new instance of the activity. so the menuitem which i was trying to stop spinning was actually belonging to the older instance and not the new one.
so refactored the code to use the current instance of the activity and everything worked great!
Regarding onOptionsItemSelected not getting called when clicking on the intermediate progress: when action view is set, it disables the default behavior of the action item. It also overrides previously set action provider. The only way to handle action view click is by setting OnClickListener.
Regarding intermediate progress not stopping after rotation: I did not have exact this problem. In my case the intermediate progress would not show up at all when I click on the refresh action item, but it would show up nicely if it was triggered from the code. The way I made it work is by moving the code that calls setActionView(...) to Handler.post(Runnable).